import Vue from 'vue'
describe('Global API: set/delete', () => {
describe('Vue.set', () => {
it('should update a vue object', done => {
const vm = new Vue({
template: '
{{x}}
',
data: { x: 1 }
}).$mount()
expect(vm.$el.innerHTML).toBe('1')
Vue.set(vm, 'x', 2)
waitForUpdate(() => {
expect(vm.$el.innerHTML).toBe('2')
}).then(done)
})
it('should update a observing object', done => {
const vm = new Vue({
template: '{{foo.x}}
',
data: { foo: { x: 1 }}
}).$mount()
expect(vm.$el.innerHTML).toBe('1')
Vue.set(vm.foo, 'x', 2)
waitForUpdate(() => {
expect(vm.$el.innerHTML).toBe('2')
}).then(done)
})
it('should update a observing array', done => {
const vm = new Vue({
template: '',
data: { list: ['a', 'b', 'c'] }
}).$mount()
expect(vm.$el.innerHTML).toBe('0-a
1-b
2-c
')
Vue.set(vm.list, 1, 'd')
waitForUpdate(() => {
expect(vm.$el.innerHTML).toBe('0-a
1-d
2-c
')
}).then(done)
})
it('should update a vue object with nothing', done => {
const vm = new Vue({
template: '{{x}}
',
data: { x: 1 }
}).$mount()
expect(vm.$el.innerHTML).toBe('1')
Vue.set(vm, 'x', null)
waitForUpdate(() => {
expect(vm.$el.innerHTML).toBe('')
Vue.set(vm, 'x')
}).then(() => {
expect(vm.$el.innerHTML).toBe('')
}).then(done)
})
})
describe('Vue.delete', () => {
it('should delete a key', done => {
const vm = new Vue({
template: '{{obj.x}}
',
data: { obj: { x: 1 }}
}).$mount()
expect(vm.$el.innerHTML).toBe('1')
vm.obj.x = 2
waitForUpdate(() => {
expect(vm.$el.innerHTML).toBe('2')
Vue.delete(vm.obj, 'x')
}).then(() => {
expect(vm.$el.innerHTML).toBe('')
vm.obj.x = 3
}).then(() => {
expect(vm.$el.innerHTML).toBe('')
}).then(done)
})
it('be able to delete an item in array', done => {
const vm = new Vue({
template: '',
data: {
lists: [
{ name: 'A' },
{ name: 'B' },
{ name: 'C' }
]
}
}).$mount()
expect(vm.$el.innerHTML).toBe('A
B
C
')
Vue.delete(vm.lists, 1)
waitForUpdate(() => {
expect(vm.$el.innerHTML).toBe('A
C
')
Vue.delete(vm.lists, 1)
}).then(() => {
expect(vm.$el.innerHTML).toBe('A
')
Vue.delete(vm.lists, 0)
}).then(() => {
expect(vm.$el.innerHTML).toBe('')
}).then(done)
})
})
})