forked from Clovis-team/clovis-open-code-extracts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient:components:text-input.test.js
102 lines (79 loc) · 2.77 KB
/
client:components:text-input.test.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
import sinon from 'sinon'
import { shallow, mount } from 'enzyme'
import { assert } from 'chai'
import {
MockedTooltipMountPoint as TooltipMountPoint
} from '~/components/tooltip/mocked-mount-point'
import { TextInput } from '.'
import { ValidationResult } from './validation'
describe('<TextInput />', () => {
it('renders an <input> by default', () => {
const ti = shallow(<TextInput />)
assert(ti.find('input').length === 1)
assert(!ti.find('textarea').length)
})
it('can render a <textarea>', () => {
const ti = shallow(<TextInput textarea />)
assert(!ti.find('input').length)
assert(ti.find('textarea').length === 1)
})
it('transfer props to the underlying <input>', () => {
const ti = shallow(<TextInput readOnly />)
assert(ti.find('input').props().readOnly === true)
})
it('shows and hides validation errors properly', async () => {
const validator = () =>
new ValidationResult('error', 'Error message')
const wait = () =>
new Promise(resolve => setTimeout(resolve, 0))
let tooltipVisible = false
const tmp = mount(
<TooltipMountPoint onCreation={id => tooltipVisible = true}
onRemoval={() => tooltipVisible = false}>
<TextInput validator={validator} />
</TooltipMountPoint>
)
const ti = tmp.find('.text-input')
const input = ti.find('input')
input.simulate('blur')
await wait()
assert(!tooltipVisible)
input.simulate('focus')
await wait()
input.simulate('blur')
await wait()
assert(!tooltipVisible)
input.simulate('change')
await wait()
input.simulate('blur')
await wait()
assert(tooltipVisible)
input.simulate('focus')
await wait()
assert(!tooltipVisible)
input.simulate('blur')
await wait()
assert(tooltipVisible)
})
it('get focused when the autoFocus prop change', () => {
const ti = mount(<TextInput />)
const input = ti.state().input
input.focus = sinon.spy()
ti.setProps({ autoFocus: true })
assert(input.focus.calledOnce)
})
it("doesn't get focused when readOnly", () => {
const ti = mount(<TextInput readOnly />)
const input = ti.state().input
input.focus = sinon.spy()
ti.setProps({ autoFocus: true })
assert(input.focus.notCalled)
})
it("doesn't get focused when disabled", () => {
const ti = mount(<TextInput disabled />)
const input = ti.state().input
input.focus = sinon.spy()
ti.setProps({ autoFocus: true })
assert(input.focus.notCalled)
})
})