mirror of
https://gitee.com/vant-contrib/vant.git
synced 2025-04-06 03:57:59 +08:00
checkbox and radio unit test
This commit is contained in:
parent
4616351575
commit
cd049aa2a1
packages/checkbox/src
test/unit
@ -79,8 +79,6 @@ export default {
|
|||||||
isChecked() {
|
isChecked() {
|
||||||
if ({}.toString.call(this.currentValue) === '[object Boolean]') {
|
if ({}.toString.call(this.currentValue) === '[object Boolean]') {
|
||||||
return this.currentValue;
|
return this.currentValue;
|
||||||
} else if (Array.isArray(this.currentValue)) {
|
|
||||||
return this.currentValue.indexOf(this.name) > -1;
|
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
@ -89,7 +87,7 @@ export default {
|
|||||||
*/
|
*/
|
||||||
isDisabled() {
|
isDisabled() {
|
||||||
return this.isGroup && this.parentGroup
|
return this.isGroup && this.parentGroup
|
||||||
? this.parentGroup.disabled || this.disabled
|
? this.parentGroup.disabled
|
||||||
: this.disabled;
|
: this.disabled;
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
29
test/unit/components/checkbox.vue
Normal file
29
test/unit/components/checkbox.vue
Normal file
@ -0,0 +1,29 @@
|
|||||||
|
<template>
|
||||||
|
<zan-checkbox-group v-model="result">
|
||||||
|
<zan-checkbox v-for="item in list" :name="item">复选框{{item}}</zan-checkbox>
|
||||||
|
</zan-checkbox-group>
|
||||||
|
</template>
|
||||||
|
|
||||||
|
<script>
|
||||||
|
import Checkbox from 'packages/checkbox';
|
||||||
|
import CheckboxGroup from 'packages/checkbox-group';
|
||||||
|
|
||||||
|
export default {
|
||||||
|
components: {
|
||||||
|
'zan-checkbox': Checkbox,
|
||||||
|
'zan-checkbox-group': CheckboxGroup
|
||||||
|
},
|
||||||
|
|
||||||
|
data() {
|
||||||
|
return {
|
||||||
|
list: [
|
||||||
|
'a',
|
||||||
|
'b',
|
||||||
|
'c',
|
||||||
|
'd'
|
||||||
|
],
|
||||||
|
result: ['a', 'b']
|
||||||
|
};
|
||||||
|
}
|
||||||
|
};
|
||||||
|
</script>
|
24
test/unit/components/radio.vue
Normal file
24
test/unit/components/radio.vue
Normal file
@ -0,0 +1,24 @@
|
|||||||
|
<template>
|
||||||
|
<zan-radio-group v-model="radio">
|
||||||
|
<zan-radio name="1">单选框1</zan-radio>
|
||||||
|
<zan-radio name="2">单选框2</zan-radio>
|
||||||
|
</zan-radio-group>
|
||||||
|
</template>
|
||||||
|
|
||||||
|
<script>
|
||||||
|
import Radio from 'packages/radio';
|
||||||
|
import RadioGroup from 'packages/radio-group';
|
||||||
|
|
||||||
|
export default {
|
||||||
|
components: {
|
||||||
|
'zan-radio': Radio,
|
||||||
|
'zan-radio-group': RadioGroup
|
||||||
|
},
|
||||||
|
|
||||||
|
data() {
|
||||||
|
return {
|
||||||
|
radio: '1'
|
||||||
|
};
|
||||||
|
}
|
||||||
|
};
|
||||||
|
</script>
|
@ -1,51 +1,131 @@
|
|||||||
// import Checkbox from 'packages/checkbox';
|
import { mount } from 'avoriaz';
|
||||||
// import CheckboxGroup from 'packages/checkbox-group';
|
import Checkbox from 'packages/checkbox';
|
||||||
// import { mount } from 'avoriaz';
|
import CheckboxTestComponent from '../components/checkbox';
|
||||||
|
|
||||||
// describe('Checkbox', () => {
|
describe('CheckboxGroup', () => {
|
||||||
// let wrapper;
|
let wrapper;
|
||||||
// afterEach(() => {
|
afterEach(() => {
|
||||||
// wrapper && wrapper.destroy();
|
wrapper && wrapper.destroy();
|
||||||
// });
|
});
|
||||||
|
|
||||||
// it('create a checkbox', () => {
|
it('create a checkbox-group', () => {
|
||||||
// wrapper = mount(Checkbox, {
|
wrapper = mount(CheckboxTestComponent);
|
||||||
// propsData: {}
|
|
||||||
// });
|
|
||||||
|
|
||||||
// expect(wrapper.hasClass('zan-checkbox')).to.be.true;
|
expect(wrapper.hasClass('zan-checkbox-group')).to.be.true;
|
||||||
// });
|
|
||||||
// });
|
|
||||||
|
|
||||||
// describe('CheckboxGroup', () => {
|
expect(wrapper.vNode.child.value.length).to.equal(2);
|
||||||
// let wrapper;
|
expect(wrapper.vNode.child.disabled).to.be.false;
|
||||||
// afterEach(() => {
|
});
|
||||||
// wrapper && wrapper.destroy();
|
|
||||||
// });
|
|
||||||
|
|
||||||
// it('create a checkbox-group', () => {
|
it('emit a change event', (done) => {
|
||||||
// wrapper = mount(CheckboxGroup, {
|
wrapper = mount(CheckboxTestComponent);
|
||||||
// propsData: {}
|
|
||||||
// });
|
|
||||||
|
|
||||||
// expect(wrapper.hasClass('zan-checkbox-group')).to.be.true;
|
expect(wrapper.vNode.child.value.length).to.equal(2);
|
||||||
// });
|
const eventStub = sinon.stub(wrapper.vNode.child, '$emit');
|
||||||
|
|
||||||
// // it('emit a change event', () => {
|
wrapper.setData({
|
||||||
// // wrapper = mount(CheckboxGroup, {
|
'result': ['a']
|
||||||
// // propsData: {
|
});
|
||||||
// // value: false
|
wrapper.update();
|
||||||
// // }
|
wrapper.vm.$nextTick(() => {
|
||||||
// // });
|
expect(wrapper.vNode.child.value.length).to.equal(1);
|
||||||
|
expect(eventStub.calledOnce).to.be.true;
|
||||||
|
expect(eventStub.calledWith('change'));
|
||||||
|
done();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
// // const eventStub = sinon.stub(wrapper.vm, '$emit');
|
it('click on checked checkbox', (done) => {
|
||||||
|
wrapper = mount(CheckboxTestComponent);
|
||||||
|
|
||||||
// // wrapper.vm.value = true;
|
const eventStub = sinon.stub(wrapper.vNode.child, '$emit');
|
||||||
// // wrapper.update();
|
|
||||||
// // Vue.nextTick(() => {
|
const firstCheckboxLabel = wrapper.find('.zan-checkbox')[0].find('.zan-checkbox__label')[0];
|
||||||
// // expect(eventStub.calledOnce).to.be.true;
|
firstCheckboxLabel.simulate('click');
|
||||||
// // expect(eventStub.calledWith('change'));
|
|
||||||
// // done();
|
wrapper.update();
|
||||||
// // });
|
wrapper.vm.$nextTick(() => {
|
||||||
// // });
|
expect(eventStub.calledOnce).to.be.true;
|
||||||
// });
|
expect(eventStub.calledWith('input'));
|
||||||
|
done();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
it('click on unchecked checkbox', (done) => {
|
||||||
|
wrapper = mount(CheckboxTestComponent);
|
||||||
|
|
||||||
|
const eventStub = sinon.stub(wrapper.vNode.child, '$emit');
|
||||||
|
|
||||||
|
const lastCheckboxLabel = wrapper.find('.zan-checkbox')[3].find('.zan-checkbox__label')[0];
|
||||||
|
lastCheckboxLabel.simulate('click');
|
||||||
|
|
||||||
|
wrapper.update();
|
||||||
|
wrapper.vm.$nextTick(() => {
|
||||||
|
expect(eventStub.calledOnce).to.be.true;
|
||||||
|
expect(eventStub.calledWith('input'));
|
||||||
|
done();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('Checkbox', () => {
|
||||||
|
let wrapper;
|
||||||
|
afterEach(() => {
|
||||||
|
wrapper && wrapper.destroy();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('create a checkbox', () => {
|
||||||
|
wrapper = mount(Checkbox, {
|
||||||
|
propsData: {
|
||||||
|
value: false,
|
||||||
|
disabled: false
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(wrapper.hasClass('zan-checkbox')).to.be.true;
|
||||||
|
expect(wrapper.instance().currentValue).to.be.false;
|
||||||
|
expect(wrapper.instance().isDisabled).to.be.false;
|
||||||
|
expect(wrapper.instance().isChecked).to.be.false;
|
||||||
|
});
|
||||||
|
|
||||||
|
it('click on a checkbox', (done) => {
|
||||||
|
wrapper = mount(Checkbox, {
|
||||||
|
propsData: {
|
||||||
|
value: false,
|
||||||
|
disabled: false
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(wrapper.hasClass('zan-checkbox')).to.be.true;
|
||||||
|
const eventStub = sinon.stub(wrapper.vm, '$emit');
|
||||||
|
|
||||||
|
const checkboxLabel = wrapper.find('.zan-checkbox__label')[0];
|
||||||
|
checkboxLabel.simulate('click');
|
||||||
|
|
||||||
|
wrapper.update();
|
||||||
|
wrapper.vm.$nextTick(() => {
|
||||||
|
expect(eventStub.calledOnce).to.be.true;
|
||||||
|
expect(eventStub.calledWith('input'));
|
||||||
|
done();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
it('click on a disabled checkbox', () => {
|
||||||
|
wrapper = mount(Checkbox, {
|
||||||
|
propsData: {
|
||||||
|
value: false,
|
||||||
|
disabled: true
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(wrapper.hasClass('zan-checkbox')).to.be.true;
|
||||||
|
expect(wrapper.hasClass('zan-checkbox--disabled')).to.be.true;
|
||||||
|
expect(wrapper.instance().currentValue).to.be.false;
|
||||||
|
expect(wrapper.instance().isDisabled).to.be.true;
|
||||||
|
|
||||||
|
const checkboxLabel = wrapper.find('.zan-checkbox__label')[0];
|
||||||
|
checkboxLabel.simulate('click');
|
||||||
|
|
||||||
|
expect(wrapper.instance().currentValue).to.be.false;
|
||||||
|
});
|
||||||
|
});
|
||||||
|
139
test/unit/specs/radio.spec.js
Normal file
139
test/unit/specs/radio.spec.js
Normal file
@ -0,0 +1,139 @@
|
|||||||
|
import { mount } from 'avoriaz';
|
||||||
|
import Radio from 'packages/radio';
|
||||||
|
import RadioTestComponent from '../components/radio';
|
||||||
|
|
||||||
|
describe('RadioGroup', () => {
|
||||||
|
let wrapper;
|
||||||
|
afterEach(() => {
|
||||||
|
wrapper && wrapper.destroy();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('create a radio-group', () => {
|
||||||
|
wrapper = mount(RadioTestComponent);
|
||||||
|
|
||||||
|
expect(wrapper.hasClass('zan-radio-group')).to.be.true;
|
||||||
|
|
||||||
|
expect(wrapper.vNode.child.value).to.equal('1');
|
||||||
|
expect(wrapper.vNode.child.disabled).to.be.false;
|
||||||
|
});
|
||||||
|
|
||||||
|
it('emit a change event', (done) => {
|
||||||
|
wrapper = mount(RadioTestComponent);
|
||||||
|
|
||||||
|
expect(wrapper.vNode.child.value).to.equal('1');
|
||||||
|
const eventStub = sinon.stub(wrapper.vNode.child, '$emit');
|
||||||
|
|
||||||
|
wrapper.setData({
|
||||||
|
'radio': '2'
|
||||||
|
});
|
||||||
|
wrapper.update();
|
||||||
|
wrapper.vm.$nextTick(() => {
|
||||||
|
expect(wrapper.vNode.child.value).to.equal('2');
|
||||||
|
expect(eventStub.calledOnce).to.be.true;
|
||||||
|
expect(eventStub.calledWith('change'));
|
||||||
|
done();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
it('click on unchecked radio', (done) => {
|
||||||
|
wrapper = mount(RadioTestComponent);
|
||||||
|
|
||||||
|
const eventStub = sinon.stub(wrapper.vNode.child, '$emit');
|
||||||
|
|
||||||
|
const uncheckedRadioLabel = wrapper.find('.zan-radio')[1].find('.zan-radio__label')[0];
|
||||||
|
uncheckedRadioLabel.simulate('click');
|
||||||
|
|
||||||
|
wrapper.update();
|
||||||
|
wrapper.vm.$nextTick(() => {
|
||||||
|
expect(eventStub.calledOnce).to.be.true;
|
||||||
|
expect(eventStub.calledWith('input'));
|
||||||
|
done();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('Radio', () => {
|
||||||
|
let wrapper;
|
||||||
|
afterEach(() => {
|
||||||
|
wrapper && wrapper.destroy();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('create a radio', () => {
|
||||||
|
wrapper = mount(Radio, {
|
||||||
|
propsData: {
|
||||||
|
value: '1',
|
||||||
|
name: '1',
|
||||||
|
disabled: false
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(wrapper.hasClass('zan-radio')).to.be.true;
|
||||||
|
expect(wrapper.hasClass('zan-radio--disabled')).to.be.false;
|
||||||
|
expect(wrapper.instance().currentValue).to.equal('1');
|
||||||
|
expect(wrapper.instance().isDisabled).to.be.false;
|
||||||
|
});
|
||||||
|
|
||||||
|
it('click on a radio', (done) => {
|
||||||
|
wrapper = mount(Radio, {
|
||||||
|
propsData: {
|
||||||
|
value: '1',
|
||||||
|
name: '1',
|
||||||
|
disabled: false
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(wrapper.hasClass('zan-radio')).to.be.true;
|
||||||
|
const eventStub = sinon.stub(wrapper.vm, '$emit');
|
||||||
|
|
||||||
|
wrapper.simulate('click');
|
||||||
|
wrapper.update();
|
||||||
|
wrapper.vm.$nextTick(() => {
|
||||||
|
expect(eventStub.calledOnce).to.be.true;
|
||||||
|
expect(eventStub.calledWith('click'));
|
||||||
|
done();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
it('click on a radio label', (done) => {
|
||||||
|
wrapper = mount(Radio, {
|
||||||
|
propsData: {
|
||||||
|
value: '1',
|
||||||
|
name: '1',
|
||||||
|
disabled: false
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(wrapper.hasClass('zan-radio')).to.be.true;
|
||||||
|
const eventStub = sinon.stub(wrapper.vm, '$emit');
|
||||||
|
|
||||||
|
const checkboxLabel = wrapper.find('.zan-radio__label')[0];
|
||||||
|
checkboxLabel.simulate('click');
|
||||||
|
|
||||||
|
wrapper.update();
|
||||||
|
wrapper.vm.$nextTick(() => {
|
||||||
|
expect(eventStub.calledOnce).to.be.true;
|
||||||
|
expect(eventStub.calledWith('input'));
|
||||||
|
done();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
it('click on a disabled radio', () => {
|
||||||
|
wrapper = mount(Radio, {
|
||||||
|
propsData: {
|
||||||
|
value: '1',
|
||||||
|
name: '2',
|
||||||
|
disabled: true
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(wrapper.hasClass('zan-radio')).to.be.true;
|
||||||
|
expect(wrapper.hasClass('zan-radio--disabled')).to.be.true;
|
||||||
|
expect(wrapper.instance().currentValue).to.equal('1');
|
||||||
|
expect(wrapper.instance().isDisabled).to.be.true;
|
||||||
|
|
||||||
|
const checkboxLabel = wrapper.find('.zan-radio__label')[0];
|
||||||
|
checkboxLabel.simulate('click');
|
||||||
|
|
||||||
|
expect(wrapper.instance().currentValue).to.equal('1');
|
||||||
|
});
|
||||||
|
});
|
Loading…
x
Reference in New Issue
Block a user