1
1
import React from 'react' ;
2
- import { Datepicker , DatepickerProps } from './Datepicker' ;
2
+ import { Datepicker , DatepickerProviderProps } from './Datepicker' ;
3
3
import { render , screen } from '@testing-library/react' ;
4
4
import userEvent from '@testing-library/user-event' ;
5
5
6
6
const defaultProps = {
7
7
value : '' ,
8
8
onChange : ( ) => { } ,
9
+ locale : 'nb' as const ,
10
+ labelId : 'datepicker-label' ,
9
11
} ;
10
12
11
- const renderDatePicker = ( props ?: Partial < DatepickerProps > ) =>
13
+ const renderDatePicker = ( props ?: Partial < DatepickerProviderProps > ) =>
12
14
render ( < Datepicker { ...defaultProps } { ...props } /> ) ;
13
15
14
16
describe ( '<Datepicker />' , ( ) => {
@@ -23,14 +25,40 @@ describe('<Datepicker />', () => {
23
25
24
26
it ( 'contains a single DateInput component' , ( ) => {
25
27
renderDatePicker ( ) ;
26
- expect ( screen . getByRole ( 'textbox ' ) ) . toBeInTheDocument ( ) ;
28
+ expect ( screen . getByRole ( 'group ' ) ) . toBeInTheDocument ( ) ;
27
29
} ) ;
28
30
29
31
it ( 'does not contain a Calendar component' , ( ) => {
30
32
renderDatePicker ( ) ;
31
33
expect ( screen . queryByRole ( 'dialog' ) ) . not . toBeInTheDocument ( ) ;
32
34
} ) ;
33
35
36
+ it ( 'responds to arrow up and down' , async ( ) => {
37
+ renderDatePicker ( ) ;
38
+ const [ dayInput ] = screen . getAllByRole ( 'spinbutton' ) ;
39
+ await userEvent . type ( dayInput , '{arrowup}' ) ;
40
+ expect ( dayInput ) . toHaveValue ( 1 ) ;
41
+ await userEvent . type ( dayInput , '{arrowdown}' ) ;
42
+ expect ( dayInput ) . toHaveValue ( 31 ) ;
43
+ } ) ;
44
+
45
+ it ( 'triggers onchange when arrows are used' , async ( ) => {
46
+ const onChange = jest . fn ( ) ;
47
+ renderDatePicker ( { onChange } ) ;
48
+ const [ dayInput ] = screen . getAllByRole ( 'spinbutton' ) ;
49
+ await userEvent . type ( dayInput , '{arrowup}' ) ;
50
+ expect ( onChange ) . toHaveBeenCalledTimes ( 1 ) ;
51
+ } ) ;
52
+
53
+ it ( 'reponds to arrow left and right' , async ( ) => {
54
+ renderDatePicker ( ) ;
55
+ const [ dayInput , monthInput ] = screen . getAllByRole ( 'spinbutton' ) ;
56
+ await userEvent . type ( dayInput , '{arrowright}' ) ;
57
+ expect ( monthInput ) . toHaveFocus ( ) ;
58
+ await userEvent . type ( monthInput , '{arrowleft}' ) ;
59
+ expect ( dayInput ) . toHaveFocus ( ) ;
60
+ } ) ;
61
+
34
62
describe ( 'with click on button' , ( ) => {
35
63
const user = userEvent . setup ( ) ;
36
64
it ( 'contains a Calendar' , async ( ) => {
@@ -54,8 +82,8 @@ describe('<Datepicker />', () => {
54
82
it ( 'calls onChange method' , async ( ) => {
55
83
const onChange = jest . fn ( ) ;
56
84
renderDatePicker ( { onChange } ) ;
57
- const input = screen . getByRole ( 'textbox ') ;
58
- await user . type ( input , '1 ' ) ;
85
+ const [ dayInput ] = screen . getAllByRole ( 'spinbutton ') ;
86
+ await user . type ( dayInput , '4 ' ) ;
59
87
expect ( onChange ) . toHaveBeenCalledTimes ( 1 ) ;
60
88
} ) ;
61
89
} ) ;
@@ -81,33 +109,24 @@ describe('<Datepicker />', () => {
81
109
describe ( 'ariaInvalid' , ( ) => {
82
110
it ( 'has correct aria-invalid value if given prop' , ( ) => {
83
111
renderDatePicker ( { ariaInvalid : true } ) ;
84
- const input = screen . getByRole ( 'textbox' ) ;
85
- expect ( input . getAttribute ( 'aria-invalid' ) ) . toBe ( 'true' ) ;
112
+ const [ date , month , year ] =
113
+ screen . getAllByRole ( 'spinbutton' ) ;
114
+ expect ( date . getAttribute ( 'aria-invalid' ) ) . toBe ( 'true' ) ;
115
+ expect ( month . getAttribute ( 'aria-invalid' ) ) . toBe ( 'true' ) ;
116
+ expect ( year . getAttribute ( 'aria-invalid' ) ) . toBe ( 'true' ) ;
86
117
} ) ;
87
118
88
119
it ( 'has correct aria-describedby if aria-describedby given as input prop' , ( ) => {
89
- const inputProps = {
90
- 'aria-describedby' : 'test' ,
91
- } ;
92
120
renderDatePicker ( {
93
121
ariaInvalid : true ,
94
- inputProps ,
122
+ ariaDescribedby : 'test' ,
95
123
} ) ;
96
- const input = screen . getByRole ( 'textbox' ) ;
97
- expect ( input . getAttribute ( 'aria-describedby' ) ) . toBe ( 'test' ) ;
98
- } ) ;
99
- } ) ;
100
124
101
- describe ( 'inputProps' , ( ) => {
102
- it ( 'is passed on to input field' , ( ) => {
103
- const inputProps = {
104
- className : 'customClass' ,
105
- id : 'custom-input-id' ,
106
- } ;
107
- renderDatePicker ( { inputProps } ) ;
108
- const input = screen . getByRole ( 'textbox' ) ;
109
- expect ( input . classList . contains ( 'customClass' ) ) . toBe ( true ) ;
110
- expect ( input . getAttribute ( 'id' ) ) . toBe ( 'custom-input-id' ) ;
125
+ const [ date , month , year ] =
126
+ screen . getAllByRole ( 'spinbutton' ) ;
127
+ expect ( date . getAttribute ( 'aria-describedby' ) ) . toBe ( 'test' ) ;
128
+ expect ( month . getAttribute ( 'aria-describedby' ) ) . toBe ( 'test' ) ;
129
+ expect ( year . getAttribute ( 'aria-describedby' ) ) . toBe ( 'test' ) ;
111
130
} ) ;
112
131
} ) ;
113
132
@@ -126,43 +145,14 @@ describe('<Datepicker />', () => {
126
145
} ) ;
127
146
} ) ;
128
147
129
- describe ( 'try to be smart in which century to place an input of two digit years' , ( ) => {
130
- const user = userEvent . setup ( ) ;
131
- it ( 'defaults to the 20th century' , async ( ) => {
132
- const onChange = jest . fn ( ) ;
133
- renderDatePicker ( { onChange, value : '101099' } ) ;
134
-
135
- const input = screen . getByRole ( 'textbox' ) ;
136
- await user . type ( input , '{Tab}' ) ;
137
- expect ( onChange ) . toHaveBeenCalledWith ( '10.10.2099' ) ;
138
- } ) ;
139
-
140
- it ( 'assumes last century if maxDate is today-ish' , async ( ) => {
141
- const onChange = jest . fn ( ) ;
142
- renderDatePicker ( {
143
- maxDate : '02.02.2022' ,
144
- onChange,
145
- value : '111199' ,
146
- } ) ;
147
-
148
- const input = screen . getByRole ( 'textbox' ) ;
149
- await user . type ( input , '{Tab}' ) ;
150
-
151
- expect ( onChange ) . toHaveBeenCalledWith ( '11.11.1999' ) ;
152
- } ) ;
153
-
154
- it ( 'assumes this century if maxDate is today-ish but input is rather close to it' , async ( ) => {
155
- const onChange = jest . fn ( ) ;
156
- renderDatePicker ( {
157
- maxDate : '02.02.2022' ,
158
- onChange,
159
- value : '121220' ,
160
- } ) ;
161
-
162
- const input = screen . getByRole ( 'textbox' ) ;
163
- await user . type ( input , '{Tab}' ) ;
148
+ describe ( 'with value' , ( ) => {
149
+ it ( 'has correct value in input field' , ( ) => {
150
+ renderDatePicker ( { value : '01.01.2021' } ) ;
164
151
165
- expect ( onChange ) . toHaveBeenCalledWith ( '12.12.2020' ) ;
152
+ const [ date , month , year ] = screen . getAllByRole ( 'spinbutton' ) ;
153
+ expect ( date ) . toHaveValue ( 1 ) ;
154
+ expect ( month ) . toHaveValue ( 1 ) ;
155
+ expect ( year ) . toHaveValue ( 2021 ) ;
166
156
} ) ;
167
157
} ) ;
168
158
} ) ;
0 commit comments