var _ = require('../../../../src/util')
var Vue = require('../../../../src/vue')
if (_.inBrowser) {
describe('v-if', function () {
var el
beforeEach(function () {
el = document.createElement('div')
spyOn(_, 'warn')
})
function wrap (content) {
return '' + content + ''
}
it('normal', function (done) {
var vm = new Vue({
el: el,
data: { test: false, a: 'A' },
template: '
',
components: {
test: {
inherit: true,
template: '{{a}}'
}
}
})
// lazy instantitation
expect(el.innerHTML).toBe(wrap(''))
expect(vm._children.length).toBe(0)
vm.test = true
_.nextTick(function () {
expect(el.innerHTML).toBe(wrap(''))
expect(vm._children.length).toBe(1)
vm.test = false
_.nextTick(function () {
expect(el.innerHTML).toBe(wrap(''))
expect(vm._children.length).toBe(0)
vm.test = true
_.nextTick(function () {
expect(el.innerHTML).toBe(wrap(''))
expect(vm._children.length).toBe(1)
var child = vm._children[0]
vm.$destroy()
expect(child._isDestroyed).toBe(true)
done()
})
})
})
})
it('template block', function (done) {
var vm = new Vue({
el: el,
data: { test: false, a: 'A', b: 'B' },
template: '{{a}}
{{b}}
'
})
// lazy instantitation
expect(el.innerHTML).toBe(wrap(''))
vm.test = true
_.nextTick(function () {
expect(el.innerHTML).toBe(wrap('A
B
'))
vm.test = false
_.nextTick(function () {
expect(el.innerHTML).toBe(wrap(''))
done()
})
})
})
it('v-if + v-component', function (done) {
var attachSpy = jasmine.createSpy()
var detachSpy = jasmine.createSpy()
var readySpy = jasmine.createSpy()
var vm = new Vue({
el: el,
data: { ok: false },
template: '',
components: {
test: {
data: function () {
return { a: 123 }
},
template: '{{a}}',
ready: readySpy,
attached: attachSpy,
detached: detachSpy
}
}
})
vm.$appendTo(document.body)
expect(el.innerHTML).toBe(wrap(''))
expect(vm._children.length).toBe(0)
vm.ok = true
_.nextTick(function () {
expect(el.innerHTML).toBe(wrap('123
'))
expect(vm._children.length).toBe(1)
expect(attachSpy).toHaveBeenCalled()
expect(readySpy).toHaveBeenCalled()
vm.ok = false
_.nextTick(function () {
expect(detachSpy).toHaveBeenCalled()
expect(el.innerHTML).toBe(wrap(''))
expect(vm._children.length).toBe(0)
vm.$remove()
done()
})
})
})
it('v-if + dynamic component', function (done) {
var vm = new Vue({
el: el,
data: {
ok: false,
view: 'a'
},
template: '',
components: {
a: {
template: 'AAA'
},
b: {
template: 'BBB'
}
}
})
expect(el.innerHTML).toBe(wrap(''))
expect(vm._children.length).toBe(0)
// toggle if with lazy instantiation
vm.ok = true
_.nextTick(function () {
expect(el.innerHTML).toBe(wrap('AAA
'))
expect(vm._children.length).toBe(1)
// switch view when if=true
vm.view = 'b'
_.nextTick(function () {
expect(el.innerHTML).toBe(wrap('BBB
'))
expect(vm._children.length).toBe(1)
// toggle if when already instantiated
vm.ok = false
_.nextTick(function () {
expect(el.innerHTML).toBe(wrap(''))
expect(vm._children.length).toBe(0)
// toggle if and switch view at the same time
vm.view = 'a'
vm.ok = true
_.nextTick(function () {
expect(el.innerHTML).toBe(wrap('AAA
'))
expect(vm._children.length).toBe(1)
done()
})
})
})
})
})
it('v-if with different truthy values', function (done) {
var vm = new Vue({
el: el,
data: {
a: 1
},
template: '{{a}}
'
})
expect(el.innerHTML).toBe(wrap('1
'))
vm.a = 2
_.nextTick(function () {
expect(el.innerHTML).toBe(wrap('2
'))
done()
})
})
it('invalid warn', function () {
el.setAttribute('v-if', 'test')
var vm = new Vue({
el: el
})
expect(_.warn).toHaveBeenCalled()
})
})
}