-
-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathTimeInput.spec.js
154 lines (128 loc) · 5.66 KB
/
TimeInput.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
/* eslint-env jest */
import React from 'react'
import { mount } from 'enzyme'
import MockDate from 'mockdate'
import { unwrap } from '@material-ui/core/test-utils'
import Button from '@material-ui/core/Button'
import Input from '@material-ui/core/Input'
import TimeInput from './TimeInput'
import Clock from './Clock'
import * as testUtils from '../test/utils'
describe('<TimeInput />', () => {
describe('24h', () => {
it('matches the snapshot', () => {
const originalGetHours = Date.prototype.getHours
Date.prototype.getHours = Date.prototype.getUTCHours // eslint-disable-line
const tree = mount(<TimeInput defaultValue={new Date('2017-10-15T13:37Z')} mode='24h' />)
expect(tree).toMatchSnapshot()
Date.prototype.getHours = originalGetHours // eslint-disable-line
})
it('displays the formatted time', () => {
const tree = mount(<TimeInput value={new Date(2017, 10, 15, 13, 37, 0, 0)} mode='24h' />)
expect(getValue(tree)).toBe('13:37')
const tree2 = mount(<TimeInput value={new Date(2017, 10, 15, 1, 23, 0, 0)} mode='24h' />)
expect(getValue(tree2)).toBe('01:23')
})
it('disables the input if the disabled prop is set to true', () => {
const tree = mount(<TimeInput value={new Date(2017, 10, 15, 13, 37, 0, 0)} mode='24h' disabled />)
expect(tree.find(Input).prop('disabled')).toBe(true)
})
})
describe('12h', () => {
it('matches the snapshot', () => {
const originalGetHours = Date.prototype.getHours
Date.prototype.getHours = Date.prototype.getUTCHours // eslint-disable-line
const tree = mount(<TimeInput defaultValue={new Date('2017-10-15T13:37Z')} mode='12h' />)
expect(tree).toMatchSnapshot()
Date.prototype.getHours = originalGetHours // eslint-disable-line
})
it('displays the formatted time', () => {
const tree = mount(<TimeInput value={new Date(2017, 10, 15, 13, 37, 0, 0)} mode='12h' />)
expect(getValue(tree)).toBe('1:37 pm')
const tree2 = mount(<TimeInput value={new Date(2017, 10, 15, 1, 23, 0, 0)} mode='12h' />)
expect(getValue(tree2)).toBe('1:23 am')
const tree3 = mount(<TimeInput value={new Date(2017, 10, 15, 0, 0, 0, 0)} mode='12h' />)
expect(getValue(tree3)).toBe('12:00 am')
const tree4 = mount(<TimeInput value={new Date(2017, 10, 15, 0, 0, 0, 0)} mode='12h' />)
expect(getValue(tree4)).toBe('12:00 am')
})
})
it('supports controlled mode', () => {
const tree = mount(<TimeInput value={new Date(2017, 10, 15, 13, 37, 0, 0)} mode='24h' />)
expect(getValue(tree)).toBe('13:37')
tree.setProps({ value: new Date(2017, 10, 15, 14, 42, 0, 0) })
expect(getValue(tree)).toBe('14:42')
})
describe('uncontrolled mode', () => {
afterEach(() => {
MockDate.reset()
})
it('supports uncontrolled mode with a default value', () => {
const tree = mount(<TimeInput defaultValue={new Date(2017, 10, 15, 13, 37, 0, 0)} mode='24h' />)
expect(getValue(tree)).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(<TimeInput mode='24h' />)
expect(getValue(tree)).toBe('13:37')
})
})
describe('TimePicker dialog', () => {
it('opens when clicking the input', () => {
const UnstyledTimeInput = unwrap(TimeInput)
const tree = mount(<UnstyledTimeInput classes={{}} />)
tree.simulate('click')
expect(tree.state('open')).toBe(true)
})
it('opens when clicking the input if it is disabled', () => {
const UnstyledTimeInput = unwrap(TimeInput)
const tree = mount(<UnstyledTimeInput classes={{}} disabled />)
tree.simulate('click')
expect(tree.state('open')).toBe(false)
})
it('closes when clicking ok', () => {
const UnstyledTimeInput = unwrap(TimeInput)
const tree = mount(<UnstyledTimeInput classes={{}} />)
tree.simulate('click')
tree.find(Button).at(1).simulate('click')
expect(tree.state('open')).toBe(false)
})
it('updates uses the new time when clicking ok', () => {
const changeHandler = jest.fn()
MockDate.set(new Date(2017, 10, 15, 13, 37, 0, 0))
const tree = mount(<TimeInput classes={{}} mode='24h' onChange={changeHandler} />)
tree.simulate('click')
getCircle(tree.find(Clock))
.simulate('click', testUtils.stubClickEvent(128, 30)) // click on 12
.simulate('mouseup', testUtils.stubClickEvent(128, 30)) // click on 12
tree.find(Button).at(1).simulate('click')
expect(getValue(tree)).toBe('12:37')
expect(changeHandler).toHaveBeenCalled()
const time = changeHandler.mock.calls[0][0]
expect(time.getHours()).toBe(12)
expect(time.getMinutes()).toBe(37)
})
it('closes when clicking cancel', () => {
const tree = mount(<TimeInput />)
tree.simulate('click')
tree.find(Button).at(0).simulate('click')
})
it('discards the new time when clicking cancel', () => {
const changeHandler = jest.fn()
MockDate.set(new Date(2017, 10, 15, 13, 37, 0, 0))
const tree = mount(<TimeInput classes={{}} mode='24h' onChange={changeHandler} />)
tree.simulate('click')
getCircle(tree.find(Clock))
.simulate('click', testUtils.stubClickEvent(128, 30)) // click on 12
.simulate('mouseup', testUtils.stubClickEvent(128, 30)) // click on 12
tree.find(Button).at(0).simulate('click')
expect(getValue(tree)).toBe('13:37') // unchanged
})
})
})
function getValue (timeInput) {
return timeInput.find('input[type="text"]').getDOMNode().value
}
function getCircle (clock) {
return clock.childAt(0).children('div').childAt(0)
}