From 90c6c2902b1f124093ad0d514984230194cb818e Mon Sep 17 00:00:00 2001 From: Rahul Kadyan Date: Thu, 12 Jan 2017 18:15:07 +0530 Subject: [PATCH] :sparkles: Tests for ref callback --- test/unit/features/ref.spec.js | 45 ++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) diff --git a/test/unit/features/ref.spec.js b/test/unit/features/ref.spec.js index da637b7bfa3..515a18ddaa0 100644 --- a/test/unit/features/ref.spec.js +++ b/test/unit/features/ref.spec.js @@ -157,4 +157,49 @@ describe('ref', () => { }).$mount() expect(vm.$refs.test).toBe(vm.$children[0]) }) + + it('should should call callback (v-for)', done => { + const vm = new Vue({ + data: { + items: [1, 2, 3] + }, + template: ` +
+ +
+ `, + components: { + test: { + props: ['n'], + template: '
{{ n }}
' + } + }, + methods: { + onRef (ref, remove) { + if (!this.$refs.list) this.$refs.list = [] + + if (remove) { + const index = this.$refs.list.indexOf(ref) + + if (index > -1) this.$refs.list.splice(index, 1) + } else { + this.$refs.list.push(ref) + } + } + } + }).$mount() + assertRefs() + // updating + vm.items.push(4) + waitForUpdate(assertRefs) + .then(() => { vm.items = [] }) + .then(assertRefs) + .then(done) + + function assertRefs () { + expect(Array.isArray(vm.$refs.list)).toBe(true) + expect(vm.$refs.list.length).toBe(vm.items.length) + expect(vm.$refs.list.every((comp, i) => comp.$el.textContent === String(i + 1))).toBe(true) + } + }) })