/
Portal.test.tsx
157 lines (125 loc) · 3.81 KB
/
Portal.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
import React from 'react';
import { mount } from 'enzyme';
import { act } from 'react-dom/test-utils';
import PortalWrapper, { getOpenCount } from '../src/PortalWrapper';
import Portal from '../src/Portal';
describe('Portal', () => {
let container: HTMLDivElement;
// Mock for raf
window.requestAnimationFrame = callback => window.setTimeout(callback);
window.cancelAnimationFrame = id => window.clearTimeout(id);
beforeEach(() => {
container = document.createElement('div');
document.body.appendChild(container);
});
afterEach(() => {
document.body.removeChild(container);
});
it('forceRender', () => {
const divRef = React.createRef<any>();
const wrapper = mount(
<PortalWrapper forceRender>
{() => <div ref={divRef}>2333</div>}
</PortalWrapper>,
);
expect(divRef.current).toBeTruthy();
wrapper.unmount();
});
it('didUpdate', () => {
const didUpdate = jest.fn();
const wrapper = mount(
<Portal
didUpdate={didUpdate}
getContainer={() => document.createElement('div')}
>
light
</Portal>,
);
expect(didUpdate).toHaveBeenCalledTimes(1);
wrapper.setProps({ justForceUpdate: true });
expect(didUpdate).toHaveBeenCalledTimes(2);
});
describe('getContainer', () => {
it('string', () => {
const div = document.createElement('div');
div.id = 'bamboo-light';
document.body.appendChild(div);
mount(
<PortalWrapper visible getContainer="#bamboo-light">
{() => <div>2333</div>}
</PortalWrapper>,
);
expect(document.querySelector('#bamboo-light').childElementCount).toEqual(
1,
);
document.body.removeChild(div);
});
it('function', () => {
const div = document.createElement('div');
mount(
<PortalWrapper visible getContainer={() => div}>
{() => <div>2333</div>}
</PortalWrapper>,
);
expect(div.childElementCount).toEqual(1);
});
it('htmlElement', () => {
const div = document.createElement('div');
mount(
<PortalWrapper visible getContainer={div}>
{() => <div>2333</div>}
</PortalWrapper>,
);
expect(div.childElementCount).toEqual(1);
});
it('delay', () => {
jest.useFakeTimers();
const divRef = React.createRef<HTMLDivElement>();
const wrapper = mount(
<div>
<PortalWrapper visible getContainer={() => divRef.current}>
{() => <div />}
</PortalWrapper>
<div ref={divRef} />
</div>,
);
act(() => {
jest.runAllTimers();
wrapper.update();
});
expect(divRef.current.childElementCount).toEqual(1);
jest.useRealTimers();
});
});
it('openCount', () => {
const Demo = ({ count, visible }: { count: number; visible: boolean }) => {
return (
<>
{new Array(count).fill(null).map((_, index) => (
<PortalWrapper key={index} visible={visible}>
{() => <div>2333</div>}
</PortalWrapper>
))}
</>
);
};
const wrapper = mount(<Demo count={1} visible />);
expect(getOpenCount()).toEqual(1);
wrapper.setProps({ count: 2 });
expect(getOpenCount()).toEqual(2);
wrapper.setProps({ count: 1 });
expect(getOpenCount()).toEqual(1);
wrapper.setProps({ visible: false });
expect(getOpenCount()).toEqual(0);
});
it('wrapperClassName', () => {
const wrapper = mount(
<PortalWrapper visible wrapperClassName="bamboo">
{() => <div />}
</PortalWrapper>,
);
expect((wrapper.instance() as any).container.className).toEqual('bamboo');
wrapper.setProps({ wrapperClassName: 'light' });
expect((wrapper.instance() as any).container.className).toEqual('light');
});
});