forked from geist-org/geist-ui
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdropdown.test.tsx
190 lines (173 loc) · 4.92 KB
/
dropdown.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
import React, { useRef } from 'react'
import { mount } from 'enzyme'
import Dropdown from '../dropdown'
import { nativeEvent, updateWrapper } from 'tests/utils'
import { act } from 'react-dom/test-utils'
const simulateGlobalClick = () => {
document.body.dispatchEvent(
new MouseEvent('click', {
view: window,
bubbles: true,
cancelable: true,
}),
)
}
describe('Dropdown', () => {
beforeAll(() => {
window.Element.prototype.getBoundingClientRect = () =>
({
width: 100,
left: 0,
right: 100,
top: 0,
bottom: 100,
height: 100,
x: 0,
} as DOMRect)
})
it('should render correctly', async () => {
const Mock: React.FC<{ visible?: boolean }> = ({ visible = false }) => {
const ref = useRef<HTMLDivElement>(null)
return (
<div ref={ref}>
<Dropdown parent={ref} visible={visible}>
<span>test-value</span>
</Dropdown>
</div>
)
}
const wrapper = mount(<Mock />)
wrapper.setProps({ visible: true })
await updateWrapper(wrapper, 300)
expect(wrapper.find('.dropdown').html()).toContain('test-value')
expect(wrapper.html()).toMatchSnapshot()
expect(() => wrapper.unmount()).not.toThrow()
})
it('should be work without parent', () => {
const wrapper = mount(
<Dropdown visible>
<span>test-value</span>
</Dropdown>,
)
expect(() => wrapper.unmount()).not.toThrow()
})
it('events should be prevented', () => {
const handler = jest.fn()
const Mock: React.FC<unknown> = () => {
const ref = useRef<HTMLDivElement>(null)
return (
<div ref={ref} onClick={handler}>
<Dropdown parent={ref} visible>
<span>test-value</span>
</Dropdown>
</div>
)
}
const wrapper = mount(<Mock />)
wrapper.find('.dropdown').simulate('click', nativeEvent)
expect(handler).not.toHaveBeenCalled()
expect(() => wrapper.unmount()).not.toThrow()
handler.mockRestore()
})
it('should trigger rect update', async () => {
let dynamicTopMock = 100,
calledTimes = 0
window.Element.prototype.getBoundingClientRect = () => {
calledTimes++
return {
width: 100,
left: 0,
right: 100,
top: 0,
bottom: dynamicTopMock,
height: 100,
x: 0,
} as DOMRect
}
const Mock: React.FC<unknown> = () => {
const ref = useRef<HTMLDivElement>(null)
return (
<div ref={ref}>
<Dropdown parent={ref} visible>
<span>test-value</span>
</Dropdown>
</div>
)
}
const wrapper = mount(<Mock />)
expect(calledTimes).toBe(1)
// Do not render if position is not updated
act(() => simulateGlobalClick())
expect(calledTimes).toBe(2)
await updateWrapper(wrapper, 50)
// Trigger position diff first, then trigger the update
// Get Rect twice total
act(() => {
dynamicTopMock++
simulateGlobalClick()
})
expect(calledTimes).toBeGreaterThanOrEqual(4)
act(() => {
dynamicTopMock++
window.dispatchEvent(new Event('resize'))
})
expect(calledTimes).toBeGreaterThanOrEqual(5)
expect(() => wrapper.unmount()).not.toThrow()
})
it('should tigger rect update when mouseenter', () => {
let calledTimes = 0
window.Element.prototype.getBoundingClientRect = () => {
calledTimes++
return {
width: 100,
left: 0,
right: 100,
top: 0,
bottom: 100,
height: 100,
x: 0,
} as DOMRect
}
const Mock: React.FC<unknown> = () => {
const ref = useRef<HTMLDivElement>(null)
return (
<div ref={ref} id="parent">
<Dropdown parent={ref} visible>
<span>test-value</span>
</Dropdown>
</div>
)
}
const wrapper = mount(<Mock />)
expect(calledTimes).toBe(1)
// MouseEnter event is monitored by native API, the simulate can not trigger it.
const parent = wrapper.find('#parent').getDOMNode() as HTMLDivElement
act(() => {
parent.dispatchEvent(new Event('mouseenter'))
})
expect(calledTimes).toBe(2)
expect(() => wrapper.unmount()).not.toThrow()
})
it('should render to specified container', () => {
const Mock: React.FC<unknown> = () => {
const ref = useRef<HTMLDivElement>(null)
const customContainer = useRef<HTMLDivElement>(null)
return (
<div>
<div ref={customContainer} id="custom" />
<div ref={ref}>
<Dropdown
parent={ref}
visible
getPopupContainer={() => customContainer.current}>
<span>test-value</span>
</Dropdown>
</div>
</div>
)
}
const wrapper = mount(<Mock />)
const customContainer = wrapper.find('#custom')
expect(customContainer.html()).toContain('dropdown')
})
})