-
-
Notifications
You must be signed in to change notification settings - Fork 193
/
Copy pathindex.spec.tsx
736 lines (618 loc) · 21.3 KB
/
index.spec.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
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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
/* eslint-disable react/no-render-return-value, max-classes-per-file, func-names, no-console */
import { fireEvent, render, act } from '@testing-library/react';
import { Provider } from '@rc-component/motion';
import KeyCode from '@rc-component/util/lib/KeyCode';
import React, { cloneElement, useEffect } from 'react';
import type { DialogProps } from '../src';
import Dialog from '../src';
jest.mock('@rc-component/motion', () => {
const OriReact = jest.requireActual('react');
const origin = jest.requireActual('@rc-component/motion');
const OriCSSMotion = origin.default;
const ProxyCSSMotion = OriReact.forwardRef((props: any, ref: any) => {
global.onAppearPrepare = props.onAppearPrepare;
return <OriCSSMotion {...props} ref={ref} />;
});
return {
...origin,
default: ProxyCSSMotion,
__esModule: true,
};
});
describe('dialog', () => {
async function runFakeTimer() {
for (let i = 0; i < 100; i += 1) {
await act(async () => {
jest.advanceTimersByTime(100);
await Promise.resolve();
});
}
}
beforeEach(() => {
jest.useFakeTimers();
});
afterEach(() => {
jest.clearAllTimers();
jest.useRealTimers();
});
it('should render correct', () => {
render(<Dialog title="Default" visible />);
jest.runAllTimers();
expect(document.querySelector('.rc-dialog-root')).toMatchSnapshot();
});
it('add rootClassName and rootStyle should render correct', () => {
const spy = jest.spyOn(console, 'error').mockImplementation(() => {});
render(
<Dialog
visible
rootClassName="customize-root-class"
rootStyle={{ fontSize: 20 }}
style={{ width: 600 }}
height={903}
wrapStyle={{ fontSize: 10 }}
/>,
);
jest.runAllTimers();
expect(document.querySelector('.rc-dialog-root')).toMatchSnapshot();
expect(spy).toHaveBeenCalledWith(
`Warning: wrapStyle is deprecated, please use styles instead.`,
);
expect(document.querySelector('.customize-root-class')).toBeTruthy();
expect(document.querySelector('.rc-dialog-wrap')).toHaveStyle('fontSize: 10px');
expect(document.querySelector('.rc-dialog-root')).toHaveStyle('fontSize: 20px');
expect(document.querySelector('.rc-dialog')).toHaveStyle('height: 903px');
expect(document.querySelector('.rc-dialog')).toHaveStyle('width: 600px');
});
it('show', () => {
render(<Dialog visible />);
jest.runAllTimers();
expect(document.querySelector('.rc-dialog-wrap')).toHaveStyle('display: block');
});
it('close', () => {
const { rerender } = render(<Dialog visible />);
jest.runAllTimers();
rerender(<Dialog visible={false} />);
jest.runAllTimers();
expect(document.querySelector('.rc-dialog-wrap')).toHaveStyle('display: none');
});
it('create & root & mask', () => {
render(<Dialog visible />);
jest.runAllTimers();
expect(document.querySelector('.rc-dialog-root')).toBeTruthy();
expect(document.querySelector('.rc-dialog-mask')).toBeTruthy();
});
it('click close', () => {
const onClose = jest.fn();
render(<Dialog closeIcon="test" onClose={onClose} visible />);
jest.runAllTimers();
const btn = document.querySelector('.rc-dialog-close');
expect(btn.textContent).toBe('test');
fireEvent.click(btn);
jest.runAllTimers();
expect(onClose).toHaveBeenCalledTimes(1);
});
describe('destroyOnClose', () => {
it('default is false', () => {
const { rerender } = render(
<Dialog visible>
<input className="test-destroy" />
</Dialog>,
);
rerender(<Dialog visible={false} />);
jest.runAllTimers();
expect(document.querySelectorAll('.test-destroy')).toHaveLength(1);
});
it('destroy on hide should unmount child components on close', () => {
const Demo = (props?: Partial<DialogProps>) => (
<Dialog destroyOnClose {...props}>
<input className="test-input" />
</Dialog>
);
const { rerender } = render(<Demo />);
// Show
rerender(<Demo visible />);
act(() => {
jest.runAllTimers();
});
document.querySelector<HTMLInputElement>('.test-input').value = 'test';
expect(document.querySelector<HTMLInputElement>('.test-input')).toHaveValue('test');
// Hide
rerender(<Demo visible={false} />);
act(() => {
jest.runAllTimers();
});
expect(document.querySelector<HTMLInputElement>('.test-input')).toBeFalsy();
// Show
rerender(<Demo visible />);
act(() => {
jest.runAllTimers();
});
expect(document.querySelector<HTMLInputElement>('.test-input')).toHaveValue('');
});
});
it('esc to close', () => {
const onClose = jest.fn();
render(<Dialog onClose={onClose} visible />);
jest.runAllTimers();
fireEvent.keyDown(document.querySelector('.rc-dialog'), { keyCode: KeyCode.ESC });
jest.runAllTimers();
expect(onClose).toHaveBeenCalled();
});
it('mask to close', () => {
const onClose = jest.fn();
const { rerender } = render(<Dialog onClose={onClose} visible />);
// Mask close
fireEvent.click(document.querySelector('.rc-dialog-wrap'));
jest.runAllTimers();
expect(onClose).toHaveBeenCalled();
onClose.mockReset();
// Mask can not close
rerender(<Dialog onClose={onClose} visible maskClosable={false} />);
fireEvent.click(document.querySelector('.rc-dialog-wrap'));
jest.runAllTimers();
expect(onClose).not.toHaveBeenCalled();
});
it('renderToBody', () => {
const container = document.createElement('div');
document.body.appendChild(container);
render(
<Dialog visible>
<p className="renderToBody">1</p>
</Dialog>,
{ container },
);
act(() => {
jest.runAllTimers();
});
expect(container.querySelector('.rc-dialog')).toBeFalsy();
expect(document.body.querySelector('.rc-dialog')).toBeTruthy();
document.body.removeChild(container);
});
it('getContainer', () => {
const returnedContainer = document.createElement('div');
render(
<Dialog visible getContainer={() => returnedContainer}>
<p className="getContainer">Hello world!</p>
</Dialog>,
);
expect(returnedContainer.querySelector('.rc-dialog')).toBeTruthy();
});
it('render title correctly', () => {
render(<Dialog visible title="bamboo" />);
expect(document.querySelector('.rc-dialog-header').textContent).toBe('bamboo');
});
it('render footer correctly', () => {
render(<Dialog visible footer="test" />);
expect(document.querySelector('.rc-dialog-footer').textContent).toBe('test');
});
// 失效了,需要修复
it.skip('support input autoFocus', () => {
render(
<Dialog visible>
<input autoFocus />
</Dialog>,
);
expect(document.querySelector('input')).toHaveFocus();
});
describe('Tab should keep focus in dialog', () => {
it('basic tabbing', () => {
render(<Dialog visible />);
const sentinelEnd = document.querySelector<HTMLDivElement>('.rc-dialog > div:last-child');
sentinelEnd.focus();
fireEvent.keyDown(document.querySelector('.rc-dialog-wrap'), {
keyCode: KeyCode.TAB,
});
const sentinelStart = document.querySelector('.rc-dialog > div:first-child');
expect(document.activeElement).toBe(sentinelStart);
});
it('trap focus after shift-tabbing', () => {
render(<Dialog visible />);
document.querySelector<HTMLDivElement>('.rc-dialog > div:first-child').focus();
fireEvent.keyDown(document.querySelector('.rc-dialog-wrap'), {
keyCode: KeyCode.TAB,
key: 'Tab',
shiftKey: true,
});
const sentinelEnd = document.querySelector('.rc-dialog > div:last-child');
expect(document.activeElement).toBe(sentinelEnd);
});
});
describe('mousePosition', () => {
function prepareModal(mousePosition: { x: number; y: number }) {
const { container } = render(
<Dialog style={{ width: 600 }} mousePosition={mousePosition} visible>
<p>the dialog</p>
</Dialog>,
);
// Trigger position align
act(() => {
global.onAppearPrepare?.();
});
return container;
}
it('sets transform-origin when property mousePosition is set', () => {
prepareModal({ x: 100, y: 100 });
expect(
document.querySelector<HTMLElement>('.rc-dialog').style['transform-origin'],
).toBeTruthy();
});
it('both undefined', () => {
prepareModal({ x: undefined, y: undefined });
expect(
document.querySelector<HTMLElement>('.rc-dialog').style['transform-origin'],
).toBeFalsy();
});
it('one valid', () => {
prepareModal({ x: 10, y: 0 });
expect(
document.querySelector<HTMLElement>('.rc-dialog').style['transform-origin'],
).toBeTruthy();
});
});
it('can get dom element before dialog first show when forceRender is set true ', () => {
render(
<Dialog forceRender>
<div>forceRender element</div>
</Dialog>,
);
expect(document.querySelector('.rc-dialog-body > div').textContent).toEqual(
'forceRender element',
);
});
describe('getContainer is false', () => {
it('not set', () => {
const { container } = render(
<Dialog visible>
<div className="bamboo" />
</Dialog>,
);
expect(container.querySelector('.bamboo')).toBeFalsy();
expect(document.body.querySelector('.bamboo')).toBeTruthy();
});
it('set to false', () => {
const { container } = render(
<Dialog visible getContainer={false}>
<div className="bamboo" />
</Dialog>,
);
expect(container.querySelector('.bamboo')).toBeTruthy();
});
});
it('should not close if mouse down in dialog', () => {
const onClose = jest.fn();
render(<Dialog onClose={onClose} visible />);
fireEvent.click(document.querySelector('.rc-dialog-body'));
expect(onClose).not.toHaveBeenCalled();
});
it('zIndex', () => {
render(<Dialog visible zIndex={903} />);
expect(document.querySelector('.rc-dialog-wrap')).toHaveStyle('z-index: 903');
});
it('should show dialog when initialize dialog, given forceRender and visible is true', () => {
class DialogWrapTest extends React.Component {
state = {
visible: true,
forceRender: true,
};
render() {
return <Dialog forceRender={this.state.forceRender} visible={this.state.visible} />;
}
}
render(
<DialogWrapTest>
<div>Show dialog with forceRender and visible is true</div>
</DialogWrapTest>,
);
act(() => {
jest.runAllTimers();
});
expect(document.querySelector('.rc-dialog-wrap')).toHaveStyle('display: block');
});
it('modalRender', () => {
render(
<Dialog
visible
modalRender={(node: React.ReactElement) =>
cloneElement(node, { ...node.props, style: { background: '#1890ff' } })
}
/>,
);
expect(document.querySelector('.rc-dialog-section')).toHaveStyle('background: #1890ff');
});
describe('focusTriggerAfterClose', () => {
it('should focus trigger after close dialog', () => {
const Demo = () => {
const [visible, setVisible] = React.useState(false);
return (
<>
<button onClick={() => setVisible(true)}>trigger</button>
<Dialog visible={visible} onClose={() => setVisible(false)}>
content
</Dialog>
</>
);
};
render(<Demo />);
const trigger = document.querySelector('button');
trigger.focus();
fireEvent.click(trigger);
jest.runAllTimers();
const closeButton = document.querySelector('.rc-dialog-close');
fireEvent.click(closeButton);
jest.runAllTimers();
expect(document.activeElement).toBe(trigger);
});
it('should focus trigger after close dialog when contains focusable element', () => {
const Demo = () => {
const [visible, setVisible] = React.useState(false);
const inputRef = React.useRef(null);
useEffect(() => {
inputRef.current?.focus();
}, []);
return (
<>
<button onClick={() => setVisible(true)}>trigger</button>
<Dialog visible={visible} onClose={() => setVisible(false)}>
<input ref={inputRef} />
</Dialog>
</>
);
};
render(<Demo />);
const trigger = document.querySelector('button');
trigger.focus();
fireEvent.click(trigger);
jest.runAllTimers();
const closeButton = document.querySelector('.rc-dialog-close');
fireEvent.click(closeButton);
jest.runAllTimers();
expect(document.activeElement).toBe(trigger);
});
});
describe('size should work', () => {
it('width', () => {
render(<Dialog visible width={1128} />);
expect(document.querySelector('.rc-dialog')).toHaveStyle('width: 1128px');
});
it('height', () => {
render(<Dialog visible height={903} />);
expect(document.querySelector('.rc-dialog')).toHaveStyle('height: 903px');
});
});
describe('re-render', () => {
function createWrapper(
props?: Partial<DialogProps>,
): [
container: HTMLElement,
getRenderTimes: () => number,
updateProps: (props?: Partial<DialogProps>) => void,
] {
let renderTimes = 0;
const RenderChecker = () => {
renderTimes += 1;
return null;
};
const Demo = (demoProps?: any) => {
return (
<Dialog visible {...props} {...demoProps}>
<RenderChecker />
</Dialog>
);
};
const { container, rerender } = render(<Demo />);
return [
container,
() => renderTimes,
(nextProps) => {
rerender(<Demo {...nextProps} />);
},
];
}
it('should not re-render when visible changed', () => {
const [, getRenderTimes, updateProps] = createWrapper();
const lastRenderTimes = getRenderTimes();
expect(getRenderTimes()).toBeGreaterThan(0);
// Hidden should not trigger render
updateProps({ visible: false });
expect(getRenderTimes()).toEqual(lastRenderTimes);
});
it('should re-render when forceRender', () => {
const [, getRenderTimes, updateProps] = createWrapper({ forceRender: true });
const lastRenderTimes = getRenderTimes();
expect(getRenderTimes()).toBeGreaterThan(0);
// Hidden should not trigger render
updateProps({ visible: false });
expect(getRenderTimes()).toBeGreaterThan(lastRenderTimes);
});
});
describe('afterClose', () => {
it('should trigger afterClose when set visible to false', () => {
const afterClose = jest.fn();
const { rerender } = render(<Dialog afterClose={afterClose} visible />);
act(() => {
jest.runAllTimers();
});
rerender(<Dialog afterClose={afterClose} visible={false} />);
act(() => {
jest.runAllTimers();
});
expect(afterClose).toHaveBeenCalledTimes(1);
});
it('should not trigger afterClose when mount dialog of getContainer={false}', () => {
const afterClose = jest.fn();
const { container } = render(<Dialog afterClose={afterClose} getContainer={false} />);
jest.runAllTimers();
render(<Dialog afterClose={afterClose} getContainer={false} visible={false} />, {
container,
});
jest.runAllTimers();
expect(afterClose).toHaveBeenCalledTimes(0);
});
it('should not trigger afterClose when mount dialog of forceRender={true}', () => {
const afterClose = jest.fn();
const { container } = render(<Dialog afterClose={afterClose} forceRender />);
jest.runAllTimers();
render(<Dialog afterClose={afterClose} forceRender visible={false} />, { container });
jest.runAllTimers();
expect(afterClose).toHaveBeenCalledTimes(0);
});
});
describe('afterOpenChange', () => {
beforeEach(() => {
jest.useFakeTimers();
});
afterEach(() => {
jest.clearAllTimers();
jest.useRealTimers();
});
it('should trigger afterOpenChange when visible changed', async () => {
const afterOpenChange = jest.fn();
const Demo = (props: any) => (
<Provider motion={false}>
<Dialog afterOpenChange={afterOpenChange} {...props} />
</Provider>
);
const { rerender } = render(<Demo visible />);
await runFakeTimer();
expect(afterOpenChange).toHaveBeenCalledWith(true);
expect(afterOpenChange).toHaveBeenCalledTimes(1);
rerender(<Demo visible={false} />);
await runFakeTimer();
expect(afterOpenChange).toHaveBeenCalledWith(false);
expect(afterOpenChange).toHaveBeenCalledTimes(2);
});
});
it('should support classNames', () => {
render(
<Dialog
visible
title="Default"
footer="Footer"
classNames={{
header: 'custom-header',
body: 'custom-body',
footer: 'custom-footer',
mask: 'custom-mask',
wrapper: 'custom-wrapper',
section: 'custom-section',
}}
style={{ width: 600 }}
height={903}
/>,
);
jest.runAllTimers();
expect(document.querySelector('.rc-dialog-root')).toMatchSnapshot();
expect(document.querySelector('.rc-dialog-wrap').className).toContain('custom-wrapper');
expect(document.querySelector('.rc-dialog-body').className).toContain('custom-body');
expect(document.querySelector('.rc-dialog-header').className).toContain('custom-header');
expect(document.querySelector('.rc-dialog-footer').className).toContain('custom-footer');
expect(document.querySelector('.rc-dialog-mask').className).toContain('custom-mask');
expect(document.querySelector('.rc-dialog-section').className).toContain('custom-section');
});
it('should support styles', () => {
render(
<Dialog
visible
title="Default"
footer="Footer"
styles={{
header: { background: 'red' },
body: { background: 'green' },
footer: { background: 'blue' },
mask: { background: 'yellow' },
wrapper: { background: 'pink' },
section: { background: 'orange' },
title: { background: 'orange' },
}}
style={{ width: 600 }}
height={903}
/>,
);
jest.runAllTimers();
expect(document.querySelector('.rc-dialog-root')).toMatchSnapshot();
expect(document.querySelector('.rc-dialog-wrap')).toHaveStyle('background: pink');
expect(document.querySelector('.rc-dialog-body')).toHaveStyle('background: green');
expect(document.querySelector('.rc-dialog-header')).toHaveStyle('background: red');
expect(document.querySelector('.rc-dialog-footer')).toHaveStyle('background: blue');
expect(document.querySelector('.rc-dialog-mask')).toHaveStyle('background: yellow');
expect(document.querySelector('.rc-dialog-section')).toHaveStyle('background: orange');
expect(document.querySelector('.rc-dialog-title')).toHaveStyle('background: orange');
});
it('should warning', () => {
const spy = jest.spyOn(console, 'error').mockImplementation(() => {});
render(
<Dialog
visible
title="Default"
footer="Footer"
bodyStyle={{ background: 'green' }}
maskStyle={{ background: 'yellow' }}
wrapClassName="custom-wrapper"
style={{ width: 600 }}
height={903}
/>,
);
jest.runAllTimers();
expect(spy).toHaveBeenCalledWith(
`Warning: bodyStyle is deprecated, please use styles instead.`,
);
expect(spy).toHaveBeenCalledWith(
`Warning: maskStyle is deprecated, please use styles instead.`,
);
expect(spy).toHaveBeenCalledWith(
`Warning: wrapClassName is deprecated, please use classNames instead.`,
);
spy.mockRestore();
});
it('support aria-* in closable', () => {
const onClose = jest.fn();
render(
<Dialog
closable={{
closeIcon: 'test',
'aria-label': 'test aria-label',
}}
visible
onClose={onClose}
/>,
);
jest.runAllTimers();
const btn = document.querySelector('.rc-dialog-close');
expect(btn.textContent).toBe('test');
expect(btn.getAttribute('aria-label')).toBe('test aria-label');
fireEvent.click(btn);
jest.runAllTimers();
expect(onClose).toHaveBeenCalledTimes(1);
});
it('support disable button in closable', () => {
const onClose = jest.fn();
render(
<Dialog
closable={{
closeIcon: 'test',
disabled: true,
}}
visible
onClose={onClose}
/>,
);
jest.runAllTimers();
const btn = document.querySelector<HTMLButtonElement>('.rc-dialog-close');
expect(btn.disabled).toBeTruthy();
fireEvent.click(btn);
jest.runAllTimers();
expect(onClose).not.toHaveBeenCalled();
fireEvent.keyDown(btn, { key: 'Enter' });
jest.runAllTimers();
expect(onClose).not.toHaveBeenCalled();
});
it('should not display closeIcon when closable is false', () => {
const onClose = jest.fn();
render(<Dialog closable={false} visible onClose={onClose} />);
act(() => {
jest.runAllTimers();
});
expect(document.querySelector('.rc-dialog')).toBeTruthy();
expect(document.querySelector('.rc-dialog-close')).toBeFalsy();
});
});