-
-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathTimePicker.spec.js
305 lines (256 loc) · 12.2 KB
/
TimePicker.spec.js
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
/* eslint-env jest */
import React from 'react'
import { mount } from 'enzyme'
import MockDate from 'mockdate'
import * as testUtils from '../test/utils'
import TimePicker from './TimePicker'
describe('<TimePicker />', () => {
it('supports controlled mode', () => {
const tree = mount(<TimePicker value={new Date(2017, 10, 15, 13, 37, 0, 0)} mode='24h' />)
expect(tree.findWhere((e) => e.type() === 'div' && testUtils.hasClass(/^TimePicker-header-/)(e)).text()).toBe('13:37')
tree.setProps({ value: new Date(2017, 10, 15, 14, 42, 0, 0) })
expect(tree.findWhere((e) => e.type() === 'div' && testUtils.hasClass(/^TimePicker-header-/)(e)).text()).toBe('14:42')
})
describe('uncontrolled mode', () => {
afterEach(() => {
MockDate.reset()
})
it('supports uncontrolled mode with a default value', () => {
const tree = mount(<TimePicker defaultValue={new Date(2017, 10, 15, 13, 37, 0, 0)} mode='24h' />)
expect(tree.findWhere((e) => e.type() === 'div' && testUtils.hasClass(/^TimePicker-header-/)(e)).text()).toBe('13:37')
})
it('uses the current time if no value or default value is set', () => {
MockDate.set(new Date(2017, 10, 15, 13, 37, 0, 0))
const tree = mount(<TimePicker mode='24h' />)
expect(tree.findWhere((e) => e.type() === 'div' && testUtils.hasClass(/^TimePicker-header-/)(e)).text()).toBe('13:37')
})
})
describe('12h', () => {
it('matches the snapshot', () => {
const originalGetHours = Date.prototype.getHours
Date.prototype.getHours = Date.prototype.getUTCHours // eslint-disable-line
const tree = mount(
<TimePicker mode='12h' value={new Date('2017-10-15T13:37Z')} />
)
expect(tree).toMatchSnapshot()
Date.prototype.getHours = originalGetHours // eslint-disable-line
})
it('starts with the hour selection', () => {
const date = new Date(2017, 10, 15, 13, 37, 0, 0)
const tree = mount(
<TimePicker mode='12h' value={date} />
)
const clock = getClock(tree)
expect(clock.parent().props().mode).toBe('12h')
})
it('changes the value and the clock mode correctly', () => {
jest.useFakeTimers()
const changeHandler = jest.fn()
const date = new Date(2017, 10, 15, 13, 37, 0, 0)
const tree = mount(
<TimePicker mode='12h' value={date} onChange={changeHandler} />
)
// hour selection
getClock(tree).findWhere((e) => e.type() === 'div' && testUtils.hasClass(/^Clock-circle/)(e))
.simulate('click', testUtils.stubClickEvent(128, 30)) // click on 12
.simulate('mouseup', testUtils.stubClickEvent(128, 30)) // click on 12
expect(changeHandler).toBeCalled()
expect(changeHandler.mock.calls[0][0].getHours()).toBe(12)
expect(changeHandler.mock.calls[0][0].getMinutes()).toBe(37)
jest.runAllTimers()
tree.update()
expect(getClock(tree).props().mode).toBe('minutes')
// minute selection
changeHandler.mockClear()
getClock(tree).findWhere((e) => e.type() === 'div' && testUtils.hasClass(/^Clock-circle/)(e))
.simulate('click', testUtils.stubClickEvent(190, 230)) // click on 25
.simulate('mouseup', testUtils.stubClickEvent(190, 230)) // click on 25
expect(changeHandler).toBeCalled()
expect(changeHandler.mock.calls[0][0].getHours()).toBe(12)
expect(changeHandler.mock.calls[0][0].getMinutes()).toBe(25)
})
it('calls onMinutesSelected after selecting the minutes', () => {
jest.useFakeTimers()
const onMinutesSelected = jest.fn()
const tree = mount(
<TimePicker mode='12h' onMinutesSelected={onMinutesSelected} />
)
getClock(tree).findWhere((e) => e.type() === 'div' && testUtils.hasClass(/^Clock-circle/)(e))
.simulate('click', testUtils.stubClickEvent(128, 30)) // click on 12
.simulate('mouseup', testUtils.stubClickEvent(128, 30)) // click on 12
jest.runAllTimers()
tree.update()
getClock(tree).findWhere((e) => e.type() === 'div' && testUtils.hasClass(/^Clock-circle/)(e))
.simulate('click', testUtils.stubClickEvent(190, 230)) // click on 25
.simulate('mouseup', testUtils.stubClickEvent(190, 230)) // click on 25
jest.runAllTimers()
expect(onMinutesSelected).toHaveBeenCalledTimes(1)
})
it('can toggle between editing the hours and the minutes', () => {
const tree = mount(
<TimePicker mode='12h' value={new Date(2017, 10, 15, 13, 37, 0, 0)} />
)
tree.findWhere((e) => e != null && e.getDOMNode() != null && e.text() === '37')
.simulate('click') // click on minutes
expect(getClock(tree).props().mode).toBe('minutes')
tree.findWhere((e) => e != null && e.getDOMNode() != null && e.text() === '01')
.simulate('click') // click on hours
expect(getClock(tree).props().mode).toBe('12h')
})
it('supports toggling between am and pm', () => {
const changeHandler = jest.fn()
let tree = mount(
<TimePicker mode='12h' value={new Date(2017, 10, 15, 13, 37, 0, 0)} onChange={changeHandler} />
)
// 13:37 = 1:37 pm --> 01:37
tree.findWhere((e) => e != null && e.getDOMNode() != null && e.text() === 'AM')
.simulate('click')
expect(changeHandler).toBeCalled()
expect(changeHandler.mock.calls[0][0].getHours()).toBe(1)
expect(changeHandler.mock.calls[0][0].getMinutes()).toBe(37)
// 01:37 = 1:37 am --> 13:37
changeHandler.mockClear()
tree.findWhere((e) => e != null && e.getDOMNode() != null && e.text() === 'PM')
.simulate('click')
expect(changeHandler).toBeCalled()
expect(changeHandler.mock.calls[0][0].getHours()).toBe(13)
expect(changeHandler.mock.calls[0][0].getMinutes()).toBe(37)
tree = mount(
<TimePicker mode='12h' value={new Date(2017, 10, 15, 1, 37, 0, 0)} onChange={changeHandler} />
)
// 01:37 = 1:37 am --> 01:37
changeHandler.mockClear()
tree.findWhere((e) => e != null && e.getDOMNode() != null && e.text() === 'AM')
.simulate('click')
expect(changeHandler).not.toBeCalled()
tree = mount(
<TimePicker mode='12h' value={new Date(2017, 10, 15, 13, 37, 0, 0)} onChange={changeHandler} />
)
// 13:37 = 1:37 pm --> 13:37
changeHandler.mockClear()
tree.findWhere((e) => e != null && e.getDOMNode() != null && e.text() === 'PM')
.simulate('click')
expect(changeHandler).not.toBeCalled()
})
it('does not switch from am to pm when selecting 12 hours', () => {
const changeHandler = jest.fn()
let tree = mount(
<TimePicker mode='12h' value={new Date(2017, 10, 15, 1, 23, 0, 0)} onChange={changeHandler} />
)
getClock(tree).findWhere((e) => e.type() === 'div' && testUtils.hasClass(/^Clock-circle/)(e))
.simulate('click', testUtils.stubClickEvent(128, 30)) // click on 12
expect(changeHandler).toHaveBeenCalled()
const time = changeHandler.mock.calls[0][0]
expect(time.getHours()).toBe(0)
})
it('does not switch from am to pm when changing the hours', () => {
const changeHandler = jest.fn()
let tree = mount(
<TimePicker mode='12h' value={new Date(2017, 10, 15, 1, 23, 0, 0)} onChange={changeHandler} />
)
getClock(tree).findWhere((e) => e.type() === 'div' && testUtils.hasClass(/^Clock-circle/)(e))
.simulate('click', testUtils.stubClickEvent(250, 128 + 30)) // click on 3
expect(changeHandler).toHaveBeenCalled()
const time = changeHandler.mock.calls[0][0]
expect(time.getHours()).toBe(3)
})
it('does not switch from pm to am when selecting 12 hours', () => {
const changeHandler = jest.fn()
let tree = mount(
<TimePicker mode='12h' value={new Date(2017, 10, 15, 13, 37, 0, 0)} onChange={changeHandler} />
)
getClock(tree).findWhere((e) => e.type() === 'div' && testUtils.hasClass(/^Clock-circle/)(e))
.simulate('click', testUtils.stubClickEvent(128, 30)) // click on 12
expect(changeHandler).toHaveBeenCalled()
const time = changeHandler.mock.calls[0][0]
expect(time.getHours()).toBe(12)
})
it('does not switch from pm to am when changing the hours', () => {
const changeHandler = jest.fn()
let tree = mount(
<TimePicker mode='12h' value={new Date(2017, 10, 15, 13, 37, 0, 0)} onChange={changeHandler} />
)
getClock(tree).findWhere((e) => e.type() === 'div' && testUtils.hasClass(/^Clock-circle/)(e))
.simulate('click', testUtils.stubClickEvent(250, 128 + 30)) // click on 3
expect(changeHandler).toHaveBeenCalled()
const time = changeHandler.mock.calls[0][0]
expect(time.getHours()).toBe(15)
})
})
describe('24h', () => {
it('matches the snapshot', () => {
const originalGetHours = Date.prototype.getHours
Date.prototype.getHours = Date.prototype.getUTCHours // eslint-disable-line
const tree = mount(
<TimePicker mode='24h' value={new Date('2017-10-15T13:37Z')} />
)
expect(tree).toMatchSnapshot()
Date.prototype.getHours = originalGetHours // eslint-disable-line
})
it('starts with the hour selection', () => {
const date = new Date(2017, 10, 15, 13, 37, 0, 0)
const tree = mount(
<TimePicker mode='24h' value={date} />
)
const clock = getClock(tree)
expect(clock.parent().props().mode).toBe('24h')
})
it('changes the value and the clock mode correctly', () => {
jest.useFakeTimers()
const changeHandler = jest.fn()
const date = new Date(2017, 10, 15, 13, 37, 0, 0)
const tree = mount(
<TimePicker mode='24h' value={date} onChange={changeHandler} />
)
// hour selection
getClock(tree).findWhere((e) => e.type() === 'div' && testUtils.hasClass(/^Clock-circle/)(e))
.simulate('click', testUtils.stubClickEvent(128, 30)) // click on 12
.simulate('mouseup', testUtils.stubClickEvent(128, 30)) // click on 12
expect(changeHandler).toBeCalled()
expect(changeHandler.mock.calls[0][0].getHours()).toBe(12)
expect(changeHandler.mock.calls[0][0].getMinutes()).toBe(37)
jest.runAllTimers()
tree.update()
expect(getClock(tree).props().mode).toBe('minutes')
// minute selection
changeHandler.mockClear()
getClock(tree).findWhere((e) => e.type() === 'div' && testUtils.hasClass(/^Clock-circle/)(e))
.simulate('click', testUtils.stubClickEvent(190, 230)) // click on 25
.simulate('mouseup', testUtils.stubClickEvent(190, 230)) // click on 25
expect(changeHandler).toBeCalled()
expect(changeHandler.mock.calls[0][0].getHours()).toBe(12)
expect(changeHandler.mock.calls[0][0].getMinutes()).toBe(25)
})
it('calls onMinutesSelected after selecting the minutes', () => {
jest.useFakeTimers()
const onMinutesSelected = jest.fn()
const tree = mount(
<TimePicker mode='24h' onMinutesSelected={onMinutesSelected} />
)
getClock(tree).findWhere((e) => e.type() === 'div' && testUtils.hasClass(/^Clock-circle/)(e))
.simulate('click', testUtils.stubClickEvent(128, 30)) // click on 12
.simulate('mouseup', testUtils.stubClickEvent(128, 30)) // click on 12
jest.runAllTimers()
tree.update()
getClock(tree).findWhere((e) => e.type() === 'div' && testUtils.hasClass(/^Clock-circle/)(e))
.simulate('click', testUtils.stubClickEvent(190, 230)) // click on 25
.simulate('mouseup', testUtils.stubClickEvent(190, 230)) // click on 25
jest.runAllTimers()
expect(onMinutesSelected).toHaveBeenCalledTimes(1)
})
it('can toggle between editing the hours and the minutes', () => {
const tree = mount(
<TimePicker mode='24h' value={new Date(2017, 10, 15, 13, 37, 0, 0)} />
)
tree.findWhere((e) => e != null && e.getDOMNode() != null && e.text() === '37')
.simulate('click') // click on minutes
expect(getClock(tree).props().mode).toBe('minutes')
tree.findWhere((e) => e != null && e.getDOMNode() != null && e.text() === '13')
.simulate('click') // click on hours
expect(getClock(tree).props().mode).toBe('24h')
})
})
})
function getClock (picker) {
return picker.findWhere((e) => e.name() === 'Clock')
}