-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy pathDatePicker.test.tsx
347 lines (291 loc) Β· 11.3 KB
/
DatePicker.test.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
import { mount, shallow, ShallowWrapper, ReactWrapper } from 'enzyme';
import * as dayjs from 'dayjs';
import * as React from 'react';
import * as sinon from 'sinon';
import { DatePickerDefaults } from '../src/common/Constant';
import Calendar from '../src/components/Calendar';
import DatePicker, { Props, State, TabValue } from '../src/components/DatePicker';
const PICKER_INPUT_CLASS = '.picker-input__text';
describe('<DatePicker/>', () => {
let shallowComponent: ShallowWrapper<React.Component<Props, State>>;
let mountComponent: ReactWrapper<Props, State>;
const defaultProps = {
initialDate: dayjs(new Date(2018, 11, 1)),
};
const pickerShow = (component: ReactWrapper) => {
component.find('Picker').setState({
show: true,
});
};
const daySelect = (at: number) => {
mountComponent
.find('td')
.at(at)
.simulate('click');
};
describe('shallow render test', () => {
beforeEach(() => {
shallowComponent = shallow(<DatePicker {...defaultProps} dateFormat="YYYYMMDD" />);
});
it('renders with no props', () => {
expect(shallowComponent).toBeTruthy();
});
it('should includeTime & showTimeOnly cannot be used together', () => {
expect(() => {
shallowComponent = shallow(<DatePicker includeTime showTimeOnly />);
}).toThrowError(Error);
});
describe('dateFormat', () => {
it('should includeTime dateFormat correctly', () => {
const shallowDatePicker = shallow<DatePicker>(<DatePicker {...defaultProps} includeTime />);
expect(shallowDatePicker.instance().getDateFormat()).toBe(
DatePickerDefaults.dateTimeFormat
);
});
it('should calendarOnly dateFormat correctly', () => {
const shallowDatePicker = shallow<DatePicker>(<DatePicker {...defaultProps} />);
expect(shallowDatePicker.instance().getDateFormat()).toBe(DatePickerDefaults.dateFormat);
});
it('should timeOnly dateFormat correctly', () => {
const shallowDatePicker = shallow<DatePicker>(
<DatePicker {...defaultProps} showTimeOnly />
);
expect(shallowDatePicker.instance().getDateFormat()).toBe(DatePickerDefaults.timeFormat);
});
});
});
describe('mount test', () => {
let onChange: sinon.SinonSpy;
beforeEach(() => {
onChange = sinon.spy();
mountComponent = mount(
<DatePicker {...defaultProps} onChange={onChange} dateFormat="YYYY/MM/DD" />
);
pickerShow(mountComponent);
});
it('should showTimeonly render only timeContainer', () => {
mountComponent = mount(<DatePicker showTimeOnly />);
pickerShow(mountComponent);
expect(mountComponent.find('.picker__container__timeonly')).toHaveLength(1);
});
it('should input ref correctly', () => {
mountComponent.find('.picker__trigger').simulate('click');
expect(mountComponent.find('Calendar')).toHaveLength(1);
});
it('should props dateFormat correctly', () => {
daySelect(7);
expect(mountComponent.find('.picker__trigger input').prop('value')).toEqual('2018/12/02');
});
it('should props onChange correctly', () => {
daySelect(6);
expect(onChange).toHaveProperty('callCount', 1);
expect(mountComponent.state('calendarShow')).toBeFalsy();
expect(mountComponent.state('selected')).toHaveLength(1);
});
it('should props showMonthCnt correctly', () => {
// 20180501
const mockDate = dayjs(new Date(2018, 5, 1));
mountComponent = mount(<DatePicker initialDate={mockDate} showMonthCnt={3} />);
pickerShow(mountComponent);
expect(mountComponent.find('.calendar__container')).toHaveLength(3);
});
it('should input interaction correctly', () => {
mountComponent.find('.picker__trigger').simulate('click');
expect(mountComponent.find('.rc-backdrop')).toHaveLength(1);
expect(mountComponent.find(Calendar)).toBeTruthy();
expect(mountComponent.find('Picker').state('show')).toBeTruthy();
});
it('should hideCalendar correctly', () => {
mountComponent.find('.picker__trigger').simulate('click');
mountComponent.find('.rc-backdrop').simulate('click');
expect(mountComponent.find('Picker').state('show')).toBeFalsy();
});
});
describe('include time', () => {
beforeEach(() => {
mountComponent = mount(<DatePicker {...defaultProps} includeTime />);
});
it('should time container render correctly', () => {
expect(mountComponent.find('.time__container')).toBeTruthy();
});
it('should tab TIME click correctly', () => {
mountComponent.setState({
...mountComponent.state,
tabValue: TabValue.DATE,
});
pickerShow(mountComponent);
mountComponent
.find('.picker__container__tab button')
.at(1)
.simulate('click');
expect(mountComponent.state('tabValue')).toEqual(TabValue.TIME);
});
describe('handleTimeChange', () => {
const handleClick = (type: string, at: number) => {
mountComponent
.find(`.time-input__${type} button`)
.at(at)
.simulate('click');
};
beforeEach(() => {
mountComponent = mount(<DatePicker includeTime />);
mountComponent.setState({
tabValue: TabValue.TIME,
});
pickerShow(mountComponent);
});
it('should set new date when state date is null', () => {
mountComponent = mount(<DatePicker includeTime />);
mountComponent.setState({
tabValue: TabValue.TIME,
});
pickerShow(mountComponent);
handleClick('up', 0);
const date = mountComponent.state().date;
expect(date).toBeDefined();
if (date) {
expect(date.format(DatePickerDefaults.dateFormat)).toBe(
dayjs().format(DatePickerDefaults.dateFormat)
);
}
});
it('should time change set date & input value correctly', () => {
mountComponent = mount(<DatePicker initialDate={dayjs('201904032320')} includeTime />);
mountComponent.setState({
tabValue: TabValue.TIME,
});
pickerShow(mountComponent);
handleClick('up', 0);
const date = mountComponent.state().date;
if (date) {
expect(date.format('YYYYMMDDHHmm')).toBe('201904032320');
}
});
it('should fire onChange Event', () => {
const onChange = sinon.spy();
mountComponent = mount(<DatePicker {...defaultProps} onChange={onChange} includeTime />);
mountComponent.setState({
tabValue: TabValue.TIME,
});
pickerShow(mountComponent);
handleClick('up', 0);
expect(onChange).toHaveProperty('callCount', 1);
});
});
});
describe('handleInputBlur', () => {
beforeEach(() => {
mountComponent = mount(<DatePicker {...defaultProps} />);
});
it('should date picker input invalid value return original date', () => {
const { dateFormat } = DatePickerDefaults;
const originalDate = dayjs(new Date(2018, 4, 1));
const testValue = 'teste333';
mountComponent.setState({
...mountComponent.state,
date: originalDate,
inputValue: testValue,
});
mountComponent.find(PICKER_INPUT_CLASS).simulate('blur');
const dateState = mountComponent.state('date');
expect(dateState).not.toBeUndefined();
if (dateState) {
expect(dayjs(dateState).format(dateFormat)).toEqual(dayjs(originalDate).format(dateFormat));
}
});
it('should date picker input valid value setState date', () => {
const { dateFormat } = DatePickerDefaults;
const originalDate = dayjs(new Date(2018, 4, 1));
const correctValue = '2018-05-02';
mountComponent.setState({
...mountComponent.state,
date: originalDate,
inputValue: correctValue,
});
mountComponent.find(PICKER_INPUT_CLASS).simulate('blur');
const dateState = mountComponent.state('date');
expect(dateState).not.toBeUndefined();
if (dateState) {
expect(dayjs(dateState).format(dateFormat)).toEqual(correctValue);
}
});
it('should time picker input invalid value return original time', () => {
mountComponent = mount(<DatePicker {...defaultProps} includeTime />);
const originalDate = dayjs('201904031022');
mountComponent.setState({
...mountComponent.state,
date: originalDate,
inputValue: `${dayjs(originalDate).format(DatePickerDefaults.dateFormat)} 03:e0A`,
});
mountComponent.find(PICKER_INPUT_CLASS).simulate('blur');
const inputValue = mountComponent.state('inputValue');
expect(inputValue).toBe(originalDate.format(DatePickerDefaults.dateTimeFormat));
});
});
describe('handleInputClear', () => {
beforeEach(() => {
mountComponent = mount(<DatePicker {...defaultProps} clear />);
});
it('should clear button render correctly', () => {
expect(mountComponent.find('.icon-clear').first()).toHaveLength(1);
});
it('should clear click state change correctly', () => {
mountComponent
.find('.icon-clear')
.first()
.simulate('click');
expect(mountComponent.state('inputValue')).toEqual('');
});
it('should clear click onChange fired!', () => {
const onChange = sinon.spy();
mountComponent = mount(<DatePicker {...defaultProps} onChange={onChange} clear />);
mountComponent
.find('.icon-clear')
.first()
.simulate('click');
expect(onChange).toHaveProperty('callCount', 1);
});
});
describe('handleInputChange', () => {
it('should input change correctly', () => {
const onChange = sinon.spy();
mountComponent = mount(<DatePicker {...defaultProps} onChange={onChange} />);
mountComponent.find(PICKER_INPUT_CLASS).simulate('change');
expect(onChange).toHaveProperty('callCount', 1);
mountComponent = mount(<DatePicker {...defaultProps} />);
mountComponent.find(PICKER_INPUT_CLASS).simulate('change');
});
});
describe('input props', () => {
it('should input disabled do not run handleCalendar', () => {
mountComponent = mount(<DatePicker {...defaultProps} disabled />);
mountComponent.find(PICKER_INPUT_CLASS).simulate('click');
expect(mountComponent.state('show')).toBeFalsy();
});
it('should showDefaultIcon correctly', () => {
mountComponent = mount(<DatePicker {...defaultProps} showDefaultIcon />);
expect(mountComponent.find('.icon-calendar').first()).toHaveLength(1);
});
});
describe('custom input test', () => {
beforeEach(() => {
const customInputComponent = (props: any) => <textarea value={props.value} readOnly />;
mountComponent = mount(
<DatePicker
{...defaultProps}
dateFormat="YYYY.MM.DD"
inputComponent={customInputComponent}
/>
);
pickerShow(mountComponent);
});
it('should customInput render correctly', () => {
expect(mountComponent.find('.picker__trigger textarea')).toBeTruthy();
});
it('should customInput value correctly', () => {
daySelect(6);
expect(mountComponent.find('.picker__trigger textarea').props().value).toBeDefined();
expect(mountComponent.find('.picker__trigger textarea').props().value).toEqual('2018.12.01');
});
});
});