Skip to content

Commit

Permalink
add tests and try workflow
Browse files Browse the repository at this point in the history
  • Loading branch information
GuilhermeAlbert committed Aug 3, 2024
1 parent 2c3e4b8 commit bd489a9
Show file tree
Hide file tree
Showing 22 changed files with 1,403 additions and 886 deletions.
1 change: 1 addition & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
NPM_TOKEN=
63 changes: 63 additions & 0 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
name: Publish to npm and create GitHub Release

on:
push:
branches:
- main

jobs:
publish:
runs-on: ubuntu-latest

steps:
- name: Checkout code
uses: actions/checkout@v2

- name: Set up Node.js
uses: actions/setup-node@v2
with:
node-version: "18"

- name: Install dependencies
run: yarn install

- name: Build package
run: yarn build

- name: Get the latest tag
id: get_latest_tag
run: echo ::set-output name=tag::$(git describe --tags `git rev-list --tags --max-count=1`)

- name: Calculate new tag
id: new_tag
run: |
echo "Current tag: ${{ steps.get_latest_tag.outputs.tag }}"
NEW_TAG=$(echo ${{ steps.get_latest_tag.outputs.tag }} | awk -F. -v OFS=. '{$NF += 1 ; print}')
echo "New tag: $NEW_TAG"
echo ::set-output name=tag::$NEW_TAG
- name: Publish to npm
run: yarn publish
env:
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}

- name: Create GitHub Release
id: create_release
uses: actions/create-release@v1
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
tag_name: ${{ steps.new_tag.outputs.tag }}
release_name: Release ${{ steps.new_tag.outputs.tag }}
draft: false
prerelease: false

- name: Upload Release Asset
uses: actions/upload-release-asset@v1
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
upload_url: ${{ steps.create_release.outputs.upload_url }}
asset_path: ./dist/index.js
asset_name: index.js
asset_content_type: application/javascript
45 changes: 35 additions & 10 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,19 +1,44 @@
# Node modules
node_modules
node_modules/

# Build output
dist
dist/

# Logs
logs
coverage/
jest-config.js
jest.setup.js

logs/
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*

# Editor directories and files
.DS_Store
Thumbs.db
.vscode/
.idea/
*.sublime-project
*.sublime-workspace
.DS_Store

*.tgz

.env.local
.env.development.local
.env.test.local
.env.production.local

.env

.npm/
*.cache

.cache/
.next/

coverage/

test-results/

*.tmp
*.temp

build/

.npmrc
2 changes: 2 additions & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,6 @@ module.exports = {
},
testRegex: "(/__tests__/.*|(\\.|/)(test|spec))\\.tsx?$",
moduleFileExtensions: ["ts", "tsx", "js", "jsx", "json", "node"],
collectCoverage: true,
coverageDirectory: "coverage",
};
14 changes: 9 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@
"build": "tsc",
"test": "jest",
"lint": "eslint . --ext .ts,.tsx",
"prepublishOnly": "npm run build"
"prepublishOnly": "yarn build"
},
"repository": {
"type": "git",
"url": "https://github.com/yourusername/lyra.git"
"url": "https://github.com/TreblaTechnologies/lyra.git"
},
"keywords": [
"helpers",
Expand All @@ -21,19 +21,23 @@
"constants",
"utility"
],
"author": "Your Name <your.email@example.com>",
"author": "Trebla <opensource@trebla.com.br>",
"license": "MIT",
"dependencies": {
"axios": "^1.7.3",
"react": "^17.0.0"
},
"devDependencies": {
"@types/jest": "^27.0.0",
"@testing-library/react-hooks": "7.0.0",
"@types/jest": "^29.5.12",
"@types/node": "^16.0.0",
"@types/react": "^17.0.0",
"@typescript-eslint/eslint-plugin": "^5.0.0",
"@typescript-eslint/parser": "^5.0.0",
"eslint": "^8.0.0",
"jest": "^27.0.0",
"jest": "^29.7.0",
"react-test-renderer": "17.0.2",
"ts-jest": "^29.2.4",
"typescript": "^4.0.0"
}
}
23 changes: 23 additions & 0 deletions src/__tests__/entities/entity-item.entity.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { EntityItem } from "../../entities/entity-item.entity";

describe("EntityItem Interface", () => {
it("should allow a string label and value", () => {
const item: EntityItem = {
label: "Test Label",
value: "Test Value",
};

expect(item.label).toBe("Test Label");
expect(item.value).toBe("Test Value");
});

it("should allow a string label and a number value", () => {
const item: EntityItem = {
label: "Test Label",
value: 123,
};

expect(item.label).toBe("Test Label");
expect(item.value).toBe(123);
});
});
61 changes: 61 additions & 0 deletions src/__tests__/enums/http.enum.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import {
ContentTypes,
HttpMethods,
ResponseEncodings,
ResponseHeaders,
ResponseTypes,
} from "../../enums/http.enum";

