vue2/test/unit/specs/directives/prop_spec.js

108 lines
2.7 KiB
JavaScript
Raw Normal View History

2015-05-15 02:03:49 +08:00
var _ = require('../../../../src/util')
var Vue = require('../../../../src/vue')
if (_.inBrowser) {
describe('prop', function () {
var el
beforeEach(function () {
el = document.createElement('div')
spyOn(_, 'warn')
})
it('should work', function (done) {
var vm = new Vue({
el: el,
data: {
b: 'B',
test: {
a: 'A'
}
},
2015-05-15 23:48:31 +08:00
template: '<test testt="{{test}}" bb="{{b}}" v-ref="child"></test>',
2015-05-15 02:03:49 +08:00
components: {
test: {
props: ['testt', 'bb'],
template: '{{testt.a}} {{bb}}'
}
}
})
expect(el.firstChild.textContent).toBe('A B')
vm.test.a = 'AA'
vm.b = 'BB'
_.nextTick(function () {
expect(el.firstChild.textContent).toBe('AA BB')
vm.test = { a: 'AAA' }
_.nextTick(function () {
expect(el.firstChild.textContent).toBe('AAA BB')
vm.$data = {
b: 'BBB',
test: {
a: 'AAAA'
}
}
_.nextTick(function () {
expect(el.firstChild.textContent).toBe('AAAA BBB')
// test two-way
vm.$.child.bb = 'B'
vm.$.child.testt = { a: 'A' }
_.nextTick(function () {
expect(el.firstChild.textContent).toBe('A B')
expect(vm.test.a).toBe('A')
expect(vm.test).toBe(vm.$.child.testt)
expect(vm.b).toBe('B')
done()
})
})
})
})
})
it('teardown', function (done) {
var vm = new Vue({
el: el,
data: {
b: 'B'
},
2015-05-15 23:48:31 +08:00
template: '<test bb="{{b}}"></test>',
2015-05-15 02:03:49 +08:00
components: {
test: {
props: ['bb'],
template: '{{bb}}'
}
}
})
expect(el.firstChild.textContent).toBe('B')
vm.b = 'BB'
_.nextTick(function () {
expect(el.firstChild.textContent).toBe('BB')
vm._children[0]._directives[0].unbind()
vm.b = 'BBB'
_.nextTick(function () {
expect(el.firstChild.textContent).toBe('BB')
done()
})
})
})
it('block instance with replace:true', function () {
var vm = new Vue({
el: el,
2015-05-15 23:48:31 +08:00
template: '<test b="{{a}}" c="{{d}}"></test>',
2015-05-15 02:03:49 +08:00
data: {
a: 'AAA',
d: 'DDD'
},
components: {
test: {
props: ['b', 'c'],
template: '<p>{{b}}</p><p>{{c}}</p>',
replace: true
}
}
})
expect(el.innerHTML).toBe('<!--v-start--><p>AAA</p><p>DDD</p><!--v-end--><!--v-component-->')
})
})
}