import Vue from 'vue'
describe('Directive v-if', () => {
it('should check if value is truthy', () => {
const vm = new Vue({
template: '
hello
',
data: { foo: true }
}).$mount()
expect(vm.$el.innerHTML).toBe('hello')
})
it('should check if value is falsy', () => {
const vm = new Vue({
template: 'hello
',
data: { foo: false }
}).$mount()
expect(vm.$el.innerHTML).toBe('')
})
it('should update if value changed', done => {
const vm = new Vue({
template: 'hello
',
data: { foo: true }
}).$mount()
expect(vm.$el.innerHTML).toBe('hello')
vm.foo = false
waitForUpdate(() => {
expect(vm.$el.innerHTML).toBe('')
vm.foo = {}
}).then(() => {
expect(vm.$el.innerHTML).toBe('hello')
vm.foo = 0
}).then(() => {
expect(vm.$el.innerHTML).toBe('')
vm.foo = []
}).then(() => {
expect(vm.$el.innerHTML).toBe('hello')
vm.foo = null
}).then(() => {
expect(vm.$el.innerHTML).toBe('')
vm.foo = '0'
}).then(() => {
expect(vm.$el.innerHTML).toBe('hello')
vm.foo = undefined
}).then(() => {
expect(vm.$el.innerHTML).toBe('')
vm.foo = 1
}).then(() => {
expect(vm.$el.innerHTML).toBe('hello')
done()
})
.catch(done)
})
it('should work well with v-else', done => {
const vm = new Vue({
template: `
hello
bye
`,
data: { foo: true }
}).$mount()
expect(vm.$el.innerHTML).toBe('hello')
vm.foo = false
waitForUpdate(() => {
expect(vm.$el.innerHTML).toBe('bye')
vm.foo = {}
}).then(() => {
expect(vm.$el.innerHTML).toBe('hello')
vm.foo = 0
}).then(() => {
expect(vm.$el.innerHTML).toBe('bye')
vm.foo = []
}).then(() => {
expect(vm.$el.innerHTML).toBe('hello')
vm.foo = null
}).then(() => {
expect(vm.$el.innerHTML).toBe('bye')
vm.foo = '0'
}).then(() => {
expect(vm.$el.innerHTML).toBe('hello')
vm.foo = undefined
}).then(() => {
expect(vm.$el.innerHTML).toBe('bye')
vm.foo = 1
}).then(() => {
expect(vm.$el.innerHTML).toBe('hello')
done()
}).catch(done)
})
it('should work well with v-for', done => {
const vm = new Vue({
template: `
{{i}}
`,
data: {
list: [
{ value: true },
{ value: false },
{ value: true }
]
}
}).$mount()
expect(vm.$el.innerHTML).toBe('02')
vm.list[0].value = false
waitForUpdate(() => {
expect(vm.$el.innerHTML).toBe('2')
vm.list.push({ value: true })
}).then(() => {
expect(vm.$el.innerHTML).toBe('23')
vm.list.splice(1, 2)
}).then(() => {
expect(vm.$el.innerHTML).toBe('1')
done()
}).catch(done)
})
it('should work well with v-for and v-else', done => {
const vm = new Vue({
template: `
hello
bye
`,
data: {
list: [
{ value: true },
{ value: false },
{ value: true }
]
}
}).$mount()
expect(vm.$el.innerHTML).toBe('hellobyehello')
vm.list[0].value = false
waitForUpdate(() => {
expect(vm.$el.innerHTML).toBe('byebyehello')
vm.list.push({ value: true })
}).then(() => {
expect(vm.$el.innerHTML).toBe('byebyehellohello')
vm.list.splice(1, 2)
}).then(() => {
expect(vm.$el.innerHTML).toBe('byehello')
done()
}).catch(done)
})
})