@@ -4,13 +4,14 @@ import {
44 cleanup ,
55 waitForElementToBeRemoved as _waitForElementToBeRemoved ,
66} from "@testing-library/vue" ;
7+ import { vMaska } from "maska/vue" ;
78import { afterEach , beforeEach , describe , expect , it , vi } from "vitest" ;
89import { VcInput , VcButton , VcInputDetails , VcLabel } from "@/ui-kit/components" ;
910import BankCardForm from "./bank-card-form.vue" ;
1011import type { RenderResult } from "@testing-library/vue" ;
11- import "@testing-library/jest-dom/vitest" ;
1212import type { DirectiveBinding } from "vue" ;
1313import type { ComponentProps } from "vue-component-type-helpers" ;
14+ import "@testing-library/jest-dom/vitest" ;
1415
1516vi . mock ( "vue-i18n" , ( ) => {
1617 return {
@@ -21,6 +22,7 @@ vi.mock("vue-i18n", () => {
2122 } ;
2223} ) ;
2324
25+ const CARD_NUMBER_LABEL = "shared.payment.bank_card_form.number_label" ;
2426const EXPIRATION_FIELD_LABEL = "shared.payment.bank_card_form.expiration_date_label" ;
2527const ERROR_MESSAGES = {
2628 MONTH : "shared.payment.bank_card_form.errors.month" ,
@@ -37,6 +39,9 @@ function queryElementByText(text: string) {
3739function getExpirationInput ( ) {
3840 return renderedComponent . getByLabelText < HTMLInputElement > ( EXPIRATION_FIELD_LABEL ) ;
3941}
42+ function getCardNumberInput ( ) {
43+ return renderedComponent . getByLabelText < HTMLInputElement > ( CARD_NUMBER_LABEL ) ;
44+ }
4045
4146async function waitForElementToBeRemoved ( selector : string ) {
4247 return await _waitForElementToBeRemoved ( ( ) => renderedComponent . queryByText ( selector ) ) ;
@@ -72,6 +77,7 @@ describe("BankCardForm", () => {
7277 VcTooltip : true ,
7378 } ,
7479 directives : {
80+ maska : vMaska ,
7581 "html-safe" : {
7682 mounted ( el : HTMLElement , binding : DirectiveBinding < string > ) {
7783 el . textContent = binding . value ;
@@ -93,15 +99,41 @@ describe("BankCardForm", () => {
9399 cleanup ( ) ;
94100 } ) ;
95101
96- // describe other fields and general form cases
102+ describe ( "Card Number Field" , ( ) => {
103+ it ( "should format input with spaces every 4 digits" , async ( ) => {
104+ const input = getCardNumberInput ( ) ;
105+
106+ await fireEvent . update ( input , "1111" ) ;
107+ expect ( input . value ) . toBe ( "1111" ) ;
108+
109+ await fireEvent . update ( input , "11111" ) ;
110+ expect ( input . value ) . toBe ( "1111 1" ) ;
111+
112+ await fireEvent . update ( input , "111111111111" ) ;
113+ expect ( input . value ) . toBe ( "1111 1111 1111" ) ;
114+ } ) ;
115+
116+ it ( "should not allow non-numeric characters" , async ( ) => {
117+ const input = getCardNumberInput ( ) ;
118+
119+ await fireEvent . update ( input , "123a" ) ;
120+ expect ( input . value ) . toBe ( "123" ) ;
121+
122+ await fireEvent . update ( input , "123/" ) ;
123+ expect ( input . value ) . toBe ( "123" ) ;
124+ } ) ;
125+ } ) ;
97126
98127 describe ( "Expiration Date Field" , ( ) => {
99128 describe ( "Formatting" , ( ) => {
100129 it ( "should format input as MM/YY while typing" , async ( ) => {
101130 const input = getExpirationInput ( ) ;
102131
103132 await fireEvent . update ( input , "12" ) ;
104- expect ( input . value ) . toBe ( "12 / " ) ;
133+ expect ( input . value ) . toBe ( "12" ) ;
134+
135+ await fireEvent . update ( input , "122" ) ;
136+ expect ( input . value ) . toBe ( "12 / 2" ) ;
105137
106138 await fireEvent . update ( input , "1223" ) ;
107139 expect ( input . value ) . toBe ( "12 / 23" ) ;
@@ -124,10 +156,11 @@ describe("BankCardForm", () => {
124156 expect ( input . value ) . toBe ( "1" ) ;
125157 } ) ;
126158
127- it ( "should limit input to 4 digits" , async ( ) => {
159+ it ( "should limit input to 4 digits (MMYY -> MM / YY) " , async ( ) => {
128160 const input = getExpirationInput ( ) ;
129161
130162 await fireEvent . update ( input , "123456" ) ;
163+ // 1234 -> 12 / 34. 56 ignored due to mask length
131164 expect ( input . value ) . toBe ( "12 / 34" ) ;
132165 } ) ;
133166 } ) ;
@@ -167,7 +200,7 @@ describe("BankCardForm", () => {
167200 expect ( await findElementByText ( ERROR_MESSAGES . YEAR_INCOMPLETE ) ) . toBeInTheDocument ( ) ;
168201
169202 // Month and partial year
170- await fireEvent . update ( input , "12/ 2" ) ;
203+ await fireEvent . update ( input , "12 / 2" ) ;
171204 expect ( await findElementByText ( ERROR_MESSAGES . YEAR_INCOMPLETE ) ) . toBeInTheDocument ( ) ;
172205 } ) ;
173206 } ) ;
0 commit comments