@targetprocess/popover 12.20.1-alpha.2 → 12.20.2-alpha.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,284 +1,287 @@
1
- import React from 'react'
2
- import ReactDOM from 'react-dom'
3
- import {mount, MountRendererProps} from 'enzyme'
4
- import {BodyDomNode, BodyDomNodeProps} from '../BodyDomNode'
5
- import {
6
- whenComponentDidMountCalled,
7
- whenComponentDidUpdateCalled,
8
- whenComponentWillUnmountCalled
9
- } from '../../../testUtils/componentLifecycleUtils'
10
- import {cleanDocumentBody} from '../../../testUtils/domUtils'
11
-
12
- const originalBodyDomNodeComponentDidMount = BodyDomNode.prototype.componentDidMount
13
- const originalBodyDomNodeComponentWillUnmount = BodyDomNode.prototype.componentWillUnmount
14
- const originalBodyAppendChild = document.body.appendChild
15
-
16
- const stubComponentDidMount = () => whenComponentDidMountCalled(BodyDomNode)
17
- const stubComponentWillUnmount = () => whenComponentWillUnmountCalled(BodyDomNode)
18
- const stubComponentDidUpdate = () => whenComponentDidUpdateCalled(BodyDomNode)
19
-
20
- const defaultRenderProp = () => null
21
- const mountComponent = (
22
- props: Partial<BodyDomNodeProps> = {},
23
- mountOptions?: MountRendererProps
24
- ) => {
25
- const {children, positioningTarget = document.createElement('dov'), ...other} = props
26
- const renderProp = children || defaultRenderProp
27
-
28
- return mount(
29
- <BodyDomNode {...other} positioningTarget={positioningTarget} containerElement={document.body}>
30
- {renderProp}
31
- </BodyDomNode>,
32
- mountOptions
33
- )
34
- }
35
-
36
- const addDivToBody = () => {
37
- const div = document.createElement('div')
38
-
39
- document.body.appendChild(div)
40
-
41
- return div
42
- }
43
-
44
- const dispatchNativeMouseEvent = (target: Element, event: string = 'click') =>
45
- target.dispatchEvent(new MouseEvent(event, {bubbles: true}))
46
- const dispatchNativeKeyboardEvent = (target: Element, event: string = 'keydown') =>
47
- target.dispatchEvent(new KeyboardEvent(event, {bubbles: true}))
48
-
49
- describe('BodyDomNode', () => {
50
- afterEach(() => {
51
- BodyDomNode.prototype.componentDidMount = originalBodyDomNodeComponentDidMount
52
- BodyDomNode.prototype.componentWillUnmount = originalBodyDomNodeComponentWillUnmount
53
- document.body.appendChild = originalBodyAppendChild
54
-
55
- cleanDocumentBody()
56
- })
57
-
58
- it('should call render prop with null initially', () => {
59
- const renderProp = jest.fn(() => null)
60
-
61
- mountComponent({children: renderProp})
62
-
63
- expect(renderProp).toHaveBeenCalled()
64
- expect(renderProp.mock.calls[0][0]).toBeNull()
65
- })
66
-
67
- it('should render element returned by render prop', () => {
68
- const wrapper = mountComponent({children: () => <button>Test</button>})
69
-
70
- expect(wrapper.find('button')).toHaveLength(1)
71
- })
72
-
73
- it('should call render prop with mounted dom node', async () => {
74
- const appendChild = jest.fn(child => originalBodyAppendChild.call(document.body, child))
75
- const renderProp = jest.fn(() => null)
76
- const componentDidMountCalled = stubComponentDidMount()
77
-
78
- document.body.appendChild = appendChild
79
-
80
- mountComponent({children: renderProp})
81
-
82
- await componentDidMountCalled
83
-
84
- expect(document.body.children).toHaveLength(1)
85
- expect(renderProp.mock.calls[1][0]).toBe(appendChild.mock.calls[0][0])
86
- })
87
-
88
- it('should remove dom node after component will be unmounted', async () => {
89
- const componentDidMountCalled = stubComponentDidMount()
90
- const componentWillUnmountCalled = stubComponentWillUnmount()
91
- const wrapper = mountComponent()
92
-
93
- await componentDidMountCalled
94
-
95
- expect(document.body.children).toHaveLength(1)
96
-
97
- wrapper.unmount()
98
-
99
- await componentWillUnmountCalled
100
-
101
- expect(document.body.children).toHaveLength(0)
102
- })
103
-
104
- it('should call onOuterAction callback for every event from outerActionEvents prop', async () => {
105
- const componentDidMountCalled = stubComponentDidMount()
106
- const onOuterAction = jest.fn()
107
-
108
- mountComponent({
109
- onOuterAction,
110
- outerActionEvents: ['click', 'keydown']
111
- })
112
-
113
- await componentDidMountCalled
114
-
115
- const eventTargetNode = addDivToBody()
116
-
117
- dispatchNativeMouseEvent(eventTargetNode)
118
- dispatchNativeKeyboardEvent(eventTargetNode)
119
-
120
- expect(onOuterAction).toBeCalledTimes(2)
121
- })
122
-
123
- it('should not call onOuterAction callback if outerActionEvents is not specified', async () => {
124
- const componentDidMountCalled = stubComponentDidMount()
125
- const onOuterAction = jest.fn()
126
-
127
- mountComponent({
128
- onOuterAction
129
- })
130
-
131
- await componentDidMountCalled
132
-
133
- const eventTargetNode = addDivToBody()
134
-
135
- dispatchNativeMouseEvent(eventTargetNode)
136
- dispatchNativeKeyboardEvent(eventTargetNode)
137
-
138
- expect(onOuterAction).not.toBeCalled()
139
- })
140
-
141
- it('should not call onOuterAction callback for events that is not specified in outerActionEvents', async () => {
142
- const componentDidMountCalled = stubComponentDidMount()
143
- const onOuterAction = jest.fn()
144
-
145
- mountComponent({
146
- onOuterAction,
147
- outerActionEvents: ['click', 'keydown']
148
- })
149
-
150
- await componentDidMountCalled
151
-
152
- const eventTargetNode = addDivToBody()
153
-
154
- const mouseEvents = ['mousedown', 'mouseup', 'mousein', 'mouseout']
155
- const keyboardEvents = ['keypress', 'keyup']
156
-
157
- mouseEvents.forEach(event => dispatchNativeMouseEvent(eventTargetNode, event))
158
- keyboardEvents.forEach(event => dispatchNativeKeyboardEvent(eventTargetNode, event))
159
-
160
- expect(onOuterAction).not.toBeCalled()
161
- })
162
-
163
- it('should not call onOuterAction after component will be unmounted', async () => {
164
- const componentDidMountCalled = stubComponentDidMount()
165
- const componentWillUnmountCalled = stubComponentWillUnmount()
166
- const onOuterAction = jest.fn()
167
-
168
- const wrapper = mountComponent({
169
- onOuterAction,
170
- outerActionEvents: ['click', 'keydown']
171
- })
172
-
173
- await componentDidMountCalled
174
-
175
- const eventTargetNode = addDivToBody()
176
-
177
- wrapper.unmount()
178
-
179
- await componentWillUnmountCalled
180
-
181
- dispatchNativeMouseEvent(eventTargetNode)
182
- dispatchNativeKeyboardEvent(eventTargetNode)
183
-
184
- expect(onOuterAction).not.toBeCalled()
185
- })
186
-
187
- it('should not call onOuterAction for events inside component children', async () => {
188
- const componentDidMountCalled = stubComponentDidMount()
189
- const onOuterAction = jest.fn()
190
- const componentContainer = addDivToBody()
191
-
192
- const wrapper = mountComponent(
193
- {
194
- children: node =>
195
- node ? ReactDOM.createPortal(<div className="i-role-event-target" />, node) : null,
196
- onOuterAction,
197
- outerActionEvents: ['click', 'keydown']
198
- },
199
- {attachTo: componentContainer}
200
- )
201
-
202
- await componentDidMountCalled
203
-
204
- const eventTargetNode = document.querySelector('.i-role-event-target')
205
-
206
- dispatchNativeMouseEvent(eventTargetNode)
207
- dispatchNativeKeyboardEvent(eventTargetNode)
208
-
209
- expect(onOuterAction).not.toBeCalled()
210
-
211
- wrapper.detach()
212
- })
213
-
214
- it('should not call previous onOuterAction after prop update', async () => {
215
- const componentDidMountCalled = stubComponentDidMount()
216
- const componentDidUpdateCalled = stubComponentDidUpdate()
217
-
218
- const onOuterActionBeforeUpdate = jest.fn()
219
- const onOuterActionAfterUpdate = jest.fn()
220
-
221
- const wrapper = mountComponent({
222
- onOuterAction: onOuterActionBeforeUpdate,
223
- outerActionEvents: ['click']
224
- })
225
-
226
- await componentDidMountCalled
227
-
228
- wrapper.setProps({onOuterAction: onOuterActionAfterUpdate})
229
-
230
- await componentDidUpdateCalled
231
-
232
- const eventTargetNode = addDivToBody()
233
-
234
- dispatchNativeMouseEvent(eventTargetNode)
235
-
236
- expect(onOuterActionBeforeUpdate).toBeCalledTimes(0)
237
- expect(onOuterActionAfterUpdate).toBeCalledTimes(1)
238
- })
239
-
240
- it('should not call onOuterAction for removed events after outerActiveEvents array change', async () => {
241
- const componentDidMountCalled = stubComponentDidMount()
242
- const componentDidUpdateCalled = stubComponentDidUpdate()
243
-
244
- const onOuterAction = jest.fn()
245
-
246
- const wrapper = mountComponent({
247
- onOuterAction,
248
- outerActionEvents: ['click']
249
- })
250
-
251
- await componentDidMountCalled
252
-
253
- wrapper.setProps({outerActionEvents: ['keydown']})
254
-
255
- await componentDidUpdateCalled
256
-
257
- const eventTargetNode = addDivToBody()
258
-
259
- dispatchNativeMouseEvent(eventTargetNode)
260
- dispatchNativeKeyboardEvent(eventTargetNode)
261
-
262
- expect(onOuterAction).toBeCalledTimes(1)
263
- expect(onOuterAction.mock.calls[0][0].type).toBe('keydown')
264
- })
265
-
266
- it('should not call onOuterAction for event inside positioningTarget tree', async () => {
267
- const componentDidMountCalled = stubComponentDidMount()
268
- const onOuterAction = jest.fn()
269
- const eventTargetNode = addDivToBody()
270
-
271
- mountComponent({
272
- onOuterAction,
273
- outerActionEvents: ['click', 'keydown'],
274
- positioningTarget: eventTargetNode
275
- })
276
-
277
- await componentDidMountCalled
278
-
279
- dispatchNativeMouseEvent(eventTargetNode)
280
- dispatchNativeKeyboardEvent(eventTargetNode)
281
-
282
- expect(onOuterAction).not.toBeCalled()
283
- })
284
- })
1
+ /*********************************************************************
2
+ * © Copyright IBM Corp. 2024
3
+ *********************************************************************/
4
+ import React from 'react'
5
+ import ReactDOM from 'react-dom'
6
+ import {mount, MountRendererProps} from 'enzyme'
7
+ import {BodyDomNode, BodyDomNodeProps} from '../BodyDomNode'
8
+ import {
9
+ whenComponentDidMountCalled,
10
+ whenComponentDidUpdateCalled,
11
+ whenComponentWillUnmountCalled
12
+ } from '../../../testUtils/componentLifecycleUtils'
13
+ import {cleanDocumentBody} from '../../../testUtils/domUtils'
14
+
15
+ const originalBodyDomNodeComponentDidMount = BodyDomNode.prototype.componentDidMount
16
+ const originalBodyDomNodeComponentWillUnmount = BodyDomNode.prototype.componentWillUnmount
17
+ const originalBodyAppendChild = document.body.appendChild
18
+
19
+ const stubComponentDidMount = () => whenComponentDidMountCalled(BodyDomNode)
20
+ const stubComponentWillUnmount = () => whenComponentWillUnmountCalled(BodyDomNode)
21
+ const stubComponentDidUpdate = () => whenComponentDidUpdateCalled(BodyDomNode)
22
+
23
+ const defaultRenderProp = () => null
24
+ const mountComponent = (
25
+ props: Partial<BodyDomNodeProps> = {},
26
+ mountOptions?: MountRendererProps
27
+ ) => {
28
+ const {children, positioningTarget = document.createElement('dov'), ...other} = props
29
+ const renderProp = children || defaultRenderProp
30
+
31
+ return mount(
32
+ <BodyDomNode {...other} positioningTarget={positioningTarget} containerElement={document.body}>
33
+ {renderProp}
34
+ </BodyDomNode>,
35
+ mountOptions
36
+ )
37
+ }
38
+
39
+ const addDivToBody = () => {
40
+ const div = document.createElement('div')
41
+
42
+ document.body.appendChild(div)
43
+
44
+ return div
45
+ }
46
+
47
+ const dispatchNativeMouseEvent = (target: Element, event: string = 'click') =>
48
+ target.dispatchEvent(new MouseEvent(event, {bubbles: true}))
49
+ const dispatchNativeKeyboardEvent = (target: Element, event: string = 'keydown') =>
50
+ target.dispatchEvent(new KeyboardEvent(event, {bubbles: true}))
51
+
52
+ describe('BodyDomNode', () => {
53
+ afterEach(() => {
54
+ BodyDomNode.prototype.componentDidMount = originalBodyDomNodeComponentDidMount
55
+ BodyDomNode.prototype.componentWillUnmount = originalBodyDomNodeComponentWillUnmount
56
+ document.body.appendChild = originalBodyAppendChild
57
+
58
+ cleanDocumentBody()
59
+ })
60
+
61
+ it('should call render prop with null initially', () => {
62
+ const renderProp = jest.fn(() => null)
63
+
64
+ mountComponent({children: renderProp})
65
+
66
+ expect(renderProp).toHaveBeenCalled()
67
+ expect(renderProp.mock.calls[0][0]).toBeNull()
68
+ })
69
+
70
+ it('should render element returned by render prop', () => {
71
+ const wrapper = mountComponent({children: () => <button>Test</button>})
72
+
73
+ expect(wrapper.find('button')).toHaveLength(1)
74
+ })
75
+
76
+ it('should call render prop with mounted dom node', async () => {
77
+ const appendChild = jest.fn(child => originalBodyAppendChild.call(document.body, child))
78
+ const renderProp = jest.fn(() => null)
79
+ const componentDidMountCalled = stubComponentDidMount()
80
+
81
+ document.body.appendChild = appendChild
82
+
83
+ mountComponent({children: renderProp})
84
+
85
+ await componentDidMountCalled
86
+
87
+ expect(document.body.children).toHaveLength(1)
88
+ expect(renderProp.mock.calls[1][0]).toBe(appendChild.mock.calls[0][0])
89
+ })
90
+
91
+ it('should remove dom node after component will be unmounted', async () => {
92
+ const componentDidMountCalled = stubComponentDidMount()
93
+ const componentWillUnmountCalled = stubComponentWillUnmount()
94
+ const wrapper = mountComponent()
95
+
96
+ await componentDidMountCalled
97
+
98
+ expect(document.body.children).toHaveLength(1)
99
+
100
+ wrapper.unmount()
101
+
102
+ await componentWillUnmountCalled
103
+
104
+ expect(document.body.children).toHaveLength(0)
105
+ })
106
+
107
+ it('should call onOuterAction callback for every event from outerActionEvents prop', async () => {
108
+ const componentDidMountCalled = stubComponentDidMount()
109
+ const onOuterAction = jest.fn()
110
+
111
+ mountComponent({
112
+ onOuterAction,
113
+ outerActionEvents: ['click', 'keydown']
114
+ })
115
+
116
+ await componentDidMountCalled
117
+
118
+ const eventTargetNode = addDivToBody()
119
+
120
+ dispatchNativeMouseEvent(eventTargetNode)
121
+ dispatchNativeKeyboardEvent(eventTargetNode)
122
+
123
+ expect(onOuterAction).toBeCalledTimes(2)
124
+ })
125
+
126
+ it('should not call onOuterAction callback if outerActionEvents is not specified', async () => {
127
+ const componentDidMountCalled = stubComponentDidMount()
128
+ const onOuterAction = jest.fn()
129
+
130
+ mountComponent({
131
+ onOuterAction
132
+ })
133
+
134
+ await componentDidMountCalled
135
+
136
+ const eventTargetNode = addDivToBody()
137
+
138
+ dispatchNativeMouseEvent(eventTargetNode)
139
+ dispatchNativeKeyboardEvent(eventTargetNode)
140
+
141
+ expect(onOuterAction).not.toBeCalled()
142
+ })
143
+
144
+ it('should not call onOuterAction callback for events that is not specified in outerActionEvents', async () => {
145
+ const componentDidMountCalled = stubComponentDidMount()
146
+ const onOuterAction = jest.fn()
147
+
148
+ mountComponent({
149
+ onOuterAction,
150
+ outerActionEvents: ['click', 'keydown']
151
+ })
152
+
153
+ await componentDidMountCalled
154
+
155
+ const eventTargetNode = addDivToBody()
156
+
157
+ const mouseEvents = ['mousedown', 'mouseup', 'mousein', 'mouseout']
158
+ const keyboardEvents = ['keypress', 'keyup']
159
+
160
+ mouseEvents.forEach(event => dispatchNativeMouseEvent(eventTargetNode, event))
161
+ keyboardEvents.forEach(event => dispatchNativeKeyboardEvent(eventTargetNode, event))
162
+
163
+ expect(onOuterAction).not.toBeCalled()
164
+ })
165
+
166
+ it('should not call onOuterAction after component will be unmounted', async () => {
167
+ const componentDidMountCalled = stubComponentDidMount()
168
+ const componentWillUnmountCalled = stubComponentWillUnmount()
169
+ const onOuterAction = jest.fn()
170
+
171
+ const wrapper = mountComponent({
172
+ onOuterAction,
173
+ outerActionEvents: ['click', 'keydown']
174
+ })
175
+
176
+ await componentDidMountCalled
177
+
178
+ const eventTargetNode = addDivToBody()
179
+
180
+ wrapper.unmount()
181
+
182
+ await componentWillUnmountCalled
183
+
184
+ dispatchNativeMouseEvent(eventTargetNode)
185
+ dispatchNativeKeyboardEvent(eventTargetNode)
186
+
187
+ expect(onOuterAction).not.toBeCalled()
188
+ })
189
+
190
+ it('should not call onOuterAction for events inside component children', async () => {
191
+ const componentDidMountCalled = stubComponentDidMount()
192
+ const onOuterAction = jest.fn()
193
+ const componentContainer = addDivToBody()
194
+
195
+ const wrapper = mountComponent(
196
+ {
197
+ children: node =>
198
+ node ? ReactDOM.createPortal(<div className="i-role-event-target" />, node) : null,
199
+ onOuterAction,
200
+ outerActionEvents: ['click', 'keydown']
201
+ },
202
+ {attachTo: componentContainer}
203
+ )
204
+
205
+ await componentDidMountCalled
206
+
207
+ const eventTargetNode = document.querySelector('.i-role-event-target')
208
+
209
+ dispatchNativeMouseEvent(eventTargetNode)
210
+ dispatchNativeKeyboardEvent(eventTargetNode)
211
+
212
+ expect(onOuterAction).not.toBeCalled()
213
+
214
+ wrapper.detach()
215
+ })
216
+
217
+ it('should not call previous onOuterAction after prop update', async () => {
218
+ const componentDidMountCalled = stubComponentDidMount()
219
+ const componentDidUpdateCalled = stubComponentDidUpdate()
220
+
221
+ const onOuterActionBeforeUpdate = jest.fn()
222
+ const onOuterActionAfterUpdate = jest.fn()
223
+
224
+ const wrapper = mountComponent({
225
+ onOuterAction: onOuterActionBeforeUpdate,
226
+ outerActionEvents: ['click']
227
+ })
228
+
229
+ await componentDidMountCalled
230
+
231
+ wrapper.setProps({onOuterAction: onOuterActionAfterUpdate})
232
+
233
+ await componentDidUpdateCalled
234
+
235
+ const eventTargetNode = addDivToBody()
236
+
237
+ dispatchNativeMouseEvent(eventTargetNode)
238
+
239
+ expect(onOuterActionBeforeUpdate).toBeCalledTimes(0)
240
+ expect(onOuterActionAfterUpdate).toBeCalledTimes(1)
241
+ })
242
+
243
+ it('should not call onOuterAction for removed events after outerActiveEvents array change', async () => {
244
+ const componentDidMountCalled = stubComponentDidMount()
245
+ const componentDidUpdateCalled = stubComponentDidUpdate()
246
+
247
+ const onOuterAction = jest.fn()
248
+
249
+ const wrapper = mountComponent({
250
+ onOuterAction,
251
+ outerActionEvents: ['click']
252
+ })
253
+
254
+ await componentDidMountCalled
255
+
256
+ wrapper.setProps({outerActionEvents: ['keydown']})
257
+
258
+ await componentDidUpdateCalled
259
+
260
+ const eventTargetNode = addDivToBody()
261
+
262
+ dispatchNativeMouseEvent(eventTargetNode)
263
+ dispatchNativeKeyboardEvent(eventTargetNode)
264
+
265
+ expect(onOuterAction).toBeCalledTimes(1)
266
+ expect(onOuterAction.mock.calls[0][0].type).toBe('keydown')
267
+ })
268
+
269
+ it('should not call onOuterAction for event inside positioningTarget tree', async () => {
270
+ const componentDidMountCalled = stubComponentDidMount()
271
+ const onOuterAction = jest.fn()
272
+ const eventTargetNode = addDivToBody()
273
+
274
+ mountComponent({
275
+ onOuterAction,
276
+ outerActionEvents: ['click', 'keydown'],
277
+ positioningTarget: eventTargetNode
278
+ })
279
+
280
+ await componentDidMountCalled
281
+
282
+ dispatchNativeMouseEvent(eventTargetNode)
283
+ dispatchNativeKeyboardEvent(eventTargetNode)
284
+
285
+ expect(onOuterAction).not.toBeCalled()
286
+ })
287
+ })