Skip to content

Commit

Permalink
refactor: update prettier command in package.json and pr-check yml
Browse files Browse the repository at this point in the history
  • Loading branch information
gabriel-logan committed Oct 20, 2024
1 parent fb276ab commit dbeced5
Show file tree
Hide file tree
Showing 15 changed files with 110 additions and 57 deletions.
6 changes: 4 additions & 2 deletions .github/workflows/typescript-pr-check.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,11 @@ jobs:

- name: Install dependencies
run: yarn install
- name: Run Prettier
run: yarn prettier
- name: Run lint
run: yarn lint
- name: Run tests
run: yarn pr-check
- name: Run test
run: yarn test
- name: Run build
run: yarn build
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
"build:browser": "webpack",
"build": "yarn build:types && yarn build:browser",
"lint": "eslint . --ext .ts",
"prettier": "prettier --write .",
"prettier": "prettier --write '**/*.{ts,js}'",
"pr-check": "yarn prettier && yarn lint && yarn build && yarn test"
},
"repository": {
Expand Down
4 changes: 3 additions & 1 deletion tests/src/cpfValidator.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,9 @@ describe("cpfIsValid", () => {
let result = cpfIsValid("");
expect(result.isValid).toBe(false);

expect(() => cpfIsValid(null as any)).toThrow("The input should be a string.");
expect(() => cpfIsValid(null as any)).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", () => {
Expand Down
8 changes: 4 additions & 4 deletions tests/src/getOnlyEmail.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,18 +93,18 @@ describe("getOnlyEmail", () => {
expect(result).toBe("[email protected]");
});

it('should clean the domain from the email when cleanDomain is true', () => {
it("should clean the domain from the email when cleanDomain is true", () => {
const result = getOnlyEmail(
"Entre em contato com a equipe: [email protected]",
{ cleanDomain: true }
{ cleanDomain: true },
);
expect(result).toBe("[email protected]");
});

it('should clean the domain from the email using a custom domain list', () => {
it("should clean the domain from the email using a custom domain list", () => {
const result = getOnlyEmail(
"Entre em contato com a equipe: [email protected]",
{ cleanDomain: [".custom"] }
{ cleanDomain: [".custom"] },
);
expect(result).toBe("[email protected]");
});
Expand Down
4 changes: 3 additions & 1 deletion tests/src/isAscii.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ describe("isAscii", () => {
});

it("should return true when the input is an empty string", () => {
expect(() => isAscii(" ")).toThrow("Input value must not be an empty string.");
expect(() => isAscii(" ")).toThrow(
"Input value must not be an empty string.",
);
});

it("should return false when the input is not ASCII", () => {
Expand Down
8 changes: 6 additions & 2 deletions tests/src/isDate.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,12 @@ describe("isDate", () => {
});

it("should throw an error when the input is a empty string", () => {
expect(() => isDate("")).toThrow("Input value must not be an empty string.");
expect(() => isDate(" ")).toThrow("Input value must not be an empty string.");
expect(() => isDate("")).toThrow(
"Input value must not be an empty string.",
);
expect(() => isDate(" ")).toThrow(
"Input value must not be an empty string.",
);
});

it("should return false if isNaN or !(dateObject instanceof Date)", () => {
Expand Down
8 changes: 6 additions & 2 deletions tests/src/isDecimal.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,9 @@ describe("isDecimal", () => {
});

it("should throw error when the input is a function", () => {
function func() { /* document why this function 'func' is empty */ }
function func() {
/* document why this function 'func' is empty */
}

expect(() => isDecimal(func() as any) as any).toThrow(errorToThrow);
});
Expand Down Expand Up @@ -191,7 +193,9 @@ describe("isDecimal", () => {
});

it("should throw error when the input is a function", () => {
function func() { /* document why this function 'func' is empty */ }
function func() {
/* document why this function 'func' is empty */
}

expect(() => isDecimal(func as any)).toThrow(
"Input value must be a string or a number.",
Expand Down
46 changes: 35 additions & 11 deletions tests/src/isValidVideo.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -171,32 +171,56 @@ describe("isValidVideo", () => {
expect(result1).toBe(false);
});

test('validateMp4 should return true for specific byte sequence', () => {
test("validateMp4 should return true for specific byte sequence", () => {
const fileBuffer = Buffer.from([
0x00, 0x00, 0x00, 0x18, // First 4 bytes
0x66, 0x74, 0x79, 0x70 // Next 4 bytes
0x00,
0x00,
0x00,
0x18, // First 4 bytes
0x66,
0x74,
0x79,
0x70, // Next 4 bytes
]);

const result = isValidVideo(fileBuffer);
expect(result).toBe(true);
});

test('validateMp4 should return true for byte sequence 1', () => {
test("validateMp4 should return true for byte sequence 1", () => {
const fileBuffer = Buffer.from([
0x00, 0x00, 0x00, 0x20, // First 4 bytes
0x66, 0x74, 0x79, 0x70, // Next 4 bytes
0x6d, 0x70, 0x34, 0x32 // Last 4 bytes
0x00,
0x00,
0x00,
0x20, // First 4 bytes
0x66,
0x74,
0x79,
0x70, // Next 4 bytes
0x6d,
0x70,
0x34,
0x32, // Last 4 bytes
]);

const result = isValidVideo(fileBuffer);
expect(result).toBe(true);
});

test('validateMp4 should return true for byte sequence 2', () => {
test("validateMp4 should return true for byte sequence 2", () => {
const fileBuffer = Buffer.from([
0x00, 0x00, 0x00, 0x1c, // First 4 bytes
0x66, 0x74, 0x79, 0x70, // Next 4 bytes
0x69, 0x73, 0x6f, 0x6d // Last 4 bytes
0x00,
0x00,
0x00,
0x1c, // First 4 bytes
0x66,
0x74,
0x79,
0x70, // Next 4 bytes
0x69,
0x73,
0x6f,
0x6d, // Last 4 bytes
]);

const result = isValidVideo(fileBuffer);
Expand Down
16 changes: 11 additions & 5 deletions tests/src/validateEmail.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,15 +88,21 @@ describe("validateEmail", () => {

it("should throw an error when errorMsg is not an array or null", () => {
// @ts-ignore
expect(() => validateEmail("[email protected]", { errorMsg: 123 })).toThrow("errorMsg must be an Array or null");
expect(() => validateEmail("[email protected]", { errorMsg: 123 })).toThrow(
"errorMsg must be an Array or null",
);
});

it("should throw an error if any element of the errorMsg array is different from string or null", () => {
expect(() => validateEmail("[email protected]", { errorMsg: [123 as any] })).toThrow("All values within the array must be strings or null/undefined.");
})
expect(() =>
validateEmail("[email protected]", { errorMsg: [123 as any] }),
).toThrow("All values within the array must be strings or null/undefined.");
});

it("should throw an error when maxLength must be a number and cannot be less than 1", () => {
expect(() => validateEmail("[email protected]", { maxLength: 0 })).toThrow("maxLength must be a number and cannot be less than 1");
expect(() => validateEmail("[email protected]", { maxLength: 0 })).toThrow(
"maxLength must be a number and cannot be less than 1",
);
});

it("should invalidate an email that does not end with the country code", () => {
Expand All @@ -110,4 +116,4 @@ describe("validateEmail", () => {
expect(result.isValid).toBe(true);
expect(result.errorMsg).toBe(null);
});
});
});
2 changes: 1 addition & 1 deletion tests/src/validateName.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ describe("validateName", () => {
const result: ValidateFunctions = validateName("");
expect(result.isValid).toBe(false);
expect(result.errorMsg).toBe("Name cannot be empty");
});
});

it("should return isValid as false for names with numbers", () => {
const result: ValidateFunctions = validateName("John123");
Expand Down
6 changes: 3 additions & 3 deletions tests/src/validatePhoneNumber.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@ describe("validatePhoneNumber", () => {
});

it("should throw an error when errorMsg is not an array or null", () => {
expect(() => validatePhoneNumber("(555) 123-4567", "Custom error" as any)).toThrow(
"errorMsg must be an Array or null",
);
expect(() =>
validatePhoneNumber("(555) 123-4567", "Custom error" as any),
).toThrow("errorMsg must be an Array or null");
});

it("should throw an error when the input is not a string", () => {
Expand Down
27 changes: 15 additions & 12 deletions tests/src/validateSurname.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,7 @@ describe("validateSurname", () => {
const result: ValidateFunctions = validateSurname("");
expect(result.isValid).toBe(false);
expect(result.errorMsg).toBe("Surname cannot be empty");
});

});

it("should return false if (/(\\w)\\1\\1/.test(surname)) is true", () => {
const result: ValidateFunctions = validateSurname("Johnnn");
Expand All @@ -37,18 +36,22 @@ describe("validateSurname", () => {
});

it("should throw an error if maxLength or minLength less than 1", () => {
expect(() => validateSurname("John", { minLength: 0, maxLength: 20 })).toThrow(
expect(() =>
validateSurname("John", { minLength: 0, maxLength: 20 }),
).toThrow(
"maxLength or minLength must be a number and cannot be less than 1",
);
expect(() => validateSurname("John", { minLength: 1, maxLength: 0 })).toThrow(
expect(() =>
validateSurname("John", { minLength: 1, maxLength: 0 }),
).toThrow(
"maxLength or minLength must be a number and cannot be less than 1",
);
});

it("should throw an error if minLength is greater than maxLength", () => {
expect(() => validateSurname("John", { minLength: 20, maxLength: 1 })).toThrow(
"minLength cannot be greater than maxLength",
);
expect(() =>
validateSurname("John", { minLength: 20, maxLength: 1 }),
).toThrow("minLength cannot be greater than maxLength");
});

it("returns error for surname with special characters", () => {
Expand Down Expand Up @@ -118,11 +121,11 @@ describe("validateSurname", () => {
});

it("should throw an error when errorMsg is not an array or null", () => {
expect(() => validateSurname("Johnson", {
errorMsg: "Custom error" as unknown as (string | null)[],
})).toThrow(
"errorMsg must be an Array or null",
);
expect(() =>
validateSurname("Johnson", {
errorMsg: "Custom error" as unknown as (string | null)[],
}),
).toThrow("errorMsg must be an Array or null");
});

it("should throw an error when the input is not a string", () => {
Expand Down
14 changes: 9 additions & 5 deletions tests/src/validateTextarea.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ import validateTextarea from "../../src/validateTextarea";

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

it("should throw an error when errorMsg is not an array", () => {
Expand All @@ -23,13 +23,17 @@ describe("validateTextarea", () => {
isRequired: true,
maxLength: -1,
}),
).toThrow("maxLength or minLength must be a number and cannot be less than 1");
).toThrow(
"maxLength or minLength must be a number and cannot be less than 1",
);
expect(() =>
validateTextarea("This is a valid textarea.", {
isRequired: true,
maxLength: "a" as any,
}),
).toThrow("maxLength or minLength must be a number and cannot be less than 1");
).toThrow(
"maxLength or minLength must be a number and cannot be less than 1",
);
});

it("validates textarea with correct length", () => {
Expand Down
6 changes: 3 additions & 3 deletions tests/src/validateUSPhoneNumber.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@ describe("validateUSPhoneNumber", () => {
});

it("should throw an error when errorMsg is not an array or null", () => {
expect(() => validateUSPhoneNumber("(555) 123-4567", "error msg" as any)).toThrow(
"errorMsg must be an Array or null",
);
expect(() =>
validateUSPhoneNumber("(555) 123-4567", "error msg" as any),
).toThrow("errorMsg must be an Array or null");
});

it("should throw an error when the input is not a string", () => {
Expand Down
10 changes: 6 additions & 4 deletions tests/src/validateUsername.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ import validateUsername from "../../src/validateUsername";

describe("validateUsername", () => {
it("should throw an error for non-string inputs", () => {
expect(() => validateUsername(123 as any)).toThrow("The input should be a string.");
expect(() => validateUsername(123 as any)).toThrow(
"The input should be a string.",
);
});
it("validates username with correct length", () => {
const result = validateUsername("User123", { minLength: 3, maxLength: 25 });
Expand All @@ -22,18 +24,18 @@ describe("validateUsername", () => {
it("should throw an error if maxLength or minlength is NaN", () => {
expect(() =>
validateUsername("User123", {
minLength: 'a' as any,
minLength: "a" as any,
maxLength: 25,
}),
).toThrow("maxLength or minLength must be a number");
expect(() =>
validateUsername("User123", {
minLength: 3,
maxLength: 'a' as any,
maxLength: "a" as any,
}),
).toThrow("maxLength or minLength must be a number");
});

it("should throw an error if minLength is greater than maxLength", () => {
expect(() =>
validateUsername("User123", {
Expand Down

0 comments on commit dbeced5

Please sign in to comment.