forked from vuejs/composition-api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsetup.spec.js
426 lines (399 loc) · 9.39 KB
/
setup.spec.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
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
const Vue = require('vue/dist/vue.common.js');
const { ref, computed, createElement: h, provide, inject } = require('../src');
describe('setup', () => {
beforeEach(() => {
warn = jest.spyOn(global.console, 'error').mockImplementation(() => null);
});
afterEach(() => {
warn.mockRestore();
});
it('should works', () => {
const vm = new Vue({
setup() {
return {
a: ref(1),
};
},
}).$mount();
expect(vm.a).toBe(1);
});
it('should be overrided by data option of plain object', () => {
const vm = new Vue({
setup() {
return {
a: ref(1),
};
},
data: {
a: 2,
},
}).$mount();
expect(vm.a).toBe(2);
});
it("should access setup's value in data", () => {
const vm = new Vue({
setup() {
return {
a: ref(1),
};
},
data() {
return {
b: this.a,
};
},
}).$mount();
expect(vm.a).toBe(1);
expect(vm.b).toBe(1);
});
it('should work with `methods` and `data` options', done => {
let calls = 0;
const vm = new Vue({
template: `<div>{{a}}{{b}}{{c}}</div>`,
setup() {
return {
a: ref(1),
};
},
beforeUpdate() {
calls++;
},
created() {
this.m();
},
data() {
return {
b: this.a,
c: 0,
};
},
methods: {
m() {
this.c = this.a;
},
},
}).$mount();
expect(vm.a).toBe(1);
expect(vm.b).toBe(1);
expect(vm.c).toBe(1);
vm.a = 2;
waitForUpdate(() => {
expect(calls).toBe(1);
expect(vm.a).toBe(2);
expect(vm.b).toBe(1);
expect(vm.c).toBe(1);
vm.b = 2;
})
.then(() => {
expect(calls).toBe(2);
expect(vm.a).toBe(2);
expect(vm.b).toBe(2);
expect(vm.c).toBe(1);
})
.then(done);
});
it('should receive props as first params', () => {
let props;
new Vue({
props: ['a'],
setup(_props) {
props = _props;
},
propsData: {
a: 1,
},
}).$mount();
expect(props.a).toBe(1);
});
it('warn for existing props', () => {
new Vue({
props: {
a: {},
},
setup() {
const a = ref();
return {
a,
};
},
});
expect(warn.mock.calls[0][0]).toMatch(
'[Vue warn]: The setup binding property "a" is already declared as a prop.'
);
});
it('warn for existing instance properties', () => {
new Vue({
setup(_, { _vm }) {
_vm.a = 1;
return {
a: ref(),
};
},
});
expect(warn.mock.calls[0][0]).toMatch(
'[Vue warn]: The setup binding property "a" is already declared.'
);
});
it('should merge result properly', () => {
const injectKey = Symbol('foo');
const A = Vue.extend({
setup() {
provide(injectKey, 'foo');
return { a: 1 };
},
});
const Test = Vue.extend({
extends: A,
setup() {
const injectVal = inject(injectKey);
return {
injectVal,
};
},
});
let vm = new Test({
setup() {
return { b: 2 };
},
});
expect(vm.a).toBe(1);
expect(vm.b).toBe(2);
expect(vm.injectVal).toBe('foo');
// no instance data
vm = new Test();
expect(vm.a).toBe(1);
// no child-val
const Extended = Test.extend({});
vm = new Extended();
expect(vm.a).toBe(1);
// recursively merge objects
const WithObject = Vue.extend({
setup() {
return {
obj: {
a: 1,
},
};
},
});
vm = new WithObject({
setup() {
return {
obj: {
b: 2,
},
};
},
});
expect(vm.obj.a).toBe(1);
expect(vm.obj.b).toBe(2);
});
it('should have access to props', () => {
const Test = {
props: ['a'],
render() {},
setup(props) {
return {
b: props.a,
};
},
};
const vm = new Vue({
template: `<test ref="test" :a="1"></test>`,
components: { Test },
}).$mount();
expect(vm.$refs.test.b).toBe(1);
});
it('props should not be reactive', done => {
let calls = 0;
const vm = new Vue({
template: `<child :msg="msg"></child>`,
setup() {
return { msg: ref('hello') };
},
beforeUpdate() {
calls++;
},
components: {
child: {
template: `<span>{{ localMsg }}</span>`,
props: ['msg'],
setup(props) {
return { localMsg: props.msg, computedMsg: computed(() => props.msg + ' world') };
},
},
},
}).$mount();
const child = vm.$children[0];
expect(child.localMsg).toBe('hello');
expect(child.computedMsg).toBe('hello world');
expect(calls).toBe(0);
vm.msg = 'hi';
waitForUpdate(() => {
expect(child.localMsg).toBe('hello');
expect(child.computedMsg).toBe('hi world');
expect(calls).toBe(1);
}).then(done);
});
it('this should be undefined', () => {
const vm = new Vue({
template: '<div></div>',
setup() {
expect(this).toBe(global);
},
}).$mount();
});
it('should not make returned non-reactive object reactive', done => {
const vm = new Vue({
setup() {
return {
form: {
a: 1,
b: 2,
},
};
},
template: '<div>{{ form.a }}, {{ form.b }}</div>',
}).$mount();
expect(vm.$el.textContent).toBe('1, 2');
// should not trigger a re-render
vm.form.a = 2;
waitForUpdate(() => {
expect(vm.$el.textContent).toBe('1, 2');
// should trigger a re-render
vm.form = { a: 2, b: 3 };
})
.then(() => {
expect(vm.$el.textContent).toBe('2, 3');
})
.then(done);
});
it("should put a unenumerable '__ob__' for non-reactive object", () => {
const clone = obj => JSON.parse(JSON.stringify(obj));
const componentSetup = jest.fn(props => {
const internalOptions = clone(props.options);
return { internalOptions };
});
const ExternalComponent = {
props: ['options'],
setup: componentSetup,
};
new Vue({
components: { ExternalComponent },
setup: () => ({ options: {} }),
template: `<external-component :options="options"></external-component>`,
}).$mount();
expect(componentSetup).toReturn();
});
it('current vue should exist in nested setup call', () => {
const spy = jest.fn();
new Vue({
setup() {
new Vue({
setup() {
spy(1);
},
});
spy(2);
},
});
expect(spy.mock.calls.length).toBe(2);
expect(spy).toHaveBeenNthCalledWith(1, 1);
expect(spy).toHaveBeenNthCalledWith(2, 2);
});
it('inline render function should receive proper params', () => {
let p;
const vm = new Vue({
template: `<child msg="foo" a="1" b="2"></child>`,
components: {
child: {
name: 'child',
props: ['msg'],
setup() {
return props => {
p = props;
return null;
};
},
},
},
}).$mount();
expect(p).toBe(undefined);
});
it('inline render function should work', done => {
// let createElement;
const vm = new Vue({
props: ['msg'],
template: '<div>1</div>',
setup(props) {
const count = ref(0);
const increment = () => {
count.value++;
};
return () =>
h('div', [
h('span', props.msg),
h(
'button',
{
on: {
click: increment,
},
},
count.value
),
]);
},
propsData: {
msg: 'foo',
},
}).$mount();
expect(vm.$el.querySelector('span').textContent).toBe('foo');
expect(vm.$el.querySelector('button').textContent).toBe('0');
vm.$el.querySelector('button').click();
waitForUpdate(() => {
expect(vm.$el.querySelector('button').textContent).toBe('1');
vm.msg = 'bar';
})
.then(() => {
expect(vm.$el.querySelector('span').textContent).toBe('bar');
})
.then(done);
});
describe('Methods', () => {
it('binds methods when calling with parenthesis', async ()=>{
let context = null;
const contextFunction = jest.fn(function (){
context = this
});
const vm = new Vue({
template: '<div><button @click="contextFunction()"/></div>',
setup() {
return {
contextFunction
}
}
}).$mount();
await vm.$el.querySelector('button').click();
expect(contextFunction).toBeCalled();
expect(context).toBe(vm);
});
it('binds methods when calling without parenthesis', async () => {
let context = null;
const contextFunction = jest.fn(function (){
context = this
});
const vm = new Vue({
template: '<div><button @click="contextFunction"/></div>',
setup() {
return {
contextFunction
}
}
}).$mount();
await vm.$el.querySelector('button').click();
expect(contextFunction).toBeCalled();
expect(context).toBe(vm);
});
})
});