forked from vuejs/composition-api
/
templateRefs.spec.js
131 lines (126 loc) · 2.83 KB
/
templateRefs.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
const Vue = require('vue/dist/vue.common.js')
const { ref, watchEffect, nextTick } = require('../src')
describe('ref', () => {
it('should work', (done) => {
let dummy
const vm = new Vue({
setup() {
const ref1 = ref(null)
watchEffect(() => {
dummy = ref1.value
})
return {
bar: ref1,
}
},
template: `<div>
<test ref="bar"></test>
</div>`,
components: {
test: {
id: 'test',
template: '<div>test</div>',
},
},
}).$mount()
vm.$nextTick()
.then(() => {
expect(dummy).toBe(vm.$refs.bar)
})
.then(done)
})
it('should dynamically update refs', (done) => {
const vm = new Vue({
setup() {
const ref1 = ref(null)
const ref2 = ref(null)
watchEffect(() => {
dummy1 = ref1.value
dummy2 = ref2.value
})
return {
value: 'bar',
bar: ref1,
foo: ref2,
}
},
template: '<div :ref="value"></div>',
}).$mount()
waitForUpdate(() => {})
.then(() => {
expect(dummy1).toBe(vm.$refs.bar)
expect(dummy2).toBe(null)
vm.value = 'foo'
})
.then(() => {
// vm updated. ref update occures after updated;
})
.then(() => {
// no render cycle, empty tick
})
.then(() => {
expect(dummy1).toBe(null)
expect(dummy2).toBe(vm.$refs.foo)
})
.then(done)
})
// #296
it('should dynamically update refs in context', async () => {
const vm = new Vue({
setup() {
const barRef = ref(null)
return {
barRef,
}
},
template: `<div>
<foo><bar ref="barRef"/></foo>
</div>`,
components: {
bar: {
setup() {
const name = ref('bar')
return {
name,
}
},
template: '<div> {{name}} </div>',
},
foo: {
setup() {
const showSlot = ref(false)
const setShow = () => {
showSlot.value = true
}
return {
setShow,
showSlot,
}
},
template: `<div> <slot v-if="showSlot"></slot> </div>`,
},
},
}).$mount()
await nextTick()
//@ts-ignore
vm.$children[0].setShow()
await nextTick()
//@ts-ignore
expect(vm.$refs.barRef).toBe(vm.barRef)
})
// TODO: how ?
// it('work with createElement', () => {
// let root;
// const vm = new Vue({
// setup() {
// root = ref(null);
// return () => {
// return h('div', {
// ref: root,
// });
// };
// },
// }).$mount();
// expect(root.value).toBe(vm.$el);
// });
})