Skip to content

Commit

Permalink
test: Adding testing files and configurations
Browse files Browse the repository at this point in the history
  • Loading branch information
gabriel-logan committed Mar 16, 2024
1 parent b3d3f9c commit 17f12e0
Show file tree
Hide file tree
Showing 33 changed files with 794 additions and 94 deletions.
2 changes: 2 additions & 0 deletions packages/typescript/.eslintignore
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
dist/
node_modules/
tests/**/*
4 changes: 3 additions & 1 deletion packages/typescript/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
"main": "./dist/index.js",
"types": "./types/index.d.ts",
"scripts": {
"test": "jest",
"test": "jest --coverage",
"test:file": "jest tests/src/validatePassword.test --watch",
"test:watch": "jest --watch",
"build": "npx tsc"
},
"repository": {
Expand Down
2 changes: 1 addition & 1 deletion packages/typescript/src/isAscii.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
*/
function isAscii(value: string): boolean {
if (typeof value !== 'string') {
throw new TypeError('Input value must be a string.');
throw new TypeError('The input should be a string.');
}

if (value.trim().length === 0) {
Expand Down
57 changes: 57 additions & 0 deletions packages/typescript/tests/src/cnpjValidator.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import cnpjIsValid from '../../src/cnpjValidator';

describe('cnpjIsValid function', () => {
test('should validate a valid CNPJ', () => {
const result = cnpjIsValid('72.501.263/0001-40');
expect(result.isValid).toBe(false);
expect(result.errorMsg).toBe("CNPJ is not valid");
});

test('should invalidate an invalid CNPJ', () => {
const result = cnpjIsValid('12.345.678/0001-91');
expect(result.isValid).toBe(false);
expect(result.errorMsg).toBe('CNPJ is not valid');
});

test('should invalidate a CNPJ with incorrect length', () => {
const result = cnpjIsValid('1234567890123');
expect(result.isValid).toBe(false);
expect(result.errorMsg).toBe('CNPJ must have 14 numerical digits');
});

test('should invalidate a CNPJ with non-digit characters', () => {
const result = cnpjIsValid('72.501.263/0001-4A');
expect(result.isValid).toBe(false);
expect(result.errorMsg).toBe('CNPJ is not valid');
});

test('should invalidate an empty CNPJ', () => {
const result = cnpjIsValid('');
expect(result.isValid).toBe(false);
expect(result.errorMsg).toBe('CNPJ invalid');
});

test('should throw an error if input is not a string', () => {
expect(() => {
cnpjIsValid(12345678901234 as any);
}).toThrow('The input should be a string.');
});

test('should throw an error if errorMsg is not an array', () => {
expect(() => {
cnpjIsValid('72.501.263/0001-40', 'error message' as any);
}).toThrow('Must be an Array');
});

test('should throw an error if errorMsg contains non-string values', () => {
expect(() => {
cnpjIsValid('72.501.263/0001-40', [123, 'error message'] as any);
}).toThrow('All values within the array must be strings or null/undefined.');
});

test('should return custom error messages', () => {
const result = cnpjIsValid('12.345.678/0001-91', ['Custom invalid message', 'Custom length message', 'Custom not valid message', 'Custom unknown error message']);
expect(result.isValid).toBe(false);
expect(result.errorMsg).toBe('Custom not valid message');
});
});
27 changes: 27 additions & 0 deletions packages/typescript/tests/src/cpfValidator.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import cpfIsValid from '../../src/cpfValidator';

describe('cpfIsValid', () => {
it('should return isValid as false and the correct error message when CPF is invalid', () => {
const result = cpfIsValid('12345678902');
expect(result.isValid).toBe(false);
expect(result.errorMsg).toBe('CPF is not valid');
});

it('should return isValid as true and errorMsg as null when CPF is valid', () => {
const result = cpfIsValid('12345678909');
expect(result.isValid).toBe(true);
expect(result.errorMsg).toBe(null);
});

it('should throw an error when the input is not a string', () => {
expect(() => cpfIsValid(12345678910 as any)).toThrow('The input should be a string.');
});

it('should throw an error when errorMsg is not an array', () => {
expect(() => cpfIsValid('12345678910', 'not an array' as any)).toThrow('Must be an Array');
});

it('should throw an error when errorMsg contains non-string values', () => {
expect(() => cpfIsValid('12345678910', [123 as any, 'error message'])).toThrow('All values within the array must be strings or null/undefined.');
});
});
33 changes: 33 additions & 0 deletions packages/typescript/tests/src/getOnlyEmail.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import getOnlyEmail from '../../src/getOnlyEmail';

describe('getOnlyEmail', () => {
it('should return the first email when multiple is false', () => {
const result = getOnlyEmail("Entre em contato com a equipe: joao@empresa.com, maria@empresa.com, contato@empresa.com", false);
expect(result).toBe('joao@empresa.com');
});

it('should return all emails when multiple is true', () => {
const result = getOnlyEmail("Entre em contato com a equipe: joao@empresa.com, maria@empresa.com, contato@empresa.com", true);
expect(result).toEqual(['joao@empresa.com', 'maria@empresa.com', 'contato@empresa.com']);
});

it('should return cleaned emails when cleanDomain is true', () => {
const result = getOnlyEmail("Entre em contato com a equipe: joao@empresa.com.br, maria@empresa.com.io, contato@empresa.com.pt", true, true);
expect(result).toEqual(['joao@empresa.com.br', 'maria@empresa.com.io', 'contato@empresa.com.pt']);
});

it('should return unique emails when repeatEmail is false', () => {
const result = getOnlyEmail("Entre em contato com a equipe: joao@empresa.com, joao@empresa.com, joao@empresa.com", true, false, false);
expect(result).toEqual(['joao@empresa.com']);
});

it('should return repeated emails when repeatEmail is true', () => {
const result = getOnlyEmail("Entre em contato com a equipe: joao@empresa.com, joao@empresa.com, joao@empresa.com", true, false, true);
expect(result).toEqual(['joao@empresa.com', 'joao@empresa.com', 'joao@empresa.com']);
});

it('should return "No email found" when no email is present', () => {
const result = getOnlyEmail("Entre em contato com a equipe", false);
expect(result).toBe('No email found');
});
});
62 changes: 62 additions & 0 deletions packages/typescript/tests/src/identifyFlagCard.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import identifyFlagCard from '../../src/identifyFlagCard';

describe('identifyFlagCard', () => {
it('should identify Visa card', () => {
const result = identifyFlagCard('4111111111111111');
expect(result).toBe('Visa');
});

it('should identify Mastercard', () => {
const result = identifyFlagCard('5555555555554444');
expect(result).toBe('Mastercard');
});

it('should identify American Express', () => {
const result = identifyFlagCard('378282246310005');
expect(result).toBe('American Express');
});

it('should identify Discover', () => {
const result = identifyFlagCard('6011111111111117');
expect(result).toBe('Discover');
});

it('should identify JCB', () => {
const result = identifyFlagCard('3530111333300000');
expect(result).toBe('JCB');
});

it('should identify Diners Club', () => {
const result = identifyFlagCard('30569309025904');
expect(result).toBe('Diners Club');
});

it('should identify Maestro', () => {
const result = identifyFlagCard('6759649826438453');
expect(result).toBe('Maestro');
});

it('should identify UnionPay', () => {
const result = identifyFlagCard('6221558812340002');
expect(result).toBe('UnionPay');
});

it('should identify Unknown', () => {
const result = identifyFlagCard('6362970000457013');
expect(result).toBe('Unknown');
});

it('should identify Hipercard', () => {
const result = identifyFlagCard('6062825624254001');
expect(result).toBe('Hipercard');
});

it('should return "Unknown" for unknown card numbers', () => {
const result = identifyFlagCard('1234567812345678');
expect(result).toBe('Unknown');
});

it('should throw an error when the input is not a string', () => {
expect(() => identifyFlagCard((1234567812345678 as any))).toThrow('The input should be a string.');
});
});
17 changes: 17 additions & 0 deletions packages/typescript/tests/src/isAscii.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import isAscii from '../../src/isAscii';

describe('isAscii', () => {
it('should return true when the input is ASCII', () => {
const result = isAscii('valid ASCII');
expect(result).toBe(true);
});

it('should return false when the input is not ASCII', () => {
const result = isAscii('日本語日本語');
expect(result).toBe(false);
});

it('should throw an error when the input is not a string', () => {
expect(() => isAscii((12345678910 as any))).toThrow('The input should be a string.');
});
});
26 changes: 26 additions & 0 deletions packages/typescript/tests/src/isBase64.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import isBase64 from '../../src/isBase64';

describe('isBase64', () => {
it('should return true when the input is a valid Base64 string', () => {
const result = isBase64('SGVsbG8gV29ybGQh');
expect(result).toBe(true);
});

it('should return false when the input is not a valid Base64 string', () => {
const result = isBase64('こんにちは');
expect(result).toBe(false);
});

it('should return false when the input is not a valid Base64 string', () => {
const result = isBase64('12345');
expect(result).toBe(false);
});

it('should throw an error when the input is not a string', () => {
expect(() => isBase64((12345678910 as any))).toThrow('Input value must be a string.');
});

it('should throw an error when the input is an empty string', () => {
expect(() => isBase64('')).toThrow('Input value must not be an empty string.');
});
});
32 changes: 32 additions & 0 deletions packages/typescript/tests/src/isCEP.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import isCEP from '../../src/isCEP';

describe('isCEP', () => {
it('should return true when the input is a valid CEP', () => {
const result = isCEP('12345-678');
expect(result).toBe(true);
});

it('should return true when the input is a valid CEP without hyphen', () => {
const result = isCEP('12345678');
expect(result).toBe(true);
});

it('should return true when the input is a valid CEP with dot and hyphen', () => {
const result = isCEP('12.345-678');
expect(result).toBe(true);
});

it('should return false when the input is not a valid CEP', () => {
const result = isCEP('1234567');
expect(result).toBe(false);
});

it('should return false when the input is not a valid CEP', () => {
const result = isCEP('123456789');
expect(result).toBe(false);
});

it('should throw an error when the input is not a string', () => {
expect(() => isCEP((12345678 as any))).toThrow('Input value must be a string.');
});
});
18 changes: 18 additions & 0 deletions packages/typescript/tests/src/isCreditCardValid.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// Testes para isCreditCardValid
import isCreditCardValid from '../../src/isCreditCardValid';

describe('isCreditCardValid', () => {
it('should return true when the input is a valid credit card number', () => {
const result = isCreditCardValid('5344 9393 8369 0842');
expect(result).toBe(true);
});

it('should return false when the input is not a valid credit card number', () => {
const result = isCreditCardValid('4111111111111112');
expect(result).toBe(false);
});

it('should throw an error when the input is not a string', () => {
expect(() => isCreditCardValid((1234567812345678 as any))).toThrow('The input should be a string.');
});
});
13 changes: 13 additions & 0 deletions packages/typescript/tests/src/isDate.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// Testes para isDate
import isDate from '../../src/isDate';

describe('isDate', () => {
it('should return true when the input is a valid date', () => {
const result = isDate('2022-12-31');
expect(result).toBe(true);
});

it('should throw an error when the input is not a string', () => {
expect(() => isDate((12345678 as any))).toThrow('Input value must be a string.');
});
});
14 changes: 14 additions & 0 deletions packages/typescript/tests/src/isDecimal.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// Testes para isDecimal
import isDecimal from '../../src/isDecimal';

describe('isDecimal', () => {
it('should return true when the input is a valid decimal number', () => {
const result = isDecimal('123.45');
expect(result).toBe(true);
});

it('should return false when the input is not a valid decimal number', () => {
const result = isDecimal('123.456.789');
expect(result).toBe(false);
});
});
32 changes: 32 additions & 0 deletions packages/typescript/tests/src/isEmail.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import isEmail from '../../src/isEmail';

describe('isEmail', () => {
it('returns true for valid email', () => {
expect(isEmail('foo@bar.com')).toBe(true);
});

it('returns false for email without domain', () => {
expect(isEmail('foo@bar')).toBe(false);
});

it('returns false for email starting with a number', () => {
expect(isEmail('1foo@bar.com')).toBe(false);
});

it('returns false for email with number after @', () => {
expect(isEmail('foo@1bar.com')).toBe(false);
});

it('returns false for email with number after last dot', () => {
expect(isEmail('foo@bar.1com')).toBe(false);
});

it('returns false for non-string input', () => {
// @ts-ignore
expect(isEmail(123)).toBe(false);
});

it('returns false for null or empty input', () => {
expect(isEmail('')).toBe(false);
});
});
14 changes: 14 additions & 0 deletions packages/typescript/tests/src/isEmpty.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// Testes para isDecimal
import isDecimal from '../../src/isDecimal';

describe('isDecimal', () => {
it('should return true when the input is a valid decimal number', () => {
const result = isDecimal('123.45');
expect(result).toBe(true);
});

it('should return false when the input is not a valid decimal number', () => {
const result = isDecimal('123.456.789');
expect(result).toBe(false);
});
});
18 changes: 18 additions & 0 deletions packages/typescript/tests/src/isMACAddress.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// Testes para isMacAddress
import isMacAddress from '../../src/isMACAddress';

describe('isMacAddress', () => {
it('should return true when the input is a valid MAC address', () => {
const result = isMacAddress('00:0a:95:9d:68:16');
expect(result).toBe(true);
});

it('should return false when the input is not a valid MAC address', () => {
const result = isMacAddress('00:0a:95:9d:68:1g');
expect(result).toBe(false);
});

it('should throw an error when the input is not a string', () => {
expect(() => isMacAddress((12345678 as any))).toThrow('The input should be a string.');
});
});
Loading

0 comments on commit 17f12e0

Please sign in to comment.