Skip to content

Commit

Permalink
refactor: update type assertions in tests and improve linting scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
gabriel-logan committed Feb 5, 2025
1 parent 83dfbe8 commit 2a98c5c
Show file tree
Hide file tree
Showing 37 changed files with 560 additions and 427 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@
"build:src": "npx tsup index.ts --format esm --legacy-output",
"build:browser": "webpack",
"build": "yarn build:types && yarn build:browser",
"lint": "eslint **/*.ts",
"lint:fix": "eslint **/*.ts --fix",
"lint": "eslint src/**/*.ts tests/**/*.ts",
"lint:fix": "yarn lint --fix",
"prettier": "prettier --write '**/*.{ts,js}'",
"pr-check": "yarn prettier && yarn lint && yarn build && yarn test"
},
Expand Down
7 changes: 3 additions & 4 deletions src/cnpjValidator.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import type { ValidateFunctions } from "./types";

// Função para calcular o primeiro dígito verificador
function calculateFirstVerifier(cnpjBase: number[]): number {
const weight: Array<number> = [5, 4, 3, 2, 9, 8, 7, 6, 5, 4, 3, 2];
Expand Down Expand Up @@ -48,10 +50,7 @@ const defaultErrorMsg: string[] = [
function cnpjIsValid(
cnpj: string,
errorMsg: (string | null)[] | null = defaultErrorMsg,
): {
isValid: boolean;
errorMsg: string | null;
} {
): ValidateFunctions {
if (typeof cnpj !== "string") {
throw new TypeError("The input should be a string.");
}
Expand Down
7 changes: 3 additions & 4 deletions src/cpfValidator.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import type { ValidateFunctions } from "./types";

const defaultErrorMsg: string[] = [
"CPF invalid",
"CPF must have 11 numerical digits",
Expand All @@ -21,10 +23,7 @@ const defaultErrorMsg: string[] = [
function cpfIsValid(
cpf: string,
errorMsg: (string | null)[] | null = defaultErrorMsg,
): {
isValid: boolean;
errorMsg: string | null;
} {
): ValidateFunctions {
if (typeof cpf !== "string") {
throw new TypeError("The input should be a string.");
}
Expand Down
4 changes: 2 additions & 2 deletions src/validatePassword.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ function validatePassword(
};
}

function validateErrorMsg(errorMsg: (string | null)[] | undefined) {
function validateErrorMsg(errorMsg: (string | null)[] | undefined): void {
if (errorMsg) {
if (!Array.isArray(errorMsg))
throw new Error("errorMsg must be an Array or null");
Expand All @@ -139,7 +139,7 @@ function validateErrorMsg(errorMsg: (string | null)[] | undefined) {
function validateLengthParams(
minLenthPassword: number,
maxLenthPassword: number,
) {
): void {
if (
typeof minLenthPassword !== "number" ||
typeof maxLenthPassword !== "number"
Expand Down
39 changes: 23 additions & 16 deletions tests/src/cnpjValidator.test.ts
Original file line number Diff line number Diff line change
@@ -1,64 +1,68 @@
import cnpjIsValid from "../../src/cnpjValidator";
import type { ValidateFunctions } from "../../src/types";

describe("cnpjIsValid function", () => {
test("should validate a valid CNPJ", () => {
const result = cnpjIsValid("72.501.263/0001-40");
const result: ValidateFunctions = 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");
const result: ValidateFunctions = 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 non-digit characters", () => {
const result = cnpjIsValid("72.501.263/0001-4A");
const result: ValidateFunctions = cnpjIsValid("72.501.263/0001-4A");
expect(result.isValid).toBe(false);
expect(result.errorMsg).toBe("CNPJ is not valid");
});

test("should return false if cnpj length is not 14 or 18", () => {
const result = cnpjIsValid("123456789012");
const result: ValidateFunctions = cnpjIsValid("123456789012");
expect(result.isValid).toBe(false);
expect(result.errorMsg).toBe("CNPJ must have 14 numerical digits");
});

test("should return false if cnpj has valid length but invalid verifier digits", () => {
const result = cnpjIsValid("12.345.678/0001-00");
const result: ValidateFunctions = cnpjIsValid("12.345.678/0001-00");
expect(result.isValid).toBe(false);
expect(result.errorMsg).toBe("CNPJ is not valid");
});

test("should invalidate an empty CNPJ", () => {
const result = cnpjIsValid("");
const result: ValidateFunctions = 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);
cnpjIsValid(12345678901234 as unknown as string);
}).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);
cnpjIsValid("72.501.263/0001-40", "error message" as unknown as string[]);
}).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);
cnpjIsValid("72.501.263/0001-40", [
123,
"error message",
] as unknown as string[]);
}).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", [
const result: ValidateFunctions = cnpjIsValid("12.345.678/0001-91", [
"Custom invalid message",
"Custom length message",
"Custom not valid message",
Expand All @@ -69,30 +73,33 @@ describe("cnpjIsValid function", () => {
});

test("should return false when all digits are repeated", () => {
const result = cnpjIsValid("11.111.111/1111-11");
const result: ValidateFunctions = cnpjIsValid("11.111.111/1111-11");
expect(result.isValid).toBe(false);
expect(result.errorMsg).toBe("CNPJ is not valid");
});

test("should return false when all digits are the same", () => {
const result = cnpjIsValid("11.111.111/1111-11");
const result: ValidateFunctions = cnpjIsValid("11.111.111/1111-11");
expect(result.isValid).toBe(false);
expect(result.errorMsg).toBe("CNPJ is not valid");
});

test("should return default error messages when errorMsg['etc', null] is passed", () => {
const result = cnpjIsValid("12.345.678/0001-91", ["etc", null]);
const result: ValidateFunctions = cnpjIsValid("12.345.678/0001-91", [
"etc",
null,
]);
expect(result.errorMsg).toBe("CNPJ is not valid");
});

test("should return default error messages when errorMsg is null", () => {
const result = cnpjIsValid("12.345.678/0001-91", null);
const result: ValidateFunctions = cnpjIsValid("12.345.678/0001-91", null);
expect(result.errorMsg).toBe("CNPJ is not valid");
});

test("should return true for a valid CNPJ where the first verifier is 0", () => {
const cnpj = "69.228.768.0159-00";
const result = cnpjIsValid(cnpj);
const cnpj: string = "69.228.768.0159-00";
const result: ValidateFunctions = cnpjIsValid(cnpj);
expect(result.isValid).toBe(true);
expect(result.errorMsg).toBeNull();
});
Expand Down
35 changes: 18 additions & 17 deletions tests/src/cpfValidator.test.ts
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);
});
});
Loading

0 comments on commit 2a98c5c

Please sign in to comment.