-
-
Notifications
You must be signed in to change notification settings - Fork 245
Expand file tree
/
Copy pathpoint.test.jsx
More file actions
204 lines (173 loc) 路 5.92 KB
/
Copy pathpoint.test.jsx
File metadata and controls
204 lines (173 loc) 路 5.92 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
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
import { act, cleanup, fireEvent, render } from '@testing-library/react';
import React from 'react';
import Trigger from '../src';
import { getMouseEvent } from './util';
/**
* dom-align internal default position is `-999`.
* We do not need to simulate full position, check offset only.
*/
describe('Trigger.Point', () => {
beforeEach(() => {
jest.useFakeTimers();
});
afterEach(() => {
cleanup();
jest.useRealTimers();
});
class Demo extends React.Component {
popup = (<div className="point-popup">POPUP</div>);
render() {
return (
<div
className="scroll"
// Jest can not get calculated style in jsdom. So we need to set it manually
style={{ overflowX: 'hidden', overflowY: 'hidden' }}
>
<Trigger
ref={this.props.triggerRef}
popup={this.popup}
popupAlign={{ points: ['tl'] }}
alignPoint
{...this.props}
>
<div className="point-region" />
</Trigger>
</div>
);
}
}
async function trigger(container, eventName, data) {
const pointRegion = container.querySelector('.point-region');
fireEvent(pointRegion, getMouseEvent(eventName, data));
// React scheduler will not hold when useEffect. We need repeat to tell that times go
for (let i = 0; i < 10; i += 1) {
await act(async () => {
jest.runAllTimers();
await Promise.resolve();
});
}
}
it('onClick', async () => {
const { container } = render(<Demo action={['click']} />);
await trigger(container, 'click', { clientX: 11, clientY: 28 });
const popup = document.querySelector('.rc-trigger-popup');
expect(popup).toHaveStyle({ left: '11px', top: '28px' });
});
it('hover', async () => {
const { container } = render(<Demo action={['hover']} />);
await trigger(container, 'mouseenter', { clientX: 10, clientY: 20 });
await trigger(container, 'mouseover', { clientX: 9, clientY: 3 });
const popup = document.querySelector('.rc-trigger-popup');
expect(popup).toHaveStyle({ left: '9px', top: '3px' });
});
describe('contextMenu', () => {
it('basic', async () => {
const { container } = render(
<Demo action={['contextMenu']} hideAction={['click']} />,
);
await trigger(container, 'contextmenu', { clientX: 10, clientY: 20 });
const popup = document.querySelector('.rc-trigger-popup');
expect(popup.style).toEqual(
expect.objectContaining({ left: '10px', top: '20px' }),
);
// Not trigger point update when close
const clickEvent = {};
const pagePropDefine = {
get: () => {
throw new Error('should not read when close');
},
};
Object.defineProperties(clickEvent, {
clientX: pagePropDefine,
clientY: pagePropDefine,
});
fireEvent(
container.querySelector('.point-region'),
getMouseEvent('click', clickEvent),
);
expect(document.querySelector('.rc-trigger-popup-hidden')).toBeTruthy();
});
// https://github.com/ant-design/ant-design/issues/17043
it('not prevent default', (done) => {
(async function () {
const { container } = render(
<Demo showAction={['contextMenu']} hideAction={['click']} />,
);
await trigger(container, 'contextmenu', { clientX: 10, clientY: 20 });
const popup = document.querySelector('.rc-trigger-popup');
expect(popup).toHaveStyle({ left: '10px', top: '20px' });
// Click to close
fireEvent(
document.querySelector('.rc-trigger-popup > *'),
getMouseEvent('click', {
preventDefault() {
done.fail();
},
}),
);
done();
})();
});
it('should hide popup when set alignPoint after scrolling', async () => {
const { container } = render(<Demo action={['contextMenu']} />);
await trigger(container, 'contextmenu', { clientX: 10, clientY: 20 });
const popup = document.querySelector('.rc-trigger-popup');
expect(popup.style).toEqual(
expect.objectContaining({ left: '10px', top: '20px' }),
);
const scrollDiv = container.querySelector('.scroll');
fireEvent.scroll(scrollDiv);
expect(document.querySelector('.rc-trigger-popup-hidden')).toBeTruthy();
});
it('should use the latest alignPoint value when scrolling', async () => {
const { container, rerender } = render(
<Demo action={['click']} alignPoint={false} />,
);
await trigger(container, 'click', { clientX: 10, clientY: 20 });
expect(document.querySelector('.rc-trigger-popup-hidden')).toBeFalsy();
rerender(<Demo action={['click']} alignPoint />);
fireEvent.scroll(container.querySelector('.scroll'));
expect(document.querySelector('.rc-trigger-popup-hidden')).toBeTruthy();
});
});
describe('placement', () => {
function testPlacement(name, builtinPlacements, afterAll) {
it(name, async () => {
const { container } = render(
<Demo
action={['click']}
builtinPlacements={builtinPlacements}
popupPlacement="right"
/>,
);
await trigger(container, 'click', { clientX: 10, clientY: 20 });
const popup = document.querySelector('.rc-trigger-popup');
expect(popup.style).toEqual(
expect.objectContaining({ left: '10px', top: '20px' }),
);
if (afterAll) {
afterAll(document.body);
}
});
}
testPlacement('not hit', {
right: {
// This should not hit
points: ['cl'],
},
});
testPlacement(
'hit builtin',
{
left: {
points: ['tl'],
},
},
(wrapper) => {
expect(wrapper.querySelector('div.rc-trigger-popup')).toHaveClass(
'rc-trigger-popup-placement-left',
);
},
);
});
});