message-box.spec.js
7.5 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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
import MessageBox from 'packages/message-box';
describe('MessageBox', () => {
let hasPromise = true;
before(() => {
if (!window.Promise) {
hasPromise = false;
window.Promise = require('es6-promise').Promise;
}
});
after(() => {
if (!hasPromise) {
window.Promise = undefined;
}
});
afterEach(() => {
const el = document.querySelector('.el-message-box__wrapper');
if (!el) return;
if (el.parentNode) {
el.parentNode.removeChild(el);
}
MessageBox.close();
});
it('create and close', done => {
MessageBox({
type: 'success',
title: '消息',
message: '这是一段内容'
});
setTimeout(() => {
const msgbox = document.querySelector('.el-message-box__wrapper');
expect(msgbox.__vue__.$parent.visible).to.true;
expect(msgbox.querySelector('.el-message-box__title span').textContent).to.equal('消息');
expect(msgbox.querySelector('.el-message-box__message')
.querySelector('p').textContent).to.equal('这是一段内容');
MessageBox.close();
expect(msgbox.__vue__.$parent.visible).to.false;
done();
}, 300);
});
it('invoke with strings', done => {
MessageBox('消息', '这是一段内容', 'success');
setTimeout(() => {
expect(document.querySelector('.el-message-box__wrapper')).to.exist;
done();
}, 300);
});
it('custom icon', done => {
MessageBox({
type: 'warning',
iconClass: 'el-icon-question',
message: '这是一段内容'
});
setTimeout(() => {
const icon = document.querySelector('.el-message-box__status');
expect(icon.classList.contains('el-icon-question')).to.true;
done();
}, 300);
});
it('html string', done => {
MessageBox({
title: 'html string',
dangerouslyUseHTMLString: true,
message: '<strong>html string</strong>'
});
setTimeout(() => {
const message = document.querySelector('.el-message-box__message strong');
expect(message.textContent).to.equal('html string');
done();
}, 300);
});
it('distinguish cancel and close', done => {
let msgAction = '';
MessageBox({
title: '消息',
message: '这是一段内容',
distinguishCancelAndClose: true
}, action => {
msgAction = action;
});
setTimeout(() => {
document.querySelector('.el-message-box__close').click();
setTimeout(() => {
expect(msgAction).to.equal('close');
done();
}, 10);
}, 10);
});
it('alert', done => {
MessageBox.alert('这是一段内容', {
title: '标题名称',
type: 'warning'
});
setTimeout(() => {
document.querySelector('.v-modal').click();
expect(document.querySelector('.el-message-box__wrapper')
.__vue__.$parent.visible).to.true;
expect(document.querySelector('.el-message-box__wrapper')
.__vue__.$parent.type).to.equal('warning');
done();
}, 300);
});
it('confirm', done => {
MessageBox.confirm('这是一段内容', {
title: '标题名称',
type: 'warning'
});
setTimeout(() => {
document.querySelector('.el-message-box__wrapper')
.querySelector('.el-button--primary').click();
expect(document.querySelector('.el-message-box__wrapper')
.__vue__.$parent.visible).to.false;
done();
}, 200);
});
it('prompt', done => {
MessageBox.prompt('这是一段内容', {
title: '标题名称',
inputPattern: /test/,
inputErrorMessage: 'validation failed'
});
setTimeout(() => {
const messageBox = document.querySelector('.el-message-box__wrapper').__vue__.$parent;
expect(messageBox.$el.querySelector('.el-message-box__input')).to.exist;
const haveFocus = messageBox.$el.querySelector('input').isSameNode(document.activeElement);
expect(haveFocus).to.true;
messageBox.inputValue = 'no';
setTimeout(() => {
expect(messageBox.$el.querySelector('.el-message-box__errormsg')
.textContent).to.equal('validation failed');
done();
}, 100);
}, 700);
});
it('prompt: focus on textarea', done => {
MessageBox.prompt('这是一段内容', {
inputType: 'textarea',
title: '标题名称'
});
setTimeout(() => {
const messageBox = document.querySelector('.el-message-box__wrapper').__vue__.$parent;
const haveFocus = messageBox.$el.querySelector('textarea').isSameNode(document.activeElement);
expect(haveFocus).to.true;
done();
}, 700);
});
describe('custom validator', () => {
it('returns a string', done => {
const validator = value => {
if (value !== 'test') return 'wrong';
return true;
};
MessageBox.prompt('这是一段内容', {
title: '标题名称',
inputValidator: validator
});
setTimeout(() => {
const messageBox = document.querySelector('.el-message-box__wrapper').__vue__.$parent;
messageBox.inputValue = 'no';
setTimeout(() => {
expect(document.querySelector('.el-message-box__errormsg')
.textContent).to.equal('wrong');
messageBox.inputValue = 'test';
setTimeout(() => {
expect(document.querySelector('.el-message-box__errormsg')
.textContent).to.equal('');
done();
}, 100);
}, 100);
}, 200);
});
it('returns false', done => {
const validator = value => false;
MessageBox.prompt('这是一段内容', {
title: '标题名称',
inputValidator: validator
});
setTimeout(() => {
const messageBox = document.querySelector('.el-message-box__wrapper').__vue__.$parent;
messageBox.inputValue = 'no';
setTimeout(() => {
expect(document.querySelector('.el-message-box__errormsg')
.textContent).to.equal('输入的数据不合法!');
done();
}, 100);
}, 200);
});
});
it('callback', done => {
let msgAction = '';
MessageBox({
title: '消息',
message: '这是一段内容'
}, action => {
msgAction = action;
});
setTimeout(() => {
document.querySelector('.el-message-box__close').click();
setTimeout(() => {
expect(msgAction).to.equal('cancel');
done();
}, 10);
}, 10);
});
it('beforeClose', done => {
let msgAction = '';
MessageBox({
title: '消息',
message: '这是一段内容',
beforeClose: (action, instance) => {
instance.close();
}
}, action => {
msgAction = action;
});
setTimeout(() => {
document.querySelector('.el-message-box__wrapper .el-button--primary').click();
setTimeout(() => {
expect(msgAction).to.equal('confirm');
done();
}, 10);
}, 10);
});
describe('promise', () => {
it('resolve', done => {
MessageBox.confirm('此操作将永久删除该文件, 是否继续?', '提示')
.then(action => {
expect(action).to.equal('confirm');
done();
});
setTimeout(() => {
document.querySelector('.el-message-box__wrapper .el-button--primary').click();
}, 50);
});
it('reject', done => {
MessageBox.confirm('此操作将永久删除该文件, 是否继续?', '提示')
.catch(action => {
expect(action).to.equal('cancel');
done();
});
setTimeout(() => {
document.querySelector('.el-message-box__wrapper .el-button').click();
}, 50);
});
});
});