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

122 lines
3.1 KiB
JavaScript
Raw Normal View History

2014-09-17 13:32:05 +08:00
var _ = require('../../../../src/util')
var Vue = require('../../../../src/vue')
if (_.inBrowser) {
describe('v-ref', function () {
var el
beforeEach(function () {
el = document.createElement('div')
spyOn(_, 'warn')
})
var components = {
test: {
id: 'test'
},
test2: {
id: 'test2'
2014-09-17 13:32:05 +08:00
}
}
it('normal', function () {
var vm = new Vue({
el: el,
components: components,
2015-05-15 23:48:31 +08:00
template: '<test v-ref="test"></test>'
2014-09-17 13:32:05 +08:00
})
expect(vm.$.test).toBeTruthy()
expect(vm.$.test.$options.id).toBe('test')
})
it('with dynamic v-component', function (done) {
var vm = new Vue({
el: el,
components: components,
data: { test: 'test' },
2015-05-15 23:48:31 +08:00
template: '<component type="{{test}}" v-ref="test"></component>'
})
expect(vm.$.test.$options.id).toBe('test')
vm.test = 'test2'
_.nextTick(function () {
expect(vm.$.test.$options.id).toBe('test2')
vm.test = ''
_.nextTick(function () {
expect(vm.$.test).toBeNull()
done()
})
})
})
it('should also work in child template', function (done) {
var vm = new Vue({
el: el,
data: { view: 'test1' },
2015-05-15 23:48:31 +08:00
template: '<component type="{{view}}"></component>',
components: {
test1: {
id: 'test1',
template: '<div v-ref="test1"></div>',
replace: true
},
test2: {
id: 'test2',
template: '<div v-ref="test2"></div>',
replace: true
}
}
})
expect(vm.$.test1.$options.id).toBe('test1')
vm.view = 'test2'
_.nextTick(function () {
expect(vm.$.test1).toBeNull()
expect(vm.$.test2.$options.id).toBe('test2')
done()
})
2014-09-17 13:32:05 +08:00
})
it('with v-repeat', function (done) {
var vm = new Vue({
el: el,
data: { items: [1,2,3,4,5] },
template: '<div v-repeat="items" v-ref="test"></div>'
})
expect(vm.$.test).toBeTruthy()
expect(Array.isArray(vm.$.test)).toBe(true)
expect(vm.$.test[0].$value).toBe(1)
expect(vm.$.test[4].$value).toBe(5)
vm.items = []
_.nextTick(function () {
expect(vm.$.test.length).toBe(0)
2014-09-19 04:07:42 +08:00
vm._directives[0].unbind()
expect(vm.$.test).toBeNull()
2014-09-17 13:32:05 +08:00
done()
})
})
2014-10-15 01:04:05 +08:00
it('nested v-repeat', function () {
var vm = new Vue({
el: el,
2015-05-15 23:48:31 +08:00
template: '<c1 v-ref="c1"></c1>',
components: {
c1: {
template: '<div v-repeat="2" v-ref="c2"></div>'
}
}
2014-10-15 01:04:05 +08:00
})
expect(vm.$.c1 instanceof Vue).toBe(true)
expect(vm.$.c2).toBeUndefined()
expect(Array.isArray(vm.$.c1.$.c2)).toBe(true)
expect(vm.$.c1.$.c2.length).toBe(2)
})
2014-12-07 08:30:59 +08:00
it('should warn on non-root', function () {
2014-09-17 13:32:05 +08:00
var vm = new Vue({
el: el,
template: '<div v-ref="test"></div>'
})
2015-05-13 07:14:08 +08:00
expect(hasWarned(_, 'should only be used on a component root element')).toBe(true)
2014-09-17 13:32:05 +08:00
})
})
}