import Vue from 'vue'
import injectStyles from './inject-styles'
import { isIE9 } from 'web/util/index'
import { nextFrame } from 'web/runtime/transition-util'
if (!isIE9) {
describe('Transition group', () => {
const duration = injectStyles()
let el
beforeEach(() => {
el = document.createElement('div')
document.body.appendChild(el)
})
function createBasicVM (useIs, appear) {
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 + 10).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 + 10).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 + 10).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 + 10).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 + 10).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: `
`,
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: `
`,
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()
})
})
}