|
1 |
| -import cnpjIsValid from '../../src/cnpjValidator'; |
2 |
| - |
3 |
| -describe('cnpjIsValid function', () => { |
4 |
| - test('should validate a valid CNPJ', () => { |
5 |
| - const result = cnpjIsValid('72.501.263/0001-40'); |
6 |
| - expect(result.isValid).toBe(false); |
7 |
| - expect(result.errorMsg).toBe("CNPJ is not valid"); |
8 |
| - }); |
9 |
| - |
10 |
| - test('should invalidate an invalid CNPJ', () => { |
11 |
| - const result = cnpjIsValid('12.345.678/0001-91'); |
12 |
| - expect(result.isValid).toBe(false); |
13 |
| - expect(result.errorMsg).toBe('CNPJ is not valid'); |
14 |
| - }); |
15 |
| - |
16 |
| - test('should invalidate a CNPJ with incorrect length', () => { |
17 |
| - const result = cnpjIsValid('1234567890123'); |
18 |
| - expect(result.isValid).toBe(false); |
19 |
| - expect(result.errorMsg).toBe('CNPJ must have 14 numerical digits'); |
20 |
| - }); |
21 |
| - |
22 |
| - test('should invalidate a CNPJ with non-digit characters', () => { |
23 |
| - const result = cnpjIsValid('72.501.263/0001-4A'); |
24 |
| - expect(result.isValid).toBe(false); |
25 |
| - expect(result.errorMsg).toBe('CNPJ is not valid'); |
26 |
| - }); |
27 |
| - |
28 |
| - test('should invalidate an empty CNPJ', () => { |
29 |
| - const result = cnpjIsValid(''); |
30 |
| - expect(result.isValid).toBe(false); |
31 |
| - expect(result.errorMsg).toBe('CNPJ invalid'); |
32 |
| - }); |
33 |
| - |
34 |
| - test('should throw an error if input is not a string', () => { |
35 |
| - expect(() => { |
36 |
| - cnpjIsValid(12345678901234 as any); |
37 |
| - }).toThrow('The input should be a string.'); |
38 |
| - }); |
39 |
| - |
40 |
| - test('should throw an error if errorMsg is not an array', () => { |
41 |
| - expect(() => { |
42 |
| - cnpjIsValid('72.501.263/0001-40', 'error message' as any); |
43 |
| - }).toThrow('Must be an Array'); |
44 |
| - }); |
45 |
| - |
46 |
| - test('should throw an error if errorMsg contains non-string values', () => { |
47 |
| - expect(() => { |
48 |
| - cnpjIsValid('72.501.263/0001-40', [123, 'error message'] as any); |
49 |
| - }).toThrow('All values within the array must be strings or null/undefined.'); |
50 |
| - }); |
51 |
| - |
52 |
| - test('should return custom error messages', () => { |
53 |
| - const result = cnpjIsValid('12.345.678/0001-91', ['Custom invalid message', 'Custom length message', 'Custom not valid message', 'Custom unknown error message']); |
54 |
| - expect(result.isValid).toBe(false); |
55 |
| - expect(result.errorMsg).toBe('Custom not valid message'); |
56 |
| - }); |
57 |
| - |
58 |
| - test('should return false when all digits are repeated', () => { |
59 |
| - const result = cnpjIsValid('11.111.111/1111-11'); |
| 1 | +import cnpjIsValid from "../../src/cnpjValidator"; |
| 2 | + |
| 3 | +describe("cnpjIsValid function", () => { |
| 4 | + test("should validate a valid CNPJ", () => { |
| 5 | + const result = cnpjIsValid("72.501.263/0001-40"); |
| 6 | + expect(result.isValid).toBe(false); |
| 7 | + expect(result.errorMsg).toBe("CNPJ is not valid"); |
| 8 | + }); |
| 9 | + |
| 10 | + test("should invalidate an invalid CNPJ", () => { |
| 11 | + const result = cnpjIsValid("12.345.678/0001-91"); |
| 12 | + expect(result.isValid).toBe(false); |
| 13 | + expect(result.errorMsg).toBe("CNPJ is not valid"); |
| 14 | + }); |
| 15 | + |
| 16 | + test("should invalidate a CNPJ with incorrect length", () => { |
| 17 | + const result = cnpjIsValid("1234567890123"); |
| 18 | + expect(result.isValid).toBe(false); |
| 19 | + expect(result.errorMsg).toBe("CNPJ must have 14 numerical digits"); |
| 20 | + }); |
| 21 | + |
| 22 | + test("should invalidate a CNPJ with non-digit characters", () => { |
| 23 | + const result = cnpjIsValid("72.501.263/0001-4A"); |
| 24 | + expect(result.isValid).toBe(false); |
| 25 | + expect(result.errorMsg).toBe("CNPJ is not valid"); |
| 26 | + }); |
| 27 | + |
| 28 | + test("should invalidate an empty CNPJ", () => { |
| 29 | + const result = cnpjIsValid(""); |
| 30 | + expect(result.isValid).toBe(false); |
| 31 | + expect(result.errorMsg).toBe("CNPJ invalid"); |
| 32 | + }); |
| 33 | + |
| 34 | + test("should throw an error if input is not a string", () => { |
| 35 | + expect(() => { |
| 36 | + cnpjIsValid(12345678901234 as any); |
| 37 | + }).toThrow("The input should be a string."); |
| 38 | + }); |
| 39 | + |
| 40 | + test("should throw an error if errorMsg is not an array", () => { |
| 41 | + expect(() => { |
| 42 | + cnpjIsValid("72.501.263/0001-40", "error message" as any); |
| 43 | + }).toThrow("Must be an Array"); |
| 44 | + }); |
| 45 | + |
| 46 | + test("should throw an error if errorMsg contains non-string values", () => { |
| 47 | + expect(() => { |
| 48 | + cnpjIsValid("72.501.263/0001-40", [123, "error message"] as any); |
| 49 | + }).toThrow( |
| 50 | + "All values within the array must be strings or null/undefined.", |
| 51 | + ); |
| 52 | + }); |
| 53 | + |
| 54 | + test("should return custom error messages", () => { |
| 55 | + const result = cnpjIsValid("12.345.678/0001-91", [ |
| 56 | + "Custom invalid message", |
| 57 | + "Custom length message", |
| 58 | + "Custom not valid message", |
| 59 | + "Custom unknown error message", |
| 60 | + ]); |
| 61 | + expect(result.isValid).toBe(false); |
| 62 | + expect(result.errorMsg).toBe("Custom not valid message"); |
| 63 | + }); |
| 64 | + |
| 65 | + test("should return false when all digits are repeated", () => { |
| 66 | + const result = cnpjIsValid("11.111.111/1111-11"); |
60 | 67 | expect(result.isValid).toBe(false);
|
61 |
| - expect(result.errorMsg).toBe('CNPJ is not valid'); |
| 68 | + expect(result.errorMsg).toBe("CNPJ is not valid"); |
62 | 69 | });
|
63 | 70 | });
|
0 commit comments