-
-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
Copy pathindex.test.js
139 lines (127 loc) · 4.56 KB
/
index.test.js
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
import { asyncExpect } from '../../../tests/utils';
import message, { getInstance } from '..';
import SmileOutlined from '@ant-design/icons-vue/SmileOutlined';
describe('message', () => {
beforeEach(() => {
jest.useFakeTimers();
document.body.outerHTML = '';
});
afterEach(() => {
message.destroy();
});
afterEach(() => {
message.destroy();
jest.useRealTimers();
});
it('should be able to config top', async () => {
message.config({
top: '100px',
});
message.info('whatever');
await asyncExpect(() => {
expect(document.querySelectorAll('.ant-message')[0].style.top).toBe('100px');
});
});
it('should be able to config getContainer', () => {
message.config({
getContainer: () => {
const div = document.createElement('div');
div.className = 'custom-container';
document.body.appendChild(div);
return div;
},
});
message.info('whatever');
expect(document.querySelectorAll('.custom-container').length).toBe(1);
});
it('should be able to config maxCount', async () => {
message.config({
maxCount: 5,
});
for (let i = 0; i < 10; i += 1) {
message.info('test');
}
message.info('last');
await Promise.resolve();
jest.runAllTimers();
expect(document.querySelectorAll('.ant-message-notice').length).toBe(5);
expect(document.querySelectorAll('.ant-message-notice')[4].textContent).toBe('last');
});
it('should be able to hide manually', async () => {
const hide1 = message.info('whatever', 0);
const hide2 = message.info('whatever', 0);
await Promise.resolve();
expect(document.querySelectorAll('.ant-message-notice').length).toBe(2);
hide1();
jest.runAllTimers();
expect(getInstance().component.value.notices).toHaveLength(1);
hide2();
jest.runAllTimers();
expect(getInstance().component.value.notices).toHaveLength(0);
});
it('should be able to destroy globally', async () => {
message.info('whatever', 0);
message.info('whatever', 0);
await Promise.resolve();
expect(document.querySelectorAll('.ant-message').length).toBe(1);
expect(document.querySelectorAll('.ant-message-notice').length).toBe(2);
message.destroy();
expect(document.querySelectorAll('.ant-message').length).toBe(0);
expect(document.querySelectorAll('.ant-message-notice').length).toBe(0);
});
it('should not need to use duration argument when using the onClose arguments', () => {
message.info('whatever', () => {});
});
it('should have the default duration when using the onClose arguments', done => {
jest.useRealTimers();
const defaultDuration = 3;
const now = Date.now();
message.info('whatever', () => {
// calculate the approximately duration value
const aboutDuration = parseInt((Date.now() - now) / 1000, 10);
expect(aboutDuration).toBe(defaultDuration);
done();
});
});
it('should be called like promise', done => {
jest.useRealTimers();
const defaultDuration = 3;
const now = Date.now();
message.info('whatever').then(() => {
// calculate the approximately duration value
const aboutDuration = parseInt((Date.now() - now) / 1000, 10);
expect(aboutDuration).toBe(defaultDuration);
done();
});
});
// https:// github.com/ant-design/ant-design/issues/8201
it('should hide message correctly', async () => {
let hide = message.loading('Action in progress..', 0);
await Promise.resolve();
expect(document.querySelectorAll('.ant-message-notice').length).toBe(1);
hide();
await Promise.resolve();
jest.runAllTimers();
expect(document.querySelectorAll('.ant-message-notice').length).toBe(0);
});
it('should allow custom icon', async () => {
message.open({ content: 'Message', icon: <SmileOutlined /> });
await Promise.resolve();
expect(document.querySelectorAll('.anticon-smile').length).toBe(1);
});
it('should have no icon', async () => {
message.open({ content: 'Message' });
await Promise.resolve();
expect(document.querySelectorAll('.ant-message-notice .anticon').length).toBe(0);
});
// https://github.com/ant-design/ant-design/issues/8201
it('should destroy messages correctly', async () => {
message.loading('Action in progress1..', 0);
message.loading('Action in progress2..', 0);
setTimeout(() => message.destroy(), 1000);
await Promise.resolve();
expect(document.querySelectorAll('.ant-message-notice').length).toBe(2);
jest.runAllTimers();
expect(document.querySelectorAll('.ant-message-notice').length).toBe(0);
});
});