11import cnpjIsValid from "../../src/cnpjValidator" ;
2+ import type { ValidateFunctions } from "../../src/types" ;
23
34describe ( "cnpjIsValid function" , ( ) => {
45 test ( "should validate a valid CNPJ" , ( ) => {
5- const result = cnpjIsValid ( "72.501.263/0001-40" ) ;
6+ const result : ValidateFunctions = cnpjIsValid ( "72.501.263/0001-40" ) ;
67 expect ( result . isValid ) . toBe ( false ) ;
78 expect ( result . errorMsg ) . toBe ( "CNPJ is not valid" ) ;
89 } ) ;
910
1011 test ( "should invalidate an invalid CNPJ" , ( ) => {
11- const result = cnpjIsValid ( "12.345.678/0001-91" ) ;
12+ const result : ValidateFunctions = cnpjIsValid ( "12.345.678/0001-91" ) ;
1213 expect ( result . isValid ) . toBe ( false ) ;
1314 expect ( result . errorMsg ) . toBe ( "CNPJ is not valid" ) ;
1415 } ) ;
1516
1617 test ( "should invalidate a CNPJ with non-digit characters" , ( ) => {
17- const result = cnpjIsValid ( "72.501.263/0001-4A" ) ;
18+ const result : ValidateFunctions = cnpjIsValid ( "72.501.263/0001-4A" ) ;
1819 expect ( result . isValid ) . toBe ( false ) ;
1920 expect ( result . errorMsg ) . toBe ( "CNPJ is not valid" ) ;
2021 } ) ;
2122
2223 test ( "should return false if cnpj length is not 14 or 18" , ( ) => {
23- const result = cnpjIsValid ( "123456789012" ) ;
24+ const result : ValidateFunctions = cnpjIsValid ( "123456789012" ) ;
2425 expect ( result . isValid ) . toBe ( false ) ;
2526 expect ( result . errorMsg ) . toBe ( "CNPJ must have 14 numerical digits" ) ;
2627 } ) ;
2728
2829 test ( "should return false if cnpj has valid length but invalid verifier digits" , ( ) => {
29- const result = cnpjIsValid ( "12.345.678/0001-00" ) ;
30+ const result : ValidateFunctions = cnpjIsValid ( "12.345.678/0001-00" ) ;
3031 expect ( result . isValid ) . toBe ( false ) ;
3132 expect ( result . errorMsg ) . toBe ( "CNPJ is not valid" ) ;
3233 } ) ;
3334
3435 test ( "should invalidate an empty CNPJ" , ( ) => {
35- const result = cnpjIsValid ( "" ) ;
36+ const result : ValidateFunctions = cnpjIsValid ( "" ) ;
3637 expect ( result . isValid ) . toBe ( false ) ;
3738 expect ( result . errorMsg ) . toBe ( "CNPJ invalid" ) ;
3839 } ) ;
3940
4041 test ( "should throw an error if input is not a string" , ( ) => {
4142 expect ( ( ) => {
42- cnpjIsValid ( 12345678901234 as any ) ;
43+ cnpjIsValid ( 12345678901234 as unknown as string ) ;
4344 } ) . toThrow ( "The input should be a string." ) ;
4445 } ) ;
4546
4647 test ( "should throw an error if errorMsg is not an array" , ( ) => {
4748 expect ( ( ) => {
48- cnpjIsValid ( "72.501.263/0001-40" , "error message" as any ) ;
49+ cnpjIsValid ( "72.501.263/0001-40" , "error message" as unknown as string [ ] ) ;
4950 } ) . toThrow ( "Must be an Array" ) ;
5051 } ) ;
5152
5253 test ( "should throw an error if errorMsg contains non-string values" , ( ) => {
5354 expect ( ( ) => {
54- cnpjIsValid ( "72.501.263/0001-40" , [ 123 , "error message" ] as any ) ;
55+ cnpjIsValid ( "72.501.263/0001-40" , [
56+ 123 ,
57+ "error message" ,
58+ ] as unknown as string [ ] ) ;
5559 } ) . toThrow (
5660 "All values within the array must be strings or null/undefined." ,
5761 ) ;
5862 } ) ;
5963
6064 test ( "should return custom error messages" , ( ) => {
61- const result = cnpjIsValid ( "12.345.678/0001-91" , [
65+ const result : ValidateFunctions = cnpjIsValid ( "12.345.678/0001-91" , [
6266 "Custom invalid message" ,
6367 "Custom length message" ,
6468 "Custom not valid message" ,
@@ -69,30 +73,33 @@ describe("cnpjIsValid function", () => {
6973 } ) ;
7074
7175 test ( "should return false when all digits are repeated" , ( ) => {
72- const result = cnpjIsValid ( "11.111.111/1111-11" ) ;
76+ const result : ValidateFunctions = cnpjIsValid ( "11.111.111/1111-11" ) ;
7377 expect ( result . isValid ) . toBe ( false ) ;
7478 expect ( result . errorMsg ) . toBe ( "CNPJ is not valid" ) ;
7579 } ) ;
7680
7781 test ( "should return false when all digits are the same" , ( ) => {
78- const result = cnpjIsValid ( "11.111.111/1111-11" ) ;
82+ const result : ValidateFunctions = cnpjIsValid ( "11.111.111/1111-11" ) ;
7983 expect ( result . isValid ) . toBe ( false ) ;
8084 expect ( result . errorMsg ) . toBe ( "CNPJ is not valid" ) ;
8185 } ) ;
8286
8387 test ( "should return default error messages when errorMsg['etc', null] is passed" , ( ) => {
84- const result = cnpjIsValid ( "12.345.678/0001-91" , [ "etc" , null ] ) ;
88+ const result : ValidateFunctions = cnpjIsValid ( "12.345.678/0001-91" , [
89+ "etc" ,
90+ null ,
91+ ] ) ;
8592 expect ( result . errorMsg ) . toBe ( "CNPJ is not valid" ) ;
8693 } ) ;
8794
8895 test ( "should return default error messages when errorMsg is null" , ( ) => {
89- const result = cnpjIsValid ( "12.345.678/0001-91" , null ) ;
96+ const result : ValidateFunctions = cnpjIsValid ( "12.345.678/0001-91" , null ) ;
9097 expect ( result . errorMsg ) . toBe ( "CNPJ is not valid" ) ;
9198 } ) ;
9299
93100 test ( "should return true for a valid CNPJ where the first verifier is 0" , ( ) => {
94- const cnpj = "69.228.768.0159-00" ;
95- const result = cnpjIsValid ( cnpj ) ;
101+ const cnpj : string = "69.228.768.0159-00" ;
102+ const result : ValidateFunctions = cnpjIsValid ( cnpj ) ;
96103 expect ( result . isValid ) . toBe ( true ) ;
97104 expect ( result . errorMsg ) . toBeNull ( ) ;
98105 } ) ;
0 commit comments