You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

467 lines
13 KiB

import Vue from 'vue'
describe('Directive v-for', () => {
it('should render array of primitive values', done => {
const vm = new Vue({
template: `
<div>
<span v-for="item in list">{{item}}</span>
</div>
`,
data: {
list: ['a', 'b', 'c']
}
}).$mount()
expect(vm.$el.innerHTML).toBe('<span>a</span><span>b</span><span>c</span>')
Vue.set(vm.list, 0, 'd')
waitForUpdate(() => {
expect(vm.$el.innerHTML).toBe('<span>d</span><span>b</span><span>c</span>')
vm.list.push('d')
}).then(() => {
expect(vm.$el.innerHTML).toBe('<span>d</span><span>b</span><span>c</span><span>d</span>')
vm.list.splice(1, 2)
}).then(() => {
expect(vm.$el.innerHTML).toBe('<span>d</span><span>d</span>')
vm.list = ['x', 'y']
}).then(() => {
expect(vm.$el.innerHTML).toBe('<span>x</span><span>y</span>')
}).then(done)
})
it('should render array of primitive values with index', done => {
const vm = new Vue({
template: `
<div>
<span v-for="(item, i) in list">{{i}}-{{item}}</span>
</div>
`,
data: {
list: ['a', 'b', 'c']
}
}).$mount()
expect(vm.$el.innerHTML).toBe('<span>0-a</span><span>1-b</span><span>2-c</span>')
Vue.set(vm.list, 0, 'd')
waitForUpdate(() => {
expect(vm.$el.innerHTML).toBe('<span>0-d</span><span>1-b</span><span>2-c</span>')
vm.list.push('d')
}).then(() => {
expect(vm.$el.innerHTML).toBe('<span>0-d</span><span>1-b</span><span>2-c</span><span>3-d</span>')
vm.list.splice(1, 2)
}).then(() => {
expect(vm.$el.innerHTML).toBe('<span>0-d</span><span>1-d</span>')
vm.list = ['x', 'y']
}).then(() => {
expect(vm.$el.innerHTML).toBe('<span>0-x</span><span>1-y</span>')
}).then(done)
})
it('should render array of object values', done => {
const vm = new Vue({
template: `
<div>
<span v-for="item in list">{{item.value}}</span>
</div>
`,
data: {
list: [
{ value: 'a' },
{ value: 'b' },
{ value: 'c' }
]
}
}).$mount()
expect(vm.$el.innerHTML).toBe('<span>a</span><span>b</span><span>c</span>')
Vue.set(vm.list, 0, { value: 'd' })
waitForUpdate(() => {
expect(vm.$el.innerHTML).toBe('<span>d</span><span>b</span><span>c</span>')
vm.list[0].value = 'e'
}).then(() => {
expect(vm.$el.innerHTML).toBe('<span>e</span><span>b</span><span>c</span>')
vm.list.push({})
}).then(() => {
expect(vm.$el.innerHTML).toBe('<span>e</span><span>b</span><span>c</span><span></span>')
vm.list.splice(1, 2)
}).then(() => {
expect(vm.$el.innerHTML).toBe('<span>e</span><span></span>')
vm.list = [{ value: 'x' }, { value: 'y' }]
}).then(() => {
expect(vm.$el.innerHTML).toBe('<span>x</span><span>y</span>')
}).then(done)
})
it('should render array of object values with index', done => {
const vm = new Vue({
template: `
<div>
<span v-for="(item, i) in list">{{i}}-{{item.value}}</span>
</div>
`,
data: {
list: [
{ value: 'a' },
{ value: 'b' },
{ value: 'c' }
]
}
}).$mount()
expect(vm.$el.innerHTML).toBe('<span>0-a</span><span>1-b</span><span>2-c</span>')
Vue.set(vm.list, 0, { value: 'd' })
waitForUpdate(() => {
expect(vm.$el.innerHTML).toBe('<span>0-d</span><span>1-b</span><span>2-c</span>')
vm.list[0].value = 'e'
}).then(() => {
expect(vm.$el.innerHTML).toBe('<span>0-e</span><span>1-b</span><span>2-c</span>')
vm.list.push({})
}).then(() => {
expect(vm.$el.innerHTML).toBe('<span>0-e</span><span>1-b</span><span>2-c</span><span>3-</span>')
vm.list.splice(1, 2)
}).then(() => {
expect(vm.$el.innerHTML).toBe('<span>0-e</span><span>1-</span>')
vm.list = [{ value: 'x' }, { value: 'y' }]
}).then(() => {
expect(vm.$el.innerHTML).toBe('<span>0-x</span><span>1-y</span>')
}).then(done)
})
it('should render an Object', done => {
const vm = new Vue({
template: `
<div>
<span v-for="val in obj">{{val}}</span>
</div>
`,
data: {
obj: { a: 0, b: 1, c: 2 }
}
}).$mount()
expect(vm.$el.innerHTML).toBe('<span>0</span><span>1</span><span>2</span>')
vm.obj.a = 3
waitForUpdate(() => {
expect(vm.$el.innerHTML).toBe('<span>3</span><span>1</span><span>2</span>')
Vue.set(vm.obj, 'd', 4)
}).then(() => {
expect(vm.$el.innerHTML).toBe('<span>3</span><span>1</span><span>2</span><span>4</span>')
Vue.delete(vm.obj, 'a')
}).then(() => {
expect(vm.$el.innerHTML).toBe('<span>1</span><span>2</span><span>4</span>')
}).then(done)
})
it('should render an Object with key', done => {
const vm = new Vue({
template: `
<div>
<span v-for="(val, key) in obj">{{val}}-{{key}}</span>
</div>
`,
data: {
obj: { a: 0, b: 1, c: 2 }
}
}).$mount()
expect(vm.$el.innerHTML).toBe('<span>0-a</span><span>1-b</span><span>2-c</span>')
vm.obj.a = 3
waitForUpdate(() => {
expect(vm.$el.innerHTML).toBe('<span>3-a</span><span>1-b</span><span>2-c</span>')
Vue.set(vm.obj, 'd', 4)
}).then(() => {
expect(vm.$el.innerHTML).toBe('<span>3-a</span><span>1-b</span><span>2-c</span><span>4-d</span>')
Vue.delete(vm.obj, 'a')
}).then(() => {
expect(vm.$el.innerHTML).toBe('<span>1-b</span><span>2-c</span><span>4-d</span>')
}).then(done)
})
it('should render an Object with key and index', done => {
const vm = new Vue({
template: `
<div>