-
Notifications
You must be signed in to change notification settings - Fork 34
Expand file tree
/
Copy pathmodal-container.test.js
More file actions
138 lines (104 loc) · 3.99 KB
/
Copy pathmodal-container.test.js
File metadata and controls
138 lines (104 loc) · 3.99 KB
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
import React from 'react'
import renderer from 'react-test-renderer'
import ReactDOM from 'react-dom'
import ModalContainer from './modal-container'
describe('ModalContainer', () => {
test('ModalContainer component', () => {
// Override watchForPortalContainerMutations so we bypass the MutationObserver
const spy = jest
.spyOn(ModalContainer.prototype, 'watchForPortalContainerMutations')
.mockImplementation(jest.fn())
const component = renderer.create(<ModalContainer />)
const tree = component.toJSON()
expect(tree).toMatchSnapshot()
spy.mockRestore()
})
test('ModalContainer works with legacy modals', () => {
// Override watchForPortalContainerMutations so we bypass the MutationObserver
const spy = jest
.spyOn(ModalContainer.prototype, 'watchForPortalContainerMutations')
.mockImplementation(jest.fn())
const component = renderer.create(
<ModalContainer modalItem={{ component: <div>modalItem</div> }} />
)
const tree = component.toJSON()
expect(tree).toMatchSnapshot()
spy.mockRestore()
})
test('ModalContainer works with portal modals', () => {
// Override watchForPortalContainerMutations so we bypass the MutationObserver
const spy = jest
.spyOn(ModalContainer.prototype, 'watchForPortalContainerMutations')
.mockImplementation(jest.fn())
const component = renderer.create(
<ModalContainer modalItem={{ component: <div>modalItem</div> }} />
)
component.getInstance().setState({ numPortalElements: 1 })
const tree = component.toJSON()
expect(tree).toMatchSnapshot()
spy.mockRestore()
})
test('componentDidMount and onRefChanged call watchForPortalContainerMutations', () => {
const mockWatchFn = jest.fn()
ModalContainer.prototype.componentDidMount.bind({
watchForPortalContainerMutations: mockWatchFn
})()
expect(mockWatchFn).toBeCalledTimes(1)
ModalContainer.prototype.onRefChanged.bind({
watchForPortalContainerMutations: mockWatchFn
})()
expect(mockWatchFn).toBeCalledTimes(2)
})
test('onMutation updates state with the number of portal elements', () => {
const spy = jest
.spyOn(ReactDOM, 'findDOMNode')
.mockReturnValue({ children: { length: 'mock-length' } })
const mockSetState = jest.fn()
ModalContainer.prototype.onMutation.bind({
setState: mockSetState
})()
expect(mockSetState).toHaveBeenCalledWith({ numPortalElements: 'mock-length' })
spy.mockRestore()
})
test('watchForPortalContainerMutations creates a MutationObserver', () => {
const originalMutationObserver = global.MutationObserver
// Override MutationObserver
const mockObserve = jest.fn()
const mockDisconnect = jest.fn()
const mockEl = jest.fn()
Object.defineProperty(global, 'MutationObserver', {
value: () => ({
observe: mockObserve,
disconnect: mockDisconnect
}),
enumerable: true,
configurable: true
})
const spy = jest.spyOn(ReactDOM, 'findDOMNode').mockReturnValue(mockEl)
const thisVal = {}
ModalContainer.prototype.watchForPortalContainerMutations.bind(thisVal)()
expect(thisVal).toEqual({ observer: { observe: mockObserve, disconnect: mockDisconnect } })
expect(mockDisconnect).toHaveBeenCalled()
expect(mockObserve).toHaveBeenCalledWith(mockEl, { childList: true })
spy.mockRestore()
// Restore MutationObserver
Object.defineProperty(global, 'MutationObserver', {
value: originalMutationObserver
})
})
test('watchForPortalContainerMutations reuses a MutationObserver', () => {
// Override MutationObserver
const mockObserve = jest.fn()
const mockDisconnect = jest.fn()
const mockEl = jest.fn()
const spy = jest.spyOn(ReactDOM, 'findDOMNode').mockReturnValue(mockEl)
const thisVal = {
observer: { observe: mockObserve, disconnect: mockDisconnect }
}
ModalContainer.prototype.watchForPortalContainerMutations.bind(thisVal)()
expect(thisVal).toEqual({ observer: { observe: mockObserve, disconnect: mockDisconnect } })
expect(mockDisconnect).toHaveBeenCalled()
expect(mockObserve).toHaveBeenCalledWith(mockEl, { childList: true })
spy.mockRestore()
})
})