@@ -16,6 +16,7 @@ import {
1616 getYearAndMonthBasedOnFormat ,
1717 getMetaObject ,
1818 getDeviceModel ,
19+ formatInputFieldValue ,
1920} from '../../src/utils/helpers' ;
2021import {
2122 CardType ,
@@ -300,4 +301,223 @@ describe('test getYearAndMonthBasedOnFormat function', () => {
300301 year : '2032' ,
301302 } ) ;
302303 } ) ;
304+
305+ it ( 'should format expiration date options correctly' , ( ) => {
306+ const validFormats = [ 'MM/YY' , 'MM/YYYY' , 'YYYY/MM' , 'YY/MM' ] ;
307+ validFormats . forEach ( ( format ) => {
308+ const result = formatCollectElementOptions (
309+ ElementType . EXPIRATION_DATE ,
310+ { format } ,
311+ LogLevel . WARN
312+ ) ;
313+ expect ( result ) . toEqual ( {
314+ format,
315+ required : false ,
316+ } ) ;
317+ } ) ;
318+ } ) ;
319+
320+ it ( 'should use default format for invalid expiration date format' , ( ) => {
321+ const result = formatCollectElementOptions (
322+ ElementType . EXPIRATION_DATE ,
323+ { format : 'INVALID' } ,
324+ LogLevel . WARN
325+ ) ;
326+ expect ( result ) . toEqual ( {
327+ format : DEFAULT_EXPIRATION_DATE_FORMAT ,
328+ required : false ,
329+ } ) ;
330+ expect ( printLog ) . toBeCalled ( ) ;
331+ } ) ;
332+
333+ it ( 'should format expiration year options correctly' , ( ) => {
334+ const validFormats = [ 'YY' , 'YYYY' ] ;
335+ validFormats . forEach ( ( format ) => {
336+ const result = formatCollectElementOptions (
337+ ElementType . EXPIRATION_YEAR ,
338+ { format } ,
339+ LogLevel . WARN
340+ ) ;
341+ expect ( result ) . toEqual ( {
342+ format,
343+ required : false ,
344+ } ) ;
345+ } ) ;
346+ } ) ;
347+
348+ it ( 'should use default format for invalid expiration year format' , ( ) => {
349+ const result = formatCollectElementOptions (
350+ ElementType . EXPIRATION_YEAR ,
351+ { format : 'INVALID' } ,
352+ LogLevel . WARN
353+ ) ;
354+ expect ( result ) . toEqual ( {
355+ format : DEFAULT_EXPIRATION_YEAR_FORMAT ,
356+ required : false ,
357+ } ) ;
358+ expect ( printLog ) . toBeCalled ( ) ;
359+ } ) ;
360+ } ) ;
361+
362+ describe ( 'test formatCardNumber with different formats' , ( ) => {
363+ const testCases = [
364+ {
365+ input : '4111111111111111' ,
366+ type : CardType . VISA ,
367+ format : 'XXXX-XXXX-XXXX-XXXX' ,
368+ expected : '4111-1111-1111-1111' ,
369+ } ,
370+ {
371+ input : '378282246310005' ,
372+ type : CardType . AMEX ,
373+ format : 'XXXX-XXXX-XXXX-XXXX' ,
374+ expected : '3782-822463-10005' ,
375+ } ,
376+ {
377+ input : '6011111111111117' ,
378+ type : CardType . DISCOVER ,
379+ format : 'XXXX XXXX XXXX XXXX' ,
380+ expected : '6011 1111 1111 1117' ,
381+ } ,
382+ // Test partial inputs
383+ {
384+ input : '411111' ,
385+ type : CardType . VISA ,
386+ format : 'XXXX-XXXX-XXXX-XXXX' ,
387+ expected : '4111-11' ,
388+ } ,
389+ // Test empty input
390+ {
391+ input : '' ,
392+ type : CardType . DEFAULT ,
393+ format : 'XXXX-XXXX-XXXX-XXXX' ,
394+ expected : '' ,
395+ } ,
396+ ] ;
397+
398+ testCases . forEach ( ( testCase ) => {
399+ it ( `should format ${ testCase . type } card number correctly with format ${ testCase . format } ` , ( ) => {
400+ expect (
401+ formatCardNumber ( testCase . input , testCase . type , testCase . format )
402+ ) . toBe ( testCase . expected ) ;
403+ } ) ;
404+ } ) ;
405+ } ) ;
406+
407+ describe ( 'test formatInputFieldValue with different formats' , ( ) => {
408+ const testCases = [
409+ {
410+ input : '123456' ,
411+ format : 'XX-XX-XX' ,
412+ expected : '12-34-56' ,
413+ } ,
414+ {
415+ input : '123456' ,
416+ format : 'XXX XXX' ,
417+ expected : '123 456' ,
418+ } ,
419+ {
420+ input : '12345' ,
421+ format : 'XX/XX/X' ,
422+ expected : '12/34/5' ,
423+ } ,
424+ {
425+ input : '1567C89' ,
426+ format : 'XX/XYX/X' ,
427+ translation : { X : '[5-9]' , Y : '[A-C]' } ,
428+ expected : '56/7C8/9' ,
429+ } ,
430+ // Combination of constants and dynamic inputs
431+ {
432+ input : '1234121234' ,
433+ format : '+91 XXXX-XX-XXXX' ,
434+ expected : '+91 1234-12-1234' ,
435+ } ,
436+ {
437+ input : 'B5678978C7QAB97' ,
438+ format : '91 YXX-XZX-XXY-XZYXX' ,
439+ translation : { X : '[5-9]' , Y : '[A-C]' } ,
440+ expected : '91 B56-7Z8-97C-7ZA97' ,
441+ } ,
442+ // Test with special characters in format
443+ {
444+ input : '123456' ,
445+ format : 'XX@XX#XX' ,
446+ expected : '12@34#56' ,
447+ } ,
448+ // Test partial input
449+ {
450+ input : '123' ,
451+ format : 'XX-XX-XX' ,
452+ expected : '12-3' ,
453+ } ,
454+ // Test input longer than format
455+ {
456+ input : '1234567' ,
457+ format : 'XX-XX' ,
458+ expected : '12-34' ,
459+ } ,
460+ // Test empty input
461+ {
462+ input : '' ,
463+ format : 'XX-XX' ,
464+ expected : '' ,
465+ } ,
466+ // Test with no format
467+ {
468+ input : '123456' ,
469+ format : '' ,
470+ expected : '123456' ,
471+ } ,
472+ ] ;
473+
474+ testCases . forEach ( ( testCase ) => {
475+ const translation = testCase . translation || {
476+ X : '[0-9]' ,
477+ } ;
478+ it ( `should format input '${ testCase . input } ' with format '${
479+ testCase . format
480+ } ' & translation '${ JSON . stringify ( translation ) } ' correctly`, ( ) => {
481+ expect (
482+ formatInputFieldValue (
483+ testCase . input ,
484+ testCase . format ,
485+ testCase ?. translation
486+ )
487+ ) . toBe ( testCase . expected ) ;
488+ } ) ;
489+ } ) ;
490+ } ) ;
491+
492+ describe ( 'test formatCollectElementOptions for different element types' , ( ) => {
493+ it ( 'should format card number options correctly' , ( ) => {
494+ const options = {
495+ format : 'XXXX-XXXX-XXXX-XXXX' ,
496+ required : true ,
497+ } ;
498+ const result = formatCollectElementOptions (
499+ ElementType . CARD_NUMBER ,
500+ options ,
501+ LogLevel . WARN
502+ ) ;
503+ expect ( result ) . toEqual ( {
504+ format : 'XXXX-XXXX-XXXX-XXXX' ,
505+ required : true ,
506+ } ) ;
507+ } ) ;
508+
509+ it ( 'should preserve other options while formatting' , ( ) => {
510+ const options = {
511+ format : 'XXXX-XXXX-XXXX-XXXX' ,
512+ required : true ,
513+ enableCardIcon : true ,
514+ customOption : 'value' ,
515+ } ;
516+ const result = formatCollectElementOptions (
517+ ElementType . CARD_NUMBER ,
518+ options ,
519+ LogLevel . WARN
520+ ) ;
521+ expect ( result ) . toEqual ( options ) ;
522+ } ) ;
303523} ) ;
0 commit comments