-
Notifications
You must be signed in to change notification settings - Fork 119
Expand file tree
/
Copy pathstack.test.tsx
More file actions
246 lines (214 loc) · 7.35 KB
/
stack.test.tsx
File metadata and controls
246 lines (214 loc) · 7.35 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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
import { useNotification } from '../src';
import { act, fireEvent, render } from '@testing-library/react';
import React from 'react';
require('../assets/index.less');
describe('stack', () => {
it('support stack', () => {
const Demo = () => {
const [api, holder] = useNotification({
stack: { threshold: 3 },
});
return (
<>
<button
type="button"
onClick={() => {
api.open({
content: <div className="context-content">Test</div>,
duration: false,
});
}}
/>
{holder}
</>
);
};
const { container } = render(<Demo />);
for (let i = 0; i < 3; i++) {
fireEvent.click(container.querySelector('button'));
}
expect(document.querySelectorAll('.rc-notification-notice')).toHaveLength(3);
expect(document.querySelector('.rc-notification-stack')).toBeTruthy();
expect(document.querySelector('.rc-notification-stack-expanded')).toBeTruthy();
for (let i = 0; i < 2; i++) {
fireEvent.click(container.querySelector('button'));
}
expect(document.querySelectorAll('.rc-notification-notice')).toHaveLength(5);
expect(document.querySelector('.rc-notification-stack-expanded')).toBeFalsy();
fireEvent.mouseEnter(document.querySelector('.rc-notification-notice'));
expect(document.querySelector('.rc-notification-stack-expanded')).toBeTruthy();
});
it('should collapse when amount is less than threshold', () => {
const Demo = () => {
const [api, holder] = useNotification({
stack: { threshold: 3 },
});
return (
<>
<button
type="button"
onClick={() => {
api.open({
content: <div className="context-content">Test</div>,
duration: false,
closable: true,
});
}}
/>
{holder}
</>
);
};
const { container } = render(<Demo />);
for (let i = 0; i < 5; i++) {
fireEvent.click(container.querySelector('button'));
}
expect(document.querySelectorAll('.rc-notification-notice')).toHaveLength(5);
expect(document.querySelector('.rc-notification-stack')).toBeTruthy();
expect(document.querySelector('.rc-notification-stack-expanded')).toBeFalsy();
fireEvent.mouseEnter(document.querySelector('.rc-notification-notice'));
expect(document.querySelector('.rc-notification-stack-expanded')).toBeTruthy();
fireEvent.click(document.querySelector('.rc-notification-notice-close'));
expect(document.querySelectorAll('.rc-notification-notice')).toHaveLength(4);
expect(document.querySelector('.rc-notification-stack-expanded')).toBeTruthy();
// mouseleave will not triggerred if notice is closed
fireEvent.mouseEnter(document.querySelector('.rc-notification-notice-wrapper'));
fireEvent.mouseLeave(document.querySelector('.rc-notification-notice-wrapper'));
expect(document.querySelector('.rc-notification-stack-expanded')).toBeFalsy();
});
});
describe('hover state after closing notice in stack', () => {
beforeEach(() => {
vi.useFakeTimers();
});
afterEach(() => {
vi.useRealTimers();
});
it('should clear hover state and resume timers when closing a hovered notice', () => {
const onClose = vi.fn();
const Demo = () => {
const [api, holder] = useNotification({
stack: { threshold: 3 },
});
return (
<>
<button
type="button"
onClick={() => {
api.open({
content: <div className="context-content">Test</div>,
duration: 1,
closable: true,
onClose,
});
}}
/>
{holder}
</>
);
};
const { container } = render(<Demo />);
for (let i = 0; i < 4; i++) {
act(() => {
fireEvent.click(container.querySelector('button'));
});
}
expect(document.querySelectorAll('.rc-notification-notice')).toHaveLength(4);
act(() => {
document.dispatchEvent(new MouseEvent('mousemove', { clientX: 100, clientY: 100 }));
});
// Hover the topmost notification wrapper
const wrappers = document.querySelectorAll('.rc-notification-notice-wrapper');
act(() => {
fireEvent.mouseEnter(wrappers[wrappers.length - 1]);
});
// Timers should be paused while hovering
act(() => {
vi.advanceTimersByTime(5000);
});
expect(document.querySelectorAll('.rc-notification-notice')).toHaveLength(4);
// Close the hovered notification via close button
act(() => {
const closeButtons = document.querySelectorAll('.rc-notification-notice-close');
fireEvent.click(closeButtons[closeButtons.length - 1]);
});
expect(document.querySelectorAll('.rc-notification-notice')).toHaveLength(3);
// Flush requestAnimationFrame so hover state recalculation takes effect
act(() => {
vi.advanceTimersByTime(100);
});
// Remaining notices should auto-close since hover state was properly cleared
act(() => {
vi.advanceTimersByTime(2000);
});
expect(document.querySelectorAll('.rc-notification-notice')).toHaveLength(0);
expect(onClose).toHaveBeenCalledTimes(4);
});
it('should keep hover state when mouse is still over a notice after close', () => {
const mockRect = {
top: 0,
left: 0,
bottom: 200,
right: 300,
width: 300,
height: 200,
x: 0,
y: 0,
toJSON: () => {},
};
const spy = vi
.spyOn(Element.prototype, 'getBoundingClientRect')
.mockReturnValue(mockRect as DOMRect);
const Demo = () => {
const [api, holder] = useNotification({
stack: { threshold: 3 },
});
return (
<>
<button
type="button"
onClick={() => {
api.open({
content: <div className="context-content">Test</div>,
duration: 1,
closable: true,
});
}}
/>
{holder}
</>
);
};
const { container } = render(<Demo />);
for (let i = 0; i < 4; i++) {
act(() => {
fireEvent.click(container.querySelector('button'));
});
}
expect(document.querySelectorAll('.rc-notification-notice')).toHaveLength(4);
// Mouse position inside the mocked bounding rect
act(() => {
document.dispatchEvent(new MouseEvent('mousemove', { clientX: 100, clientY: 100 }));
});
const wrappers = document.querySelectorAll('.rc-notification-notice-wrapper');
act(() => {
fireEvent.mouseEnter(wrappers[wrappers.length - 1]);
});
// Close the hovered notification
act(() => {
const closeButtons = document.querySelectorAll('.rc-notification-notice-close');
fireEvent.click(closeButtons[closeButtons.length - 1]);
});
expect(document.querySelectorAll('.rc-notification-notice')).toHaveLength(3);
// Flush RAF - mouse is within bounding rect so hover state should persist
act(() => {
vi.advanceTimersByTime(100);
});
// Timers should still be paused because mouse is detected over a notice
act(() => {
vi.advanceTimersByTime(5000);
});
expect(document.querySelectorAll('.rc-notification-notice')).toHaveLength(3);
spy.mockRestore();
});
});