@@ -4,7 +4,6 @@ import { useState } from 'react';
44
55import DateField from '.' ;
66
7- // Mock i18n hook (only label optional usage); return label directly
87jest . mock ( 'react-i18next' , ( ) => ( {
98 useTranslation : ( ) => ( {
109 t : ( key : string , options ?: Record < string , string > ) => options ?. label ?? key ,
@@ -29,16 +28,17 @@ const Controlled = (props: React.ComponentProps<typeof DateField>) => {
2928
3029describe ( 'DateField Component' , ( ) => {
3130 test ( 'render US format placeholders and separator' , ( ) => {
32- const { container } = render ( < DateField dateFormat = { SupportedDateFormat . US } label = "dob" /> ) ;
31+ const { container } = render (
32+ < DateField dateFormat = { SupportedDateFormat . US } label = "Date of birth" />
33+ ) ;
3334
34- // Focus wrapper to activate inputs
35- const wrapper = container . querySelector ( '[role="button"]' ) ;
36- expect ( wrapper ) . not . toBeNull ( ) ;
37- if ( wrapper ) {
38- act ( ( ) => {
39- fireEvent . click ( wrapper ) ;
40- } ) ;
41- }
35+ const labelNode = Array . from ( container . querySelectorAll ( '*' ) ) . find (
36+ ( element ) => element . textContent === 'Date of birth'
37+ ) ;
38+ expect ( labelNode ) . toBeTruthy ( ) ;
39+ act ( ( ) => {
40+ fireEvent . click ( labelNode ! ) ;
41+ } ) ;
4242
4343 const inputs = Array . from ( container . querySelectorAll ( 'input' ) ) ;
4444 expect ( inputs ) . toHaveLength ( 3 ) ;
@@ -56,14 +56,18 @@ describe('DateField Component', () => {
5656 test ( 'typing fills each part and produces final date (controlled, 2023-08-20)' , ( ) => {
5757 const handleChange = jest . fn ( ) ;
5858 const { container } = render (
59- < Controlled dateFormat = { SupportedDateFormat . US } onChange = { handleChange } />
59+ < Controlled
60+ dateFormat = { SupportedDateFormat . US }
61+ label = "Date of birth"
62+ onChange = { handleChange }
63+ />
6064 ) ;
61- const wrapper = container . querySelector ( '[role="button"]' ) ;
62- if ( wrapper ) {
63- act ( ( ) => {
64- fireEvent . click ( wrapper ) ;
65- } ) ;
66- }
65+ const labelNode = Array . from ( container . querySelectorAll ( '*' ) ) . find (
66+ ( element ) => element . textContent === 'Date of birth'
67+ ) ;
68+ act ( ( ) => {
69+ fireEvent . click ( labelNode ! ) ;
70+ } ) ;
6771 const inputs = Array . from ( container . querySelectorAll ( 'input' ) ) ;
6872
6973 act ( ( ) => {
@@ -80,14 +84,19 @@ describe('DateField Component', () => {
8084 test ( 'backspace clears previous field when empty' , ( ) => {
8185 const handleChange = jest . fn ( ) ;
8286 const { container } = render (
83- < Controlled dateFormat = { SupportedDateFormat . US } value = "08/20/2023" onChange = { handleChange } />
87+ < Controlled
88+ dateFormat = { SupportedDateFormat . US }
89+ value = "08/20/2023"
90+ label = "Date of birth"
91+ onChange = { handleChange }
92+ />
8493 ) ;
85- const wrapper = container . querySelector ( '[role="button"]' ) ;
86- if ( wrapper ) {
87- act ( ( ) => {
88- fireEvent . click ( wrapper ) ;
89- } ) ;
90- }
94+ const labelNode = Array . from ( container . querySelectorAll ( '*' ) ) . find (
95+ ( element ) => element . textContent === 'Date of birth'
96+ ) ;
97+ act ( ( ) => {
98+ fireEvent . click ( labelNode ! ) ;
99+ } ) ;
91100 const inputs = Array . from ( container . querySelectorAll ( 'input' ) ) ;
92101
93102 // Focus last input and clear it with backspace until it moves to previous
@@ -116,14 +125,18 @@ describe('DateField Component', () => {
116125 test ( 'paste distributes digits across inputs' , ( ) => {
117126 const handleChange = jest . fn ( ) ;
118127 const { container } = render (
119- < DateField dateFormat = { SupportedDateFormat . US } onChange = { handleChange } />
128+ < DateField
129+ dateFormat = { SupportedDateFormat . US }
130+ label = "Date of birth"
131+ onChange = { handleChange }
132+ />
120133 ) ;
121- const wrapper = container . querySelector ( '[role="button"]' ) ;
122- if ( wrapper ) {
123- act ( ( ) => {
124- fireEvent . click ( wrapper ) ;
125- } ) ;
126- }
134+ const labelNode = Array . from ( container . querySelectorAll ( '*' ) ) . find (
135+ ( element ) => element . textContent === 'Date of birth'
136+ ) ;
137+ act ( ( ) => {
138+ fireEvent . click ( labelNode ! ) ;
139+ } ) ;
127140 const inputs = Array . from ( container . querySelectorAll ( 'input' ) ) ;
128141
129142 act ( ( ) => {
@@ -155,14 +168,18 @@ describe('DateField Component', () => {
155168 test ( 'UK format placeholders and value assembly' , ( ) => {
156169 const handleChange = jest . fn ( ) ;
157170 const { container } = render (
158- < Controlled dateFormat = { SupportedDateFormat . UK } onChange = { handleChange } />
171+ < Controlled
172+ dateFormat = { SupportedDateFormat . UK }
173+ label = "Date of birth"
174+ onChange = { handleChange }
175+ />
159176 ) ;
160- const wrapper = container . querySelector ( '[role="button"]' ) ;
161- if ( wrapper ) {
162- act ( ( ) => {
163- fireEvent . click ( wrapper ) ;
164- } ) ;
165- }
177+ const labelNode = Array . from ( container . querySelectorAll ( '*' ) ) . find (
178+ ( element ) => element . textContent === 'Date of birth'
179+ ) ;
180+ act ( ( ) => {
181+ fireEvent . click ( labelNode ! ) ;
182+ } ) ;
166183 const inputs = Array . from ( container . querySelectorAll ( 'input' ) ) ;
167184 expect ( inputs [ 0 ] ?. getAttribute ( 'placeholder' ) ) . toBe ( 'DD' ) ;
168185 expect ( inputs [ 1 ] ?. getAttribute ( 'placeholder' ) ) . toBe ( 'MM' ) ;
@@ -178,14 +195,18 @@ describe('DateField Component', () => {
178195 test ( 'ISO format placeholders, separator and value assembly' , ( ) => {
179196 const handleChange = jest . fn ( ) ;
180197 const { container } = render (
181- < Controlled dateFormat = { SupportedDateFormat . ISO } onChange = { handleChange } />
198+ < Controlled
199+ dateFormat = { SupportedDateFormat . ISO }
200+ label = "Date of birth"
201+ onChange = { handleChange }
202+ />
182203 ) ;
183- const wrapper = container . querySelector ( '[role="button"]' ) ;
184- if ( wrapper ) {
185- act ( ( ) => {
186- fireEvent . click ( wrapper ) ;
187- } ) ;
188- }
204+ const labelNode = Array . from ( container . querySelectorAll ( '*' ) ) . find (
205+ ( element ) => element . textContent === 'Date of birth'
206+ ) ;
207+ act ( ( ) => {
208+ fireEvent . click ( labelNode ! ) ;
209+ } ) ;
189210 const inputs = Array . from ( container . querySelectorAll ( 'input' ) ) ;
190211 expect ( inputs [ 0 ] ?. getAttribute ( 'placeholder' ) ) . toBe ( 'YYYY' ) ;
191212 expect ( inputs [ 1 ] ?. getAttribute ( 'placeholder' ) ) . toBe ( 'MM' ) ;
0 commit comments