1- import { format , LENGTH , isValid , generate , RESERVED_NUMBERS } from '.' ;
1+ import {
2+ format ,
3+ LENGTH ,
4+ isValid ,
5+ generate ,
6+ generateAlphanumeric ,
7+ isAlphanumericCnpj ,
8+ isNumericCnpj ,
9+ cleanCnpj ,
10+ charToCnpjValue ,
11+ isValidFormat ,
12+ isValidNumericFormat ,
13+ RESERVED_NUMBERS ,
14+ } from '.' ;
215
316describe ( 'format' , ( ) => {
417 test ( 'should format cnpj with mask' , ( ) => {
@@ -76,7 +89,19 @@ describe('format', () => {
7689 } ) ;
7790
7891 test ( 'should remove all non numeric characters' , ( ) => {
79- expect ( format ( '46.?ABC843.485/0001-86abc' ) ) . toBe ( '46.843.485/0001-86' ) ;
92+ expect ( format ( '46.?ABC843.485/0001-86abc' ) ) . toBe ( '46.ABC.843/4850-00' ) ;
93+ } ) ;
94+
95+ // Novos testes para CNPJ alfanumérico
96+ test ( 'should format alphanumeric cnpj with mask' , ( ) => {
97+ expect ( format ( 'AB1C2D3E4F5G6' ) ) . toBe ( 'AB.1C2.D3E/4F5G-6' ) ;
98+ expect ( format ( '12ABC34501DE35' ) ) . toBe ( '12.ABC.345/01DE-35' ) ;
99+ expect ( format ( 'ABCDEFGHIJKL35' ) ) . toBe ( 'AB.CDE.FGH/IJKL-35' ) ;
100+ } ) ;
101+
102+ test ( 'should format alphanumeric cnpj with special characters' , ( ) => {
103+ expect ( format ( 'AB.?1C2.D3E/4F5G-35abc' ) ) . toBe ( 'AB.1C2.D3E/4F5G-35' ) ;
104+ expect ( format ( '12.ABC.345/01DE-35' ) ) . toBe ( '12.ABC.345/01DE-35' ) ;
80105 } ) ;
81106} ) ;
82107
@@ -93,6 +118,113 @@ describe('generate', () => {
93118 } ) ;
94119} ) ;
95120
121+ describe ( 'generateAlphanumeric' , ( ) => {
122+ test ( `should have the right length without mask (${ LENGTH } )` , ( ) => {
123+ expect ( generateAlphanumeric ( ) . length ) . toBe ( LENGTH ) ;
124+ } ) ;
125+
126+ test ( 'should return valid alphanumeric CNPJ' , ( ) => {
127+ // iterate over 100 to insure that random generated alphanumeric CNPJ is valid
128+ for ( let i = 0 ; i < 100 ; i ++ ) {
129+ const cnpj = generateAlphanumeric ( ) ;
130+ expect ( isValid ( cnpj ) ) . toBe ( true ) ;
131+ expect ( isAlphanumericCnpj ( cnpj ) ) . toBe ( true ) ;
132+ }
133+ } ) ;
134+
135+ test ( 'should contain alphanumeric characters' , ( ) => {
136+ const cnpj = generateAlphanumeric ( ) ;
137+ expect ( / [ A - Z ] / . test ( cnpj ) ) . toBe ( true ) ;
138+ expect ( / [ 0 - 9 ] / . test ( cnpj ) ) . toBe ( true ) ;
139+ } ) ;
140+ } ) ;
141+
142+ describe ( 'charToCnpjValue' , ( ) => {
143+ test ( 'should convert characters to numeric values (ASCII - 48)' , ( ) => {
144+ expect ( charToCnpjValue ( 'A' ) ) . toBe ( 17 ) ; // 65 - 48
145+ expect ( charToCnpjValue ( 'B' ) ) . toBe ( 18 ) ; // 66 - 48
146+ expect ( charToCnpjValue ( 'C' ) ) . toBe ( 19 ) ; // 67 - 48
147+ expect ( charToCnpjValue ( '0' ) ) . toBe ( 0 ) ; // 48 - 48
148+ expect ( charToCnpjValue ( '1' ) ) . toBe ( 1 ) ; // 49 - 48
149+ expect ( charToCnpjValue ( '9' ) ) . toBe ( 9 ) ; // 57 - 48
150+ expect ( charToCnpjValue ( 'Z' ) ) . toBe ( 42 ) ; // 90 - 48
151+ } ) ;
152+ } ) ;
153+
154+ describe ( 'cleanCnpj' , ( ) => {
155+ test ( 'should remove special characters and convert to uppercase' , ( ) => {
156+ expect ( cleanCnpj ( '12.ABC.345/01DE-35' ) ) . toBe ( '12ABC34501DE35' ) ;
157+ expect ( cleanCnpj ( '12.345.678/0001-95' ) ) . toBe ( '12345678000195' ) ;
158+ expect ( cleanCnpj ( 'ab.cde.fgh/ijkl-35' ) ) . toBe ( 'ABCDEFGHIJKL35' ) ;
159+ expect ( cleanCnpj ( '12.?ABC.345/01DE-35abc' ) ) . toBe ( '12ABC34501DE35ABC' ) ;
160+ } ) ;
161+ } ) ;
162+
163+ describe ( 'isNumericCnpj' , ( ) => {
164+ test ( 'should return true for numeric CNPJs' , ( ) => {
165+ expect ( isNumericCnpj ( '12345678000195' ) ) . toBe ( true ) ;
166+ expect ( isNumericCnpj ( '12.345.678/0001-95' ) ) . toBe ( true ) ;
167+ expect ( isNumericCnpj ( '00000000000000' ) ) . toBe ( true ) ;
168+ } ) ;
169+
170+ test ( 'should return false for alphanumeric CNPJs' , ( ) => {
171+ expect ( isNumericCnpj ( '12ABC34501DE35' ) ) . toBe ( false ) ;
172+ expect ( isNumericCnpj ( 'AB.1C2.D3E/4F5G-35' ) ) . toBe ( false ) ;
173+ expect ( isNumericCnpj ( 'ABCDEFGHIJKL35' ) ) . toBe ( false ) ;
174+ } ) ;
175+ } ) ;
176+
177+ describe ( 'isAlphanumericCnpj' , ( ) => {
178+ test ( 'should return true for alphanumeric CNPJs' , ( ) => {
179+ expect ( isAlphanumericCnpj ( '12ABC34501DE35' ) ) . toBe ( true ) ;
180+ expect ( isAlphanumericCnpj ( 'AB.1C2.D3E/4F5G-35' ) ) . toBe ( true ) ;
181+ expect ( isAlphanumericCnpj ( 'ABCDEFGHIJKL35' ) ) . toBe ( true ) ;
182+ } ) ;
183+
184+ test ( 'should return false for numeric CNPJs' , ( ) => {
185+ expect ( isAlphanumericCnpj ( '12345678000195' ) ) . toBe ( false ) ;
186+ expect ( isAlphanumericCnpj ( '12.345.678/0001-95' ) ) . toBe ( false ) ;
187+ expect ( isAlphanumericCnpj ( '00000000000000' ) ) . toBe ( false ) ;
188+ } ) ;
189+
190+ test ( 'should return false for invalid lengths' , ( ) => {
191+ expect ( isAlphanumericCnpj ( 'ABC' ) ) . toBe ( false ) ;
192+ expect ( isAlphanumericCnpj ( 'ABCDEFGHIJKLMNOP' ) ) . toBe ( false ) ;
193+ } ) ;
194+ } ) ;
195+
196+ describe ( 'isValidFormat' , ( ) => {
197+ test ( 'should return true for valid alphanumeric formats' , ( ) => {
198+ expect ( isValidFormat ( '12.ABC.345/01DE-35' ) ) . toBe ( true ) ;
199+ expect ( isValidFormat ( 'AB.1C2.D3E/4F5G-35' ) ) . toBe ( true ) ;
200+ expect ( isValidFormat ( '12ABC34501DE35' ) ) . toBe ( true ) ;
201+ expect ( isValidFormat ( 'AB1C2D3E4F5G35' ) ) . toBe ( true ) ;
202+ } ) ;
203+
204+ test ( 'should return true for valid numeric formats' , ( ) => {
205+ expect ( isValidFormat ( '12.345.678/0001-95' ) ) . toBe ( true ) ;
206+ expect ( isValidFormat ( '12345678000195' ) ) . toBe ( true ) ;
207+ } ) ;
208+
209+ test ( 'should return false for invalid formats' , ( ) => {
210+ expect ( isValidFormat ( '12.ABC.345/01DE-99' ) ) . toBe ( true ) ; // Actually valid format, just invalid DV
211+ expect ( isValidFormat ( 'AB.1C2.D3E/4F5G-3' ) ) . toBe ( false ) ; // Too short
212+ expect ( isValidFormat ( 'AB.1C2.D3E/4F5G-356' ) ) . toBe ( false ) ; // Too long
213+ } ) ;
214+ } ) ;
215+
216+ describe ( 'isValidNumericFormat' , ( ) => {
217+ test ( 'should return true for valid numeric formats' , ( ) => {
218+ expect ( isValidNumericFormat ( '12.345.678/0001-95' ) ) . toBe ( true ) ;
219+ expect ( isValidNumericFormat ( '12345678000195' ) ) . toBe ( true ) ;
220+ } ) ;
221+
222+ test ( 'should return false for alphanumeric formats' , ( ) => {
223+ expect ( isValidNumericFormat ( '12.ABC.345/01DE-35' ) ) . toBe ( false ) ;
224+ expect ( isValidNumericFormat ( 'AB.1C2.D3E/4F5G-35' ) ) . toBe ( false ) ;
225+ } ) ;
226+ } ) ;
227+
96228describe ( 'isValid' , ( ) => {
97229 describe ( 'should return false' , ( ) => {
98230 test ( 'when it is on the RESERVED_NUMBERS' , ( ) => {
@@ -139,6 +271,13 @@ describe('isValid', () => {
139271 test ( 'when is a CNPJ invalid' , ( ) => {
140272 expect ( isValid ( '11257245286531' ) ) . toBe ( false ) ;
141273 } ) ;
274+
275+ // Novos testes para CNPJ alfanumérico inválido
276+ test ( 'when is an invalid alphanumeric CNPJ' , ( ) => {
277+ expect ( isValid ( '12.ABC.345/01DE-99' ) ) . toBe ( false ) ; // Invalid DV
278+ expect ( isValid ( 'AB.1C2.D3E/4F5G-3' ) ) . toBe ( false ) ; // Too short
279+ expect ( isValid ( 'AB.1C2.D3E/4F5G-356' ) ) . toBe ( false ) ; // Too long
280+ } ) ;
142281 } ) ;
143282
144283 describe ( 'should return true' , ( ) => {
@@ -149,5 +288,20 @@ describe('isValid', () => {
149288 test ( 'when is a CNPJ valid with mask' , ( ) => {
150289 expect ( isValid ( '60.391.947/0001-00' ) ) . toBe ( true ) ;
151290 } ) ;
291+
292+ // Novos testes para CNPJ alfanumérico válido
293+ test ( 'when is a valid alphanumeric CNPJ' , ( ) => {
294+ // Estes testes precisam de CNPJs alfanuméricos válidos gerados pela função
295+ const alphanumericCnpj = generateAlphanumeric ( ) ;
296+ expect ( isValid ( alphanumericCnpj ) ) . toBe ( true ) ;
297+ expect ( isAlphanumericCnpj ( alphanumericCnpj ) ) . toBe ( true ) ;
298+ } ) ;
299+
300+ test ( 'when is a valid alphanumeric CNPJ with mask' , ( ) => {
301+ const alphanumericCnpj = generateAlphanumeric ( ) ;
302+ const formattedCnpj = format ( alphanumericCnpj ) ;
303+ expect ( isValid ( formattedCnpj ) ) . toBe ( true ) ;
304+ expect ( isAlphanumericCnpj ( formattedCnpj ) ) . toBe ( true ) ;
305+ } ) ;
152306 } ) ;
153307} ) ;
0 commit comments