Skip to content

Commit

Permalink
refactor: improve CPF validation tests and error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
gabriel-logan committed Jun 14, 2024
1 parent 0767bdf commit d28c2e5
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 23 deletions.
2 changes: 2 additions & 0 deletions packages/typescript/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,5 @@ node_modules/
BackupCopia/
todo.txt
teste.html
dist
types
68 changes: 45 additions & 23 deletions packages/typescript/tests/src/cpfValidator.test.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,49 @@
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.');
});
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 return isValid as false and the correct error message when CPF is invalid', () => {
const result = cpfIsValid('123.456.789-02');
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('123.456.789-09');
expect(result.isValid).toBe(true);
expect(result.errorMsg).toBe(null);
});

it('should return isValid as false and the correct error message when CPF is invalid', () => {
const result = cpfIsValid('123456789-02');
expect(result.isValid).toBe(false);
expect(result.errorMsg).toBe("CPF must have 11 numerical digits");
});

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.');
});

it('should throw an error when errorMsg contains non-string values', () => {
expect(() => cpfIsValid('12345678910', ['error message', 123 as any])).toThrow('All values within the array must be strings or null/undefined.');
});
});

0 comments on commit d28c2e5

Please sign in to comment.