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 ,
9
10
} ;
10
11
11
- const renderDatePicker = ( props ?: Partial < DatepickerProps > ) =>
12
+ const renderDatePicker = ( props ?: Partial < DatepickerProviderProps > ) =>
12
13
render ( < Datepicker { ...defaultProps } { ...props } /> ) ;
13
14
14
15
describe ( '<Datepicker />' , ( ) => {
@@ -23,14 +24,40 @@ describe('<Datepicker />', () => {
23
24
24
25
it ( 'contains a single DateInput component' , ( ) => {
25
26
renderDatePicker ( ) ;
26
- expect ( screen . getByRole ( 'textbox ' ) ) . toBeInTheDocument ( ) ;
27
+ expect ( screen . getByRole ( 'group ' ) ) . toBeInTheDocument ( ) ;
27
28
} ) ;
28
29
29
30
it ( 'does not contain a Calendar component' , ( ) => {
30
31
renderDatePicker ( ) ;
31
32
expect ( screen . queryByRole ( 'dialog' ) ) . not . toBeInTheDocument ( ) ;
32
33
} ) ;
33
34
35
+ it ( 'responds to arrow up and down' , async ( ) => {
36
+ renderDatePicker ( ) ;
37
+ const [ dayInput ] = screen . getAllByRole ( 'spinbutton' ) ;
38
+ await userEvent . type ( dayInput , '{arrowup}' ) ;
39
+ expect ( dayInput ) . toHaveValue ( 1 ) ;
40
+ await userEvent . type ( dayInput , '{arrowdown}' ) ;
41
+ expect ( dayInput ) . toHaveValue ( 31 ) ;
42
+ } ) ;
43
+
44
+ it ( 'triggers onchange when arrows are used' , async ( ) => {
45
+ const onChange = jest . fn ( ) ;
46
+ renderDatePicker ( { onChange } ) ;
47
+ const [ dayInput ] = screen . getAllByRole ( 'spinbutton' ) ;
48
+ await userEvent . type ( dayInput , '{arrowup}' ) ;
49
+ expect ( onChange ) . toHaveBeenCalledTimes ( 1 ) ;
50
+ } ) ;
51
+
52
+ it ( 'reponds to arrow left and right' , async ( ) => {
53
+ renderDatePicker ( ) ;
54
+ const [ dayInput , monthInput ] = screen . getAllByRole ( 'spinbutton' ) ;
55
+ await userEvent . type ( dayInput , '{arrowright}' ) ;
56
+ expect ( monthInput ) . toHaveFocus ( ) ;
57
+ await userEvent . type ( monthInput , '{arrowleft}' ) ;
58
+ expect ( dayInput ) . toHaveFocus ( ) ;
59
+ } ) ;
60
+
34
61
describe ( 'with click on button' , ( ) => {
35
62
const user = userEvent . setup ( ) ;
36
63
it ( 'contains a Calendar' , async ( ) => {
@@ -54,8 +81,8 @@ describe('<Datepicker />', () => {
54
81
it ( 'calls onChange method' , async ( ) => {
55
82
const onChange = jest . fn ( ) ;
56
83
renderDatePicker ( { onChange } ) ;
57
- const input = screen . getByRole ( 'textbox ') ;
58
- await user . type ( input , '1 ' ) ;
84
+ const [ dayInput ] = screen . getAllByRole ( 'spinbutton ') ;
85
+ await user . type ( dayInput , '4 ' ) ;
59
86
expect ( onChange ) . toHaveBeenCalledTimes ( 1 ) ;
60
87
} ) ;
61
88
} ) ;
@@ -81,34 +108,24 @@ describe('<Datepicker />', () => {
81
108
describe ( 'ariaInvalid' , ( ) => {
82
109
it ( 'has correct aria-invalid value if given prop' , ( ) => {
83
110
renderDatePicker ( { ariaInvalid : true } ) ;
84
- const input = screen . getByRole ( 'textbox' ) ;
85
- expect ( input . getAttribute ( 'aria-invalid' ) ) . toBe ( 'true' ) ;
86
- } ) ;
87
-
88
- it ( 'has correct aria-describedby if aria-describedby given as input prop' , ( ) => {
89
- const inputProps = {
90
- 'aria-describedby' : 'test' ,
91
- } ;
92
- renderDatePicker ( {
93
- ariaInvalid : true ,
94
- inputProps,
95
- } ) ;
96
- const input = screen . getByRole ( 'textbox' ) ;
97
- expect ( input . getAttribute ( 'aria-describedby' ) ) . toBe ( 'test' ) ;
111
+ const [ date , month , year ] =
112
+ screen . getAllByRole ( 'spinbutton' ) ;
113
+ expect ( date . getAttribute ( 'aria-invalid' ) ) . toBe ( 'true' ) ;
114
+ expect ( month . getAttribute ( 'aria-invalid' ) ) . toBe ( 'true' ) ;
115
+ expect ( year . getAttribute ( 'aria-invalid' ) ) . toBe ( 'true' ) ;
98
116
} ) ;
99
- } ) ;
100
117
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 ' ) ;
111
- } ) ;
118
+ // it('has correct aria-describedby if aria-describedby given as input prop ', () => {
119
+ // const inputProps = {
120
+ // 'aria-describedby': 'test',
121
+ // };
122
+ // renderDatePicker({
123
+ // ariaInvalid: true,
124
+ // inputProps,
125
+ // } );
126
+ // const input = screen.getByRole('textbox' );
127
+ // expect(input.getAttribute('aria-describedby ')).toBe('test ');
128
+ // });
112
129
} ) ;
113
130
114
131
describe ( 'fullWidth' , ( ) => {
@@ -126,43 +143,14 @@ describe('<Datepicker />', () => {
126
143
} ) ;
127
144
} ) ;
128
145
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}' ) ;
146
+ describe ( 'with value' , ( ) => {
147
+ it ( 'has correct value in input field' , ( ) => {
148
+ renderDatePicker ( { value : '01.01.2021' } ) ;
164
149
165
- expect ( onChange ) . toHaveBeenCalledWith ( '12.12.2020' ) ;
150
+ const [ date , month , year ] = screen . getAllByRole ( 'spinbutton' ) ;
151
+ expect ( date ) . toHaveValue ( 1 ) ;
152
+ expect ( month ) . toHaveValue ( 1 ) ;
153
+ expect ( year ) . toHaveValue ( 2021 ) ;
166
154
} ) ;
167
155
} ) ;
168
156
} ) ;
0 commit comments