describe("HTTP Enums", () => {
it("should have correct values for ContentTypes", () => {
expect(ContentTypes.Html).toBe("text/html");
expect(ContentTypes.Plain).toBe("text/plain");
expect(ContentTypes.FormData).toBe("multipart/form-data");
expect(ContentTypes.Json).toBe("application/json");
expect(ContentTypes.UrlEncoded).toBe("application/x-www-form-urlencoded");
expect(ContentTypes.OctetStream).toBe("application/octet-stream");
});

it("should have correct values for ResponseHeaders", () => {
expect(ResponseHeaders.Server).toBe("Server");
expect(ResponseHeaders.ContentType).toBe("Content-Type");
expect(ResponseHeaders.ContentLength).toBe("Content-Length");
expect(ResponseHeaders.CacheControl).toBe("Cache-Control");
expect(ResponseHeaders.ContentEncoding).toBe("Content-Encoding");
});

it("should have correct values for HttpMethods", () => {
expect(HttpMethods.Get).toBe("GET");
expect(HttpMethods.Delete).toBe("DELETE");
expect(HttpMethods.Head).toBe("HEAD");
expect(HttpMethods.Options).toBe("OPTIONS");
expect(HttpMethods.Post).toBe("POST");
expect(HttpMethods.Put).toBe("PUT");
expect(HttpMethods.Patch).toBe("PATCH");
expect(HttpMethods.Purge).toBe("PURGE");
expect(HttpMethods.Link).toBe("LINK");
expect(HttpMethods.Unlink).toBe("UNLINK");
});

it("should have correct values for ResponseTypes", () => {
expect(ResponseTypes.ArrayBuffer).toBe("arraybuffer");
expect(ResponseTypes.Blob).toBe("blob");
expect(ResponseTypes.Document).toBe("document");
expect(ResponseTypes.Json).toBe("json");
expect(ResponseTypes.Text).toBe("text");
expect(ResponseTypes.Stream).toBe("stream");
});

it("should have correct values for ResponseEncodings", () => {
expect(ResponseEncodings.Ascii).toBe("ascii");
expect(ResponseEncodings.Ansi).toBe("ansi");
expect(ResponseEncodings.Binary).toBe("binary");
expect(ResponseEncodings.Base64).toBe("base64");
expect(ResponseEncodings.Base64Url).toBe("base64url");
expect(ResponseEncodings.Hex).toBe("hex");
expect(ResponseEncodings.Latin1).toBe("latin1");
expect(ResponseEncodings.Ucs2).toBe("ucs-2");
expect(ResponseEncodings.Utf8).toBe("utf-8");
expect(ResponseEncodings.Utf16le).toBe("utf16le");
});
});
11 changes: 11 additions & 0 deletions src/__tests__/enums/time-duration.enum.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { TimeDurations } from "../../enums/time-duration.enum";

describe("TimeDurations Enum", () => {
it("should have correct values", () => {
expect(TimeDurations.HalfSecond).toBe(500);
expect(TimeDurations.OneMinute).toBe(60000);
expect(TimeDurations.TwoMinutes).toBe(120000);
expect(TimeDurations.FiveMinutes).toBe(300000);
expect(TimeDurations.OneDay).toBe(86400000);
});
});
56 changes: 56 additions & 0 deletions src/__tests__/helpers/string.helper.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import {
getFirstTwoWords,
getFirstWord,
getInitials,
getLastWord,
getPluralOrSingular,
isShortWord,
} from "../../helpers/string.helper";

describe("String Utilities", () => {
it("should get the first word", () => {
expect(getFirstWord("hello world")).toBe("hello");
expect(getFirstWord("hello")).toBe("hello");
expect(getFirstWord(" hello ")).toBe("hello");
});

it("should get the last word", () => {
expect(getLastWord("hello world")).toBe("world");
expect(getLastWord("world")).toBe("world");
expect(getLastWord(" world ")).toBe("world");
});

it("should get the first two words", () => {
expect(getFirstTwoWords("hello beautiful world")).toBe("hello beautiful");
expect(getFirstTwoWords("hello")).toBe("hello");
expect(getFirstTwoWords("hello beautiful world", false)).toBe(
"hello beautiful"
);
expect(getFirstTwoWords("hello the world", true)).toBe("hello the world");
expect(getFirstTwoWords("hello the the world", true)).toBe(
"hello the the world"
);
});

it("should identify short words correctly", () => {
expect(isShortWord("the")).toBe(true);
expect(isShortWord("a")).toBe(true);
expect(isShortWord("no")).toBe(true);
expect(isShortWord("hello")).toBe(false);
expect(isShortWord("world")).toBe(false);
});

it("should return plural or singular based on count", () => {
expect(getPluralOrSingular(1, "apple", "apples")).toBe("1 apple");
expect(getPluralOrSingular(2, "apple", "apples")).toBe("2 apples");
expect(getPluralOrSingular(0, "apple", "apples")).toBe("");
});

it("should get initials from text", () => {
expect(getInitials("John Doe")).toBe("JD");
expect(getInitials("SingleName")).toBe("S");
expect(getInitials(" John Doe ")).toBe("JD");
expect(getInitials("John the Doe")).toBe("JD");
expect(getInitials("John the the Doe")).toBe("JD");
});
});
Loading

0 comments on commit bd489a9

Please sign in to comment.