import Vue from 'vue' import { injectStyles, waitForUpdate, nextFrame } from './helpers' describe('Transition group', () => { const { duration, buffer } = injectStyles() let el beforeEach(() => { el = document.createElement('div') document.body.appendChild(el) }) function createBasicVM(useIs?, appear = false) { const vm = new Vue({ template: `
${ useIs ? `` : `` }
{{ item }}
${useIs ? `
` : ``}
`, data: { items: ['a', 'b', 'c'] } }).$mount(el) if (!appear) { expect(vm.$el.innerHTML).toBe( `` + vm.items.map(i => `
${i}
`).join('') + `
` ) } return vm } it('enter', done => { const vm = createBasicVM() vm.items.push('d', 'e') waitForUpdate(() => { expect(vm.$el.innerHTML).toBe( `` + ['a', 'b', 'c'].map(i => `
${i}
`).join('') + `
d
` + `
e
` + `
` ) }) .thenWaitFor(nextFrame) .then(() => { expect(vm.$el.innerHTML).toBe( `` + ['a', 'b', 'c'].map(i => `
${i}
`).join('') + `
d
` + `
e
` + `
` ) }) .thenWaitFor(duration + buffer) .then(() => { expect(vm.$el.innerHTML).toBe( `` + vm.items.map(i => `
${i}
`).join('') + `
` ) }) .then(done) }) it('leave', done => { const vm = createBasicVM() vm.items = ['b'] waitForUpdate(() => { expect(vm.$el.innerHTML).toBe( `` + `
a
` + `
b
` + `
c
` + `
` ) }) .thenWaitFor(nextFrame) .then(() => { expect(vm.$el.innerHTML).toBe( `` + `
a
` + `
b
` + `
c
` + `
` ) }) .thenWaitFor(duration + buffer) .then(() => { expect(vm.$el.innerHTML).toBe( `` + vm.items.map(i => `
${i}
`).join('') + `
` ) }) .then(done) }) it('enter + leave', done => { const vm = createBasicVM() vm.items = ['b', 'c', 'd'] waitForUpdate(() => { expect(vm.$el.innerHTML).toBe( `` + `
a
` + `
b
` + `
c
` + `
d
` + `
` ) }) .thenWaitFor(nextFrame) .then(() => { expect(vm.$el.innerHTML).toBe( `` + `
a
` + `
b
` + `
c
` + `
d
` + `
` ) }) .thenWaitFor(duration + buffer) .then(() => { expect(vm.$el.innerHTML).toBe( `` + vm.items.map(i => `
${i}
`).join('') + `
` ) }) .then(done) }) it('use with "is" attribute', done => { const vm = createBasicVM(true) vm.items = ['b', 'c', 'd'] waitForUpdate(() => { expect(vm.$el.innerHTML).toBe( `` + `
a
` + `
b
` + `
c
` + `
d
` + `
` ) }) .thenWaitFor(nextFrame) .then(() => { expect(vm.$el.innerHTML).toBe( `` + `
a
` + `
b
` + `
c
` + `
d
` + `
` ) }) .thenWaitFor(duration + buffer) .then(() => { expect(vm.$el.innerHTML).toBe( `` + vm.items.map(i => `
${i}
`).join('') + `
` ) }) .then(done) }) it('appear', done => { const vm = createBasicVM(false, true /* appear */) waitForUpdate(() => { expect(vm.$el.innerHTML).toBe( `` + vm.items .map(i => `
${i}
`) .join('') + `
` ) }) .thenWaitFor(nextFrame) .then(() => { expect(vm.$el.innerHTML).toBe( `` + vm.items .map( i => `
${i}
` ) .join('') + `
` ) }) .thenWaitFor(duration + buffer) .then(() => { expect(vm.$el.innerHTML).toBe( `` + vm.items.map(i => `
${i}
`).join('') + `
` ) }) .then(done) }) it('events', done => { let next const beforeEnterSpy = jasmine.createSpy() const afterEnterSpy = jasmine.createSpy() const afterLeaveSpy = jasmine.createSpy() const vm = new Vue({ template: `
{{ item }}
`, data: { items: ['a', 'b', 'c'] }, methods: { beforeEnter(el) { expect(el.textContent).toBe('d') beforeEnterSpy() }, afterEnter(el) { expect(el.textContent).toBe('d') afterEnterSpy() next() }, afterLeave(el) { expect(el.textContent).toBe('a') afterLeaveSpy() next() } } }).$mount(el) vm.items.push('d') waitForUpdate(() => { expect(vm.$el.innerHTML).toBe( `` + `
a
` + `
b
` + `
c
` + `
d
` + `
` ) expect(beforeEnterSpy.calls.count()).toBe(1) }) .thenWaitFor(_next => { next = _next }) .then(() => { expect(vm.$el.innerHTML).toBe( `` + `
a
` + `
b
` + `
c
` + `
d
` + `
` ) expect(afterEnterSpy.calls.count()).toBe(1) vm.items.shift() }) .thenWaitFor(_next => { next = _next }) .then(() => { expect(vm.$el.innerHTML).toBe( `` + `
b
` + `
c
` + `
d
` + `
` ) expect(afterLeaveSpy.calls.count()).toBe(1) }) .then(done) }) it('move', done => { const vm = new Vue({ template: `
{{ item }}
`, data: { items: ['a', 'b', 'c'] } }).$mount(el) vm.items = ['d', 'b', 'a'] waitForUpdate(() => { expect(vm.$el.innerHTML.replace(/\s?style=""(\s?)/g, '$1')).toBe( `` + `
d
` + `
b
` + `
a
` + `
c
` + `
` ) }) .thenWaitFor(nextFrame) .then(() => { expect(vm.$el.innerHTML.replace(/\s?style=""(\s?)/g, '$1')).toBe( `` + `
d
` + `
b
` + `
a
` + `
c
` + `
` ) }) .thenWaitFor(duration * 2) .then(() => { expect(vm.$el.innerHTML.replace(/\s?style=""(\s?)/g, '$1')).toBe( `` + `
d
` + `
b
` + `
a
` + `
` ) }) .then(done) }) it('warn unkeyed children', () => { new Vue({ template: `
` }).$mount() expect( ' children must be keyed:
' ).toHaveBeenWarned() }) // GitHub issue #6006 it('should work with dynamic name', done => { const vm = new Vue({ template: `
{{ item }}
`, data: { items: ['a', 'b', 'c'], name: 'group' } }).$mount(el) vm.name = 'invalid-name' vm.items = ['b', 'c', 'a'] waitForUpdate(() => { expect(vm.$el.innerHTML.replace(/\s?style=""(\s?)/g, '$1')).toBe( `` + `
b
` + `
c
` + `
a
` + `
` ) vm.name = 'group' vm.items = ['a', 'b', 'c'] }) .thenWaitFor(nextFrame) .then(() => { expect(vm.$el.innerHTML.replace(/\s?style=""(\s?)/g, '$1')).toBe( `` + `
a
` + `
b
` + `
c
` + `
` ) }) .thenWaitFor(duration * 2 + buffer) .then(() => { expect(vm.$el.innerHTML.replace(/\s?style=""(\s?)/g, '$1')).toBe( `` + `
a
` + `
b
` + `
c
` + `
` ) }) .then(done) }) })