Skip to content

Commit

Permalink
test: ✅ adding unit test cases for the application
Browse files Browse the repository at this point in the history
  • Loading branch information
WasiqB committed Dec 2, 2024
1 parent 382d2a7 commit bbf8e9e
Show file tree
Hide file tree
Showing 10 changed files with 2,520 additions and 52 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ node_modules/
.yarn/install-state.gz

# testing
/coverage
coverage/

# next.js
.next/
Expand Down
13 changes: 11 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"build": "turbo build",
"dev": "turbo dev",
"lint": "turbo lint",
"test": "turbo test",
"deploy": "turbo run build --scope=app --includeDependencies --no-deps",
"format:fix": "prettier --write .",
"eslint": "eslint --report-unused-disable-directives",
Expand All @@ -24,6 +25,10 @@
"release:prepatch": "pnpm beta prepatch"
},
"devDependencies": {
"@jest/globals": "^29.7.0",
"@testing-library/dom": "^10.4.0",
"@testing-library/jest-dom": "^6.6.3",
"@testing-library/react": "^16.0.1",
"@eslint/compat": "^1.2.3",
"@next/eslint-plugin-next": "^15.0.3",
"@release-it-plugins/lerna-changelog": "^7.0.0",
Expand All @@ -39,7 +44,7 @@
"eslint-config-turbo": "2.3.3",
"eslint-plugin-only-warn": "^1.1.0",
"eslint-plugin-prettier": "^5.2.1",
"globals": "^15.12.0",
"globals": "^15.13.0",
"husky": "^9.1.7",
"lerna-changelog": "^2.2.0",
"lint-staged": "^15.2.10",
Expand All @@ -50,7 +55,11 @@
"release-it-pnpm": "^4.6.3",
"turbo": "^2.3.3",
"typescript": "^5.7.2",
"typescript-eslint": "^8.16.0"
"typescript-eslint": "^8.16.0",
"jest": "^29.7.0",
"jest-environment-jsdom": "^29.7.0",
"ts-jest": "^29.2.5",
"ts-node": "^10.9.2"
},
"lint-staged": {
"**/*.{ts,tsx}": [
Expand Down
35 changes: 33 additions & 2 deletions packages/utils/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,55 @@
"./constants": "./src/types/constants.ts",
"./cn": "./src/functions/cn.ts",
"./formatting": "./src/functions/formatting.ts",
"./string-util": "./src/functions/string-util.ts",
"./string-util": "./src/functions/string-util/index.ts",
"./xml-parser": "./src/parsers/xml-parser.ts"
},
"scripts": {
"lint": "eslint . --max-warnings 0"
"lint": "eslint . --max-warnings 0",
"test": "jest"
},
"devDependencies": {
"@types/luxon": "^3.4.2",
"@types/xml2js": "^0.4.14",
"@ultra-reporter/typescript-config": "workspace:*"
},
"dependencies": {
"@xmldom/xmldom": "^0.9.5",
"clsx": "^2.1.1",
"date-fns": "^4.1.0",
"luxon": "^3.5.0",
"next": "15.0.3",
"react": "^18",
"tailwind-merge": "^2.5.5",
"xml2js": "^0.6.2"
},
"jest": {
"preset": "ts-jest",
"verbose": true,
"clearMocks": true,
"testEnvironment": "node",
"moduleFileExtensions": [
"js",
"ts"
],
"testMatch": [
"**/*.test.ts"
],
"testPathIgnorePatterns": [
"/node_modules/",
"/dist/"
],
"transform": {
"^.+\\.ts$": "ts-jest"
},
"coverageReporters": [
"json-summary",
"text",
"lcov"
],
"collectCoverage": true,
"collectCoverageFrom": [
"src/**"
]
}
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { DOMParser } from '@xmldom/xmldom';

/* eslint-disable @typescript-eslint/no-unused-vars */
const isJson = (text: string): boolean => {
try {
Expand All @@ -21,7 +23,8 @@ const isXml = (text: string): boolean => {
/* eslint-disable @typescript-eslint/no-unused-vars */
const isBase64Image = (str: string): boolean => {
try {
return btoa(atob(str)) === str;
const content = str.trim();
return content !== '' && btoa(atob(content)) === content;
} catch (err) {
return false;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { describe, expect, it } from '@jest/globals';
import { isBase64Image } from '..';

describe('isBase64Image() isBase64Image method', () => {
describe('Happy Paths', () => {
it('should return true for a valid base64 encoded image string', () => {
const validBase64Image =
'iVBORw0KGgoAAAANSUhEUgAAAAUA' +
'AAAFCAYAAACNbyblAAAAHElEQVQI12P4' +
'//8/w38GIAXDIBKE0DHxgljNBAAO' +
'9TXL0Y4OHwAAAABJRU5ErkJggg==';
expect(isBase64Image(validBase64Image)).toBe(true);
});
});

describe('Edge Cases', () => {
it('should return false for an empty string', () => {
expect(isBase64Image('')).toBe(false);
});

it('should return false for a string with invalid base64 characters', () => {
const invalidBase64 = 'This is not a base64 string!';
expect(isBase64Image(invalidBase64)).toBe(false);
});

it('should return false for a string that is not properly padded', () => {
const improperlyPaddedBase64 = 'iVBORw0KGgoAAAANSUhEUgAAAAUA';
expect(isBase64Image(improperlyPaddedBase64)).toBe(false);
});

it('should return false for a string that decodes to non-image data', () => {
const base64NonImage = btoa('Hello, World!');
expect(isBase64Image(base64NonImage)).toBe(false);
});

it('should return false for a null input', () => {
expect(isBase64Image(null as unknown as string)).toBe(false);
});

it('should return false for a string with whitespace', () => {
const base64WithWhitespace =
'iVBORw0KGgoAAAANSUhEUgAAAAUA AAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU5ErkJggg==';
expect(isBase64Image(base64WithWhitespace)).toBe(false);
});
});
});
73 changes: 73 additions & 0 deletions packages/utils/src/functions/string-util/test/isJson.early.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
import { describe, expect, it } from '@jest/globals';
import { isJson } from '..';

describe('isJson() isJson method', () => {
describe('Happy Paths', () => {
it('should return true for a valid JSON object', () => {
const jsonString = '{"name": "John", "age": 30}';
expect(isJson(jsonString)).toBe(true);
});

it('should return true for a valid JSON array', () => {
const jsonArray = '[1, 2, 3, 4]';
expect(isJson(jsonArray)).toBe(true);
});

it('should return true for a valid JSON string', () => {
const jsonString = '"Hello, World!"';
expect(isJson(jsonString)).toBe(true);
});

it('should return true for a valid JSON number', () => {
const jsonNumber = '12345';
expect(isJson(jsonNumber)).toBe(true);
});

it('should return true for a valid JSON boolean', () => {
const jsonBoolean = 'true';
expect(isJson(jsonBoolean)).toBe(true);
});

it('should return true for a valid JSON null', () => {
const jsonNull = 'null';
expect(isJson(jsonNull)).toBe(true);
});
});

describe('Edge Cases', () => {
it('should return false for an empty string', () => {
const emptyString = '';
expect(isJson(emptyString)).toBe(false);
});

it('should return false for a malformed JSON string', () => {
const malformedJson = '{"name": "John", "age": 30';
expect(isJson(malformedJson)).toBe(false);
});

it('should return false for a string that is not JSON', () => {
const notJson = 'Hello, World!';
expect(isJson(notJson)).toBe(false);
});

it('should return false for a string with only whitespace', () => {
const whitespaceString = ' ';
expect(isJson(whitespaceString)).toBe(false);
});

it('should return false for a number that is not a JSON string', () => {
const numberString = '123abc';
expect(isJson(numberString)).toBe(false);
});

it('should return false for a boolean that is not a JSON string', () => {
const booleanString = 'True';
expect(isJson(booleanString)).toBe(false);
});

it('should return false for a null that is not a JSON string', () => {
const nullString = 'Null';
expect(isJson(nullString)).toBe(false);
});
});
});
62 changes: 62 additions & 0 deletions packages/utils/src/functions/string-util/test/isXml.early.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import { describe, expect, it } from '@jest/globals';
import { isXml } from '..';

describe('isXml() isXml method', () => {
describe('Happy paths', () => {
it('should return true for a valid XML string', () => {
const validXml =
"<note><to>Tove</to><from>Jani</from><heading>Reminder</heading><body>Don't forget me this weekend!</body></note>";
expect(isXml(validXml)).toBe(true);
});

it('should return true for a valid XML string with namespaces', () => {
const validXmlWithNamespace =
'<note xmlns:h="http://www.w3.org/TR/html4/"><h:to>Tove</h:to><h:from>Jani</h:from></note>';
expect(isXml(validXmlWithNamespace)).toBe(true);
});

it('should return true for a valid XML string with self-closing tags', () => {
const validXmlWithSelfClosingTags =
'<note><to>Tove</to><from>Jani</from><heading>Reminder</heading><body/><footer/></note>';
expect(isXml(validXmlWithSelfClosingTags)).toBe(true);
});
});

describe('Edge cases', () => {
it('should return false for an empty string', () => {
expect(isXml('')).toBe(false);
});

it('should return false for a string with only whitespace', () => {
expect(isXml(' ')).toBe(false);
});

it('should return false for a string that is not XML', () => {
const notXml = 'This is not an XML string';
expect(isXml(notXml)).toBe(false);
});

it('should return false for a malformed XML string', () => {
const malformedXml =
"<note><to>Tove</to><from>Jani</from><heading>Reminder</heading><body>Don't forget me this weekend!</body>";
expect(isXml(malformedXml)).toBe(false);
});

it('should return false for a JSON string', () => {
const jsonString = '{"name": "John", "age": 30, "city": "New York"}';
expect(isXml(jsonString)).toBe(false);
});

it('should return false for a string with XML-like structure but invalid syntax', () => {
const invalidXmlSyntax =
"<note><to>Tove</to><from>Jani</from><heading>Reminder</heading><body>Don't forget me this weekend!</body><note>";
expect(isXml(invalidXmlSyntax)).toBe(false);
});

it('should return false for a string with special characters', () => {
const specialChars =
"<note><to>Tove</to><from>Jani</from><heading>Reminder</heading><body>Don't forget me this weekend!@#$%^&*()</body></note>";
expect(isXml(specialChars)).toBe(true);
});
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
import { describe, expect, it } from '@jest/globals';
import { prettifyJson } from '../index';

describe('prettifyJson() prettifyJson method', () => {
describe('Happy Paths', () => {
it('should prettify a valid JSON string', () => {
const input = '{"name":"John","age":30,"city":"New York"}';
const expectedOutput = `{
"name": "John",
"age": 30,
"city": "New York"
}`;
expect(prettifyJson(input)).toBe(expectedOutput);
});

it('should handle an already prettified JSON string', () => {
const input = `{
"name": "John",
"age": 30,
"city": "New York"
}`;
const expectedOutput = `{
"name": "John",
"age": 30,
"city": "New York"
}`;
expect(prettifyJson(input)).toBe(expectedOutput);
});

it('should handle a JSON string with arrays', () => {
const input = '{"fruits":["apple","banana","cherry"]}';
const expectedOutput = `{
"fruits": [
"apple",
"banana",
"cherry"
]
}`;
expect(prettifyJson(input)).toBe(expectedOutput);
});
});

describe('Edge Cases', () => {
it('should return the original string if it is not valid JSON', () => {
const input = 'Invalid JSON string';
expect(prettifyJson(input)).toBe(input);
});

it('should handle an empty string', () => {
const input = '';
expect(prettifyJson(input)).toBe(input);
});

it('should handle a JSON string with special characters', () => {
const input = '{"special":"!@#$%^&*()_+{}|:\"<>?"}';

Check failure on line 55 in packages/utils/src/functions/string-util/test/prettifyJson.early.test.ts

View workflow job for this annotation

GitHub Actions / lint

Unnecessary escape character: \"
const expectedOutput = `{
"special": "!@#$%^&*()_+{}|:\\\"<>?"

Check failure on line 57 in packages/utils/src/functions/string-util/test/prettifyJson.early.test.ts

View workflow job for this annotation

GitHub Actions / lint

Unnecessary escape character: \"
}`;
expect(prettifyJson(input)).toBe(expectedOutput);
});

it('should handle a JSON string with numbers and booleans', () => {
const input = '{"number":123,"boolean":true}';
const expectedOutput = `{
"number": 123,
"boolean": true
}`;
expect(prettifyJson(input)).toBe(expectedOutput);
});

it('should handle a JSON string with nested objects', () => {
const input = '{"person":{"name":"John","age":30}}';
const expectedOutput = `{
"person": {
"name": "John",
"age": 30
}
}`;
expect(prettifyJson(input)).toBe(expectedOutput);
});
});
});
Loading

0 comments on commit bbf8e9e

Please sign in to comment.