-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: improve CPF validation tests and error handling
- Loading branch information
1 parent
0767bdf
commit d28c2e5
Showing
2 changed files
with
47 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,3 +3,5 @@ node_modules/ | |
BackupCopia/ | ||
todo.txt | ||
teste.html | ||
dist | ||
types |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.'); | ||
}); | ||
}); |