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')
}).then(done)
})
it('should work well with v-else', done => {
const vm = new Vue({
template: `
hello
bye
`,
data: { foo: true }
}).$mount()
expect(vm.$el.innerHTML.trim()).toBe('hello')
vm.foo = false
waitForUpdate(() => {
expect(vm.$el.innerHTML.trim()).toBe('bye')
vm.foo = {}
}).then(() => {
expect(vm.$el.innerHTML.trim()).toBe('hello')
vm.foo = 0
}).then(() => {
expect(vm.$el.innerHTML.trim()).toBe('bye')
vm.foo = []
}).then(() => {
expect(vm.$el.innerHTML.trim()).toBe('hello')
vm.foo = null
}).then(() => {
expect(vm.$el.innerHTML.trim()).toBe('bye')
vm.foo = '0'
}).then(() => {
expect(vm.$el.innerHTML.trim()).toBe('hello')
vm.foo = undefined
}).then(() => {
expect(vm.$el.innerHTML.trim()).toBe('bye')
vm.foo = 1
}).then(() => {
expect(vm.$el.innerHTML.trim()).toBe('hello')
}).then(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')
}).then(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.trim()).toBe('hellobyehello')
vm.list[0].value = false
waitForUpdate(() => {
expect(vm.$el.innerHTML.trim()).toBe('byebyehello')
vm.list.push({ value: true })
}).then(() => {
expect(vm.$el.innerHTML.trim()).toBe('byebyehellohello')
vm.list.splice(1, 2)
}).then(() => {
expect(vm.$el.innerHTML.trim()).toBe('byehello')
}).then(done)
})
})