Skip to content

Commit 90c6c29

Browse files
znckyyx990803
authored andcommitted
✨ Tests for ref callback
1 parent 8d88512 commit 90c6c29

File tree

1 file changed

+45
-0
lines changed

1 file changed

+45
-0
lines changed

test/unit/features/ref.spec.js

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,4 +157,49 @@ describe('ref', () => {
157157
}).$mount()
158158
expect(vm.$refs.test).toBe(vm.$children[0])
159159
})
160+
161+
it('should should call callback (v-for)', done => {
162+
const vm = new Vue({
163+
data: {
164+
items: [1, 2, 3]
165+
},
166+
template: `
167+
<div>
168+
<test v-for="n in items" :ref="onRef" :n="n"></test>
169+
</div>
170+
`,
171+
components: {
172+
test: {
173+
props: ['n'],
174+
template: '<div>{{ n }}</div>'
175+
}
176+
},
177+
methods: {
178+
onRef (ref, remove) {
179+
if (!this.$refs.list) this.$refs.list = []
180+
181+
if (remove) {
182+
const index = this.$refs.list.indexOf(ref)
183+
184+
if (index > -1) this.$refs.list.splice(index, 1)
185+
} else {
186+
this.$refs.list.push(ref)
187+
}
188+
}
189+
}
190+
}).$mount()
191+
assertRefs()
192+
// updating
193+
vm.items.push(4)
194+
waitForUpdate(assertRefs)
195+
.then(() => { vm.items = [] })
196+
.then(assertRefs)
197+
.then(done)
198+
199+
function assertRefs () {
200+
expect(Array.isArray(vm.$refs.list)).toBe(true)
201+
expect(vm.$refs.list.length).toBe(vm.items.length)
202+
expect(vm.$refs.list.every((comp, i) => comp.$el.textContent === String(i + 1))).toBe(true)
203+
}
204+
})
160205
})

0 commit comments

Comments
 (0)