import Vue from 'vue'
describe('Directive v-model radio', () => {
it('should work', done => {
const vm = new Vue({
data: {
test: '1'
},
template: `
`
}).$mount()
document.body.appendChild(vm.$el)
expect(vm.$el.children[0].checked).toBe(true)
expect(vm.$el.children[1].checked).toBe(false)
vm.test = '2'
waitForUpdate(() => {
expect(vm.$el.children[0].checked).toBe(false)
expect(vm.$el.children[1].checked).toBe(true)
vm.$el.children[0].click()
expect(vm.$el.children[0].checked).toBe(true)
expect(vm.$el.children[1].checked).toBe(false)
expect(vm.test).toBe('1')
}).then(() => {
document.body.removeChild(vm.$el)
}).then(done)
})
it('should respect value bindings', done => {
const vm = new Vue({
data: {
test: 1
},
template: `
`
}).$mount()
document.body.appendChild(vm.$el)
expect(vm.$el.children[0].checked).toBe(true)
expect(vm.$el.children[1].checked).toBe(false)
vm.test = 2
waitForUpdate(() => {
expect(vm.$el.children[0].checked).toBe(false)
expect(vm.$el.children[1].checked).toBe(true)
vm.$el.children[0].click()
expect(vm.$el.children[0].checked).toBe(true)
expect(vm.$el.children[1].checked).toBe(false)
expect(vm.test).toBe(1)
}).then(() => {
document.body.removeChild(vm.$el)
}).then(done)
})
it('should respect value bindings (object loose equal)', done => {
const vm = new Vue({
data: {
test: { a: 1 }
},
template: `
`
}).$mount()
document.body.appendChild(vm.$el)
expect(vm.$el.children[0].checked).toBe(true)
expect(vm.$el.children[1].checked).toBe(false)
vm.test = { a: 2 }
waitForUpdate(() => {
expect(vm.$el.children[0].checked).toBe(false)
expect(vm.$el.children[1].checked).toBe(true)
vm.$el.children[0].click()
expect(vm.$el.children[0].checked).toBe(true)
expect(vm.$el.children[1].checked).toBe(false)
expect(vm.test).toEqual({ a: 1 })
}).then(() => {
document.body.removeChild(vm.$el)
}).then(done)
})
it('multiple radios ', (done) => {
const spy = jasmine.createSpy()
const vm = new Vue({
data: {
selections: ['a', '1'],
radioList: [
{
name: 'questionA',
data: ['a', 'b', 'c']
},
{
name: 'questionB',
data: ['1', '2']
}
]
},
watch: {
selections: spy
},
template:
'' +
'
' +
'
' +
'' +
'' +
'' +
'' +
'
' +
'
' +
'
'
}).$mount()
document.body.appendChild(vm.$el)
var inputs = vm.$el.getElementsByTagName('input')
inputs[1].click()
waitForUpdate(() => {
expect(vm.selections).toEqual(['b', '1'])
expect(spy).toHaveBeenCalled()
}).then(done)
})
it('.number modifier', () => {
const vm = new Vue({
data: {
test: 1
},
template: `
`
}).$mount()
document.body.appendChild(vm.$el)
expect(vm.$el.children[0].checked).toBe(true)
expect(vm.$el.children[1].checked).toBe(false)
vm.$el.children[1].click()
expect(vm.$el.children[0].checked).toBe(false)
expect(vm.$el.children[1].checked).toBe(true)
expect(vm.test).toBe(2)
})
it('warn inline checked', () => {
const vm = new Vue({
template: ``,
data: {
test: '2'
}
}).$mount()
expect(vm.$el.checked).toBe(false)
expect('inline checked attributes will be ignored when using v-model').toHaveBeenWarned()
})
})