import Vue from 'vue'
describe('Directive v-for', () => {
it('should render array of primitive values', done => {
const vm = new Vue({
template: `
{{$index}}-{{item}}
`,
data: {
list: ['a', 'b', 'c']
}
}).$mount()
expect(vm.$el.innerHTML).toBe('0-a1-b2-c')
Vue.set(vm.list, 0, 'd')
waitForUpdate(() => {
expect(vm.$el.innerHTML).toBe('0-d1-b2-c')
vm.list.push('d')
}).then(() => {
expect(vm.$el.innerHTML).toBe('0-d1-b2-c3-d')
vm.list.splice(1, 2)
}).then(() => {
expect(vm.$el.innerHTML).toBe('0-d1-d')
vm.list = ['x', 'y']
}).then(() => {
expect(vm.$el.innerHTML).toBe('0-x1-y')
done()
}).catch(done)
})
it('should render array of object values', done => {
const vm = new Vue({
template: `
{{$index}}-{{item.value}}
`,
data: {
list: [
{ value: 'a' },
{ value: 'b' },
{ value: 'c' }
]
}
}).$mount()
expect(vm.$el.innerHTML).toBe('0-a1-b2-c')
Vue.set(vm.list, 0, { value: 'd' })
waitForUpdate(() => {
expect(vm.$el.innerHTML).toBe('0-d1-b2-c')
vm.list[0].value = 'e'
}).then(() => {
expect(vm.$el.innerHTML).toBe('0-e1-b2-c')
vm.list.push({})
}).then(() => {
expect(vm.$el.innerHTML).toBe('0-e1-b2-c3-')
vm.list.splice(1, 2)
}).then(() => {
expect(vm.$el.innerHTML).toBe('0-e1-')
vm.list = [{ value: 'x' }, { value: 'y' }]
}).then(() => {
expect(vm.$el.innerHTML).toBe('0-x1-y')
done()
}).catch(done)
})
it('should render an Object', done => {
const vm = new Vue({
template: `
{{val}}-{{$key}}
`,
data: {
obj: { a: 0, b: 1, c: 2 }
}
}).$mount()
expect(vm.$el.innerHTML).toBe('0-a1-b2-c')
vm.obj.a = 3
waitForUpdate(() => {
expect(vm.$el.innerHTML).toBe('3-a1-b2-c')
Vue.set(vm.obj, 'd', 4)
}).then(() => {
expect(vm.$el.innerHTML).toBe('3-a1-b2-c4-d')
Vue.delete(vm.obj, 'a')
}).then(() => {
expect(vm.$el.innerHTML).toBe('1-b2-c4-d')
done()
}).catch(done)
})
it('should render each key of data', done => {
const vm = new Vue({
template: `
{{val}}-{{$key}}
`,
data: { a: 0, b: 1, c: 2 }
}).$mount()
expect(vm.$el.innerHTML).toBe('0-a1-b2-c')
vm.a = 3
waitForUpdate(() => {
expect(vm.$el.innerHTML).toBe('3-a1-b2-c')
done()
}).catch(done)
})
describe('alternative syntax', () => {
it('should render array of primitive values', done => {
const vm = new Vue({
template: `
{{i}}-{{item}}
`,
data: {
list: ['a', 'b', 'c']
}
}).$mount()
expect(vm.$el.innerHTML).toBe('0-a1-b2-c')
Vue.set(vm.list, 0, 'd')
waitForUpdate(() => {
expect(vm.$el.innerHTML).toBe('0-d1-b2-c')
vm.list.push('d')
}).then(() => {
expect(vm.$el.innerHTML).toBe('0-d1-b2-c3-d')
vm.list.splice(1, 2)
}).then(() => {
expect(vm.$el.innerHTML).toBe('0-d1-d')
vm.list = ['x', 'y']
}).then(() => {
expect(vm.$el.innerHTML).toBe('0-x1-y')
done()
}).catch(done)
})
it('should render array of object values', done => {
const vm = new Vue({
template: `
{{i}}-{{item.value}}
`,
data: {
list: [
{ value: 'a' },
{ value: 'b' },
{ value: 'c' }
]
}
}).$mount()
expect(vm.$el.innerHTML).toBe('0-a1-b2-c')
Vue.set(vm.list, 0, { value: 'd' })
waitForUpdate(() => {
expect(vm.$el.innerHTML).toBe('0-d1-b2-c')
vm.list[0].value = 'e'
}).then(() => {
expect(vm.$el.innerHTML).toBe('0-e1-b2-c')
vm.list.push({})
}).then(() => {
expect(vm.$el.innerHTML).toBe('0-e1-b2-c3-')
vm.list.splice(1, 2)
}).then(() => {
expect(vm.$el.innerHTML).toBe('0-e1-')
vm.list = [{ value: 'x' }, { value: 'y' }]
}).then(() => {
expect(vm.$el.innerHTML).toBe('0-x1-y')
done()
}).catch(done)
})
it('should render an Object', done => {
const vm = new Vue({
template: `
{{v}}-{{k}}
`,
data: {
obj: { a: 0, b: 1, c: 2 }
}
}).$mount()
expect(vm.$el.innerHTML).toBe('0-a1-b2-c')
vm.obj.a = 3
waitForUpdate(() => {
expect(vm.$el.innerHTML).toBe('3-a1-b2-c')
Vue.set(vm.obj, 'd', 4)
}).then(() => {
expect(vm.$el.innerHTML).toBe('3-a1-b2-c4-d')
Vue.delete(vm.obj, 'a')
}).then(() => {
expect(vm.$el.innerHTML).toBe('1-b2-c4-d')
done()
}).catch(done)
})
it('should render each key of data', done => {
const vm = new Vue({
template: `
{{v}}-{{k}}
`,
data: { a: 0, b: 1, c: 2 }
}).$mount()
expect(vm.$el.innerHTML).toBe('0-a1-b2-c')
vm.a = 3
waitForUpdate(() => {
expect(vm.$el.innerHTML).toBe('3-a1-b2-c')
done()
}).catch(done)
})
})
it('check priorities: v-if before v-for', function () {
const vm = new Vue({
data: {
items: [1, 2, 3]
},
template: ''
}).$mount()
expect(vm.$el.textContent).toBe('12')
})
it('check priorities: v-if after v-for', function () {
const vm = new Vue({
data: {
items: [1, 2, 3]
},
template: ''
}).$mount()
expect(vm.$el.textContent).toBe('12')
})
it('range v-for', () => {
const vm = new Vue({
template: ''
}).$mount()
expect(vm.$el.textContent).toBe('123')
})
it('without track-by', done => {
const vm = new Vue({
data: {
items: [
{ id: 1, msg: 'a' },
{ id: 2, msg: 'b' },
{ id: 3, msg: 'c' }
]
},
template: ''
}).$mount()
expect(vm.$el.textContent).toBe('abc')
const first = vm.$el.children[0]
vm.items.reverse()
waitForUpdate(() => {
expect(vm.$el.textContent).toBe('cba')
// assert reusing DOM element in place
expect(vm.$el.children[0]).toBe(first)
done()
})
})
it('with track-by', done => {
const vm = new Vue({
data: {
items: [
{ id: 1, msg: 'a' },
{ id: 2, msg: 'b' },
{ id: 3, msg: 'c' }
]
},
template: ''
}).$mount()
expect(vm.$el.textContent).toBe('abc')
const first = vm.$el.children[0]
vm.items.reverse()
waitForUpdate(() => {
expect(vm.$el.textContent).toBe('cba')
// assert moving DOM element
expect(vm.$el.children[0]).not.toBe(first)
expect(vm.$el.children[2]).toBe(first)
done()
})
})
it('nested loops', () => {
const vm = new Vue({
data: {
items: [
{ items: [{ a: 1 }, { a: 2 }], a: 1 },
{ items: [{ a: 3 }, { a: 4 }], a: 2 }
]
},
template:
'' +
'
' +
'
{{$index}} {{subItem.a}} {{i}} {{item.a}}
' +
'
' +
'
'
}).$mount()
expect(vm.$el.innerHTML).toBe(
'' +
''
)
})
it('template v-for', done => {
const vm = new Vue({
data: {
list: [
{ a: 1 },
{ a: 2 },
{ a: 3 }
]
},
template:
'' +
'
' +
'{{item.a}}
' +
'{{item.a + 1}}
' +
'' +
'
'
}).$mount()
assertMarkup()
vm.list.reverse()
waitForUpdate(() => {
assertMarkup()
vm.list.splice(1, 1)
}).then(() => {
assertMarkup()
vm.list.splice(1, 0, { a: 2 })
done()
}).catch(done)
function assertMarkup () {
var markup = vm.list.map(function (item) {
return '' + item.a + '
' + (item.a + 1) + '
'
}).join('')
expect(vm.$el.innerHTML).toBe(markup)
}
})
it('component v-for', done => {
const vm = new Vue({
data: {
list: [
{ a: 1 },
{ a: 2 },
{ a: 3 }
]
},
template:
'' +
'' +
'{{item.a}}' +
'' +
'
',
components: {
test: {
props: ['msg'],
template: '{{msg}}
'
}
}
}).$mount()
assertMarkup()
vm.list.reverse()
waitForUpdate(() => {
assertMarkup()
vm.list.splice(1, 1)
}).then(() => {
assertMarkup()
vm.list.splice(1, 0, { a: 2 })
done()
}).catch(done)
function assertMarkup () {
var markup = vm.list.map(function (item) {
return `${item.a}${item.a}
`
}).join('')
expect(vm.$el.innerHTML).toBe(markup)
}
})
it('dynamic component v-for', done => {
const vm = new Vue({
data: {
list: [
{ type: 'one' },
{ type: 'two' }
]
},
template:
'' +
'' +
'
',
components: {
one: {
template: 'One!
'
},
two: {
template: 'Two!
'
}
}
}).$mount()
expect(vm.$el.innerHTML).toContain('One!
Two!
')
vm.list.reverse()
waitForUpdate(() => {
expect(vm.$el.innerHTML).toContain('Two!
One!
')
done()
}).catch(done)
})
})