-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: update type assertions in tests and improve linting scripts
- Loading branch information
1 parent
83dfbe8
commit 2a98c5c
Showing
37 changed files
with
560 additions
and
427 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
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
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
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
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
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,87 +1,88 @@ | ||
import cpfIsValid from "../../src/cpfValidator"; | ||
import type { ValidateFunctions } from "../../src/types"; | ||
|
||
describe("cpfIsValid", () => { | ||
it("should return isValid as false and the correct error message when CPF is invalid", () => { | ||
const result = cpfIsValid("12345678902"); | ||
const result: ValidateFunctions = cpfIsValid("12345678902"); | ||
expect(result.isValid).toBe(false); | ||
expect(result.errorMsg).toBe("CPF is not valid"); | ||
}); | ||
|
||
test("errorMessage should be default if pass an empty array", () => { | ||
const result = cpfIsValid("12345678902", []); | ||
const result: ValidateFunctions = cpfIsValid("12345678902", []); | ||
expect(result.isValid).toBe(false); | ||
}); | ||
|
||
test("errorMessage should be default if errorMsg[index] is falsy", () => { | ||
const result = cpfIsValid("12345678902", null); | ||
const result: ValidateFunctions = cpfIsValid("12345678902", null); | ||
expect(result.isValid).toBe(false); | ||
}); | ||
|
||
it("should return isValid as true and errorMsg as null when CPF is valid", () => { | ||
const result = cpfIsValid("12345678909"); | ||
const result: ValidateFunctions = 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"); | ||
const result: ValidateFunctions = 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"); | ||
const result: ValidateFunctions = 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"); | ||
const result: ValidateFunctions = cpfIsValid("123456789-02"); | ||
expect(result.isValid).toBe(false); | ||
expect(result.errorMsg).toBe("CPF is not valid"); | ||
}); | ||
|
||
it("should throw an error when the input is not a string", () => { | ||
expect(() => cpfIsValid(12345678910 as any)).toThrow( | ||
expect(() => cpfIsValid(12345678910 as unknown as string)).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", | ||
); | ||
expect(() => | ||
cpfIsValid("12345678910", "not an array" as unknown as string[]), | ||
).toThrow("Must be an Array"); | ||
}); | ||
|
||
it("should throw an error when errorMsg contains non-string values", () => { | ||
expect(() => | ||
cpfIsValid("12345678910", [123 as any, "error message"]), | ||
cpfIsValid("12345678910", [123 as unknown as string, "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]), | ||
cpfIsValid("12345678910", ["error message", 123 as unknown as string]), | ||
).toThrow("All values within the array must be strings or null/undefined."); | ||
}); | ||
|
||
it("it should return false when all digits are repeated", () => { | ||
const result = cpfIsValid("11111111111"); | ||
const result: ValidateFunctions = cpfIsValid("11111111111"); | ||
expect(result.isValid).toBe(false); | ||
expect(result.errorMsg).toBe("CPF is not valid"); | ||
}); | ||
|
||
it("should return isValid as false and the correct error message when CPF is null or empty", () => { | ||
const result = cpfIsValid(""); | ||
const result: ValidateFunctions = cpfIsValid(""); | ||
expect(result.isValid).toBe(false); | ||
|
||
expect(() => cpfIsValid(null as any)).toThrow( | ||
expect(() => cpfIsValid(null as unknown as string)).toThrow( | ||
"The input should be a string.", | ||
); | ||
}); | ||
|
||
it("should return isValid as false and the correct error message when CPF does not have 11 digits after cleaning", () => { | ||
const result = cpfIsValid("123.456.789-0"); | ||
const result: ValidateFunctions = cpfIsValid("123.456.789-0"); | ||
expect(result.isValid).toBe(false); | ||
}); | ||
}); |
Oops, something went wrong.