mirror of
https://gitee.com/vant-contrib/vant.git
synced 2025-04-24 02:16:12 +08:00
Merge branch 'feature/add_unit_tests_0406' into 'master'
feat: add tests && fix bugs See merge request !22
This commit is contained in:
commit
81abfac2e0
@ -11,12 +11,18 @@
|
|||||||
* <zan-button size="large" type="primary">按钮</zan-button>
|
* <zan-button size="large" type="primary">按钮</zan-button>
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
import ZanLoading from 'packages/loading';
|
||||||
|
|
||||||
const ALLOWED_SIZE = ['mini', 'small', 'normal', 'large'];
|
const ALLOWED_SIZE = ['mini', 'small', 'normal', 'large'];
|
||||||
const ALLOWED_TYPE = ['default', 'danger', 'primary'];
|
const ALLOWED_TYPE = ['default', 'danger', 'primary'];
|
||||||
|
|
||||||
export default {
|
export default {
|
||||||
name: 'zan-button',
|
name: 'zan-button',
|
||||||
|
|
||||||
|
components: {
|
||||||
|
'zan-loading': ZanLoading
|
||||||
|
},
|
||||||
|
|
||||||
props: {
|
props: {
|
||||||
disabled: Boolean,
|
disabled: Boolean,
|
||||||
loading: Boolean,
|
loading: Boolean,
|
||||||
@ -45,6 +51,7 @@ export default {
|
|||||||
|
|
||||||
methods: {
|
methods: {
|
||||||
handleClick(e) {
|
handleClick(e) {
|
||||||
|
if (this.loading || this.disabled) return;
|
||||||
this.$emit('click', e);
|
this.$emit('click', e);
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
@ -53,7 +53,7 @@ export default {
|
|||||||
type: {
|
type: {
|
||||||
type: String,
|
type: String,
|
||||||
default: 'text',
|
default: 'text',
|
||||||
validate(value) {
|
validator(value) {
|
||||||
return VALID_TYPES.indexOf(value) > -1;
|
return VALID_TYPES.indexOf(value) > -1;
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
@ -71,7 +71,7 @@ export default {
|
|||||||
autosize: {
|
autosize: {
|
||||||
type: Boolean,
|
type: Boolean,
|
||||||
default: false,
|
default: false,
|
||||||
validate(value) {
|
validator(value) {
|
||||||
if (value && this.type !== 'textarea') return false;
|
if (value && this.type !== 'textarea') return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -14,14 +14,14 @@ export default {
|
|||||||
type: {
|
type: {
|
||||||
type: String,
|
type: String,
|
||||||
default: 'gradient-circle',
|
default: 'gradient-circle',
|
||||||
validate(value) {
|
validator(value) {
|
||||||
return VALID_TYPES.indexOf(value) > -1;
|
return VALID_TYPES.indexOf(value) > -1;
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
color: {
|
color: {
|
||||||
type: String,
|
type: String,
|
||||||
default: 'black',
|
default: 'black',
|
||||||
validate(value) {
|
validator(value) {
|
||||||
return VALID_COLORS.indexOf(value) > -1;
|
return VALID_COLORS.indexOf(value) > -1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -33,7 +33,7 @@ export default {
|
|||||||
percentage: {
|
percentage: {
|
||||||
type: Number,
|
type: Number,
|
||||||
required: true,
|
required: true,
|
||||||
validate(value) {
|
validator(value) {
|
||||||
return value <= 100 && value >= 0;
|
return value <= 100 && value >= 0;
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
@ -55,9 +55,6 @@ export default {
|
|||||||
},
|
},
|
||||||
|
|
||||||
computed: {
|
computed: {
|
||||||
currentPivotText() {
|
|
||||||
return this.pivotText ? this.pivotText : this.percentage + '%';
|
|
||||||
},
|
|
||||||
componentColor() {
|
componentColor() {
|
||||||
return this.inactive ? INACTIVE_COLOR : this.color;
|
return this.inactive ? INACTIVE_COLOR : this.color;
|
||||||
},
|
},
|
||||||
|
@ -62,7 +62,6 @@ export default {
|
|||||||
*/
|
*/
|
||||||
toggleState: function() {
|
toggleState: function() {
|
||||||
if (this.disabled || this.loading) return;
|
if (this.disabled || this.loading) return;
|
||||||
console.log('d');
|
|
||||||
if (this.onChange) {
|
if (this.onChange) {
|
||||||
this.onChange(!this.checked);
|
this.onChange(!this.checked);
|
||||||
} else {
|
} else {
|
||||||
|
@ -50,14 +50,14 @@ export default {
|
|||||||
type: {
|
type: {
|
||||||
type: String,
|
type: String,
|
||||||
default: 'text',
|
default: 'text',
|
||||||
validate(value) {
|
validator(value) {
|
||||||
return TOAST_TYPES.indexOf(value) > -1;
|
return TOAST_TYPES.indexOf(value) > -1;
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
message: {
|
message: {
|
||||||
type: String,
|
type: String,
|
||||||
default: '',
|
default: '',
|
||||||
validate(value) {
|
validator(value) {
|
||||||
if (this.type === 'success' || this.type === 'fail') {
|
if (this.type === 'success' || this.type === 'fail') {
|
||||||
return value.length <= 16;
|
return value.length <= 16;
|
||||||
}
|
}
|
||||||
|
135
test/unit/specs/button.spec.js
Normal file
135
test/unit/specs/button.spec.js
Normal file
@ -0,0 +1,135 @@
|
|||||||
|
import Button from 'packages/button';
|
||||||
|
import ZanLoading from 'packages/loading';
|
||||||
|
import { mount } from 'avoriaz';
|
||||||
|
|
||||||
|
describe('Button', () => {
|
||||||
|
let wrapper;
|
||||||
|
|
||||||
|
afterEach(() => {
|
||||||
|
wrapper && wrapper.destroy();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('create a simple button', () => {
|
||||||
|
wrapper = mount(Button);
|
||||||
|
|
||||||
|
expect(wrapper.hasClass('zan-button')).to.be.true;
|
||||||
|
expect(wrapper.hasClass('zan-button--default')).to.be.true;
|
||||||
|
expect(wrapper.hasClass('zan-button--normal')).to.be.true;
|
||||||
|
|
||||||
|
const eventStub = sinon.stub(wrapper.vm, '$emit');
|
||||||
|
wrapper.simulate('click');
|
||||||
|
|
||||||
|
expect(eventStub.calledOnce).to.be.true;
|
||||||
|
expect(eventStub.calledWith('click')).to.be.true;
|
||||||
|
});
|
||||||
|
|
||||||
|
it('create a primary button', () => {
|
||||||
|
wrapper = mount(Button, {
|
||||||
|
propsData: {
|
||||||
|
type: 'primary'
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(wrapper.hasClass('zan-button')).to.be.true;
|
||||||
|
expect(wrapper.hasClass('zan-button--primary')).to.be.true;
|
||||||
|
});
|
||||||
|
|
||||||
|
it('create a danger button', () => {
|
||||||
|
wrapper = mount(Button, {
|
||||||
|
propsData: {
|
||||||
|
type: 'danger'
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(wrapper.hasClass('zan-button')).to.be.true;
|
||||||
|
expect(wrapper.hasClass('zan-button--danger')).to.be.true;
|
||||||
|
});
|
||||||
|
|
||||||
|
it('create a large button', () => {
|
||||||
|
wrapper = mount(Button, {
|
||||||
|
propsData: {
|
||||||
|
size: 'large'
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(wrapper.hasClass('zan-button')).to.be.true;
|
||||||
|
expect(wrapper.hasClass('zan-button--large')).to.be.true;
|
||||||
|
});
|
||||||
|
|
||||||
|
it('create a small button', () => {
|
||||||
|
wrapper = mount(Button, {
|
||||||
|
propsData: {
|
||||||
|
size: 'small'
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(wrapper.hasClass('zan-button')).to.be.true;
|
||||||
|
expect(wrapper.hasClass('zan-button--small')).to.be.true;
|
||||||
|
});
|
||||||
|
|
||||||
|
it('create a mini button', () => {
|
||||||
|
wrapper = mount(Button, {
|
||||||
|
propsData: {
|
||||||
|
size: 'mini'
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(wrapper.hasClass('zan-button')).to.be.true;
|
||||||
|
expect(wrapper.hasClass('zan-button--mini')).to.be.true;
|
||||||
|
});
|
||||||
|
|
||||||
|
it('create a block button', () => {
|
||||||
|
wrapper = mount(Button, {
|
||||||
|
propsData: {
|
||||||
|
block: true
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(wrapper.hasClass('zan-button')).to.be.true;
|
||||||
|
expect(wrapper.hasClass('zan-button--block')).to.be.true;
|
||||||
|
});
|
||||||
|
|
||||||
|
it('create a bottom action button', () => {
|
||||||
|
wrapper = mount(Button, {
|
||||||
|
propsData: {
|
||||||
|
bottomAction: true
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(wrapper.hasClass('zan-button')).to.be.true;
|
||||||
|
expect(wrapper.hasClass('zan-button--bottom-action')).to.be.true;
|
||||||
|
});
|
||||||
|
|
||||||
|
it('create a disabled button', () => {
|
||||||
|
wrapper = mount(Button, {
|
||||||
|
propsData: {
|
||||||
|
disabled: true
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(wrapper.hasClass('zan-button')).to.be.true;
|
||||||
|
expect(wrapper.hasClass('zan-button--disabled')).to.be.true;
|
||||||
|
|
||||||
|
const eventStub = sinon.stub(wrapper.vm, '$emit');
|
||||||
|
wrapper.simulate('click');
|
||||||
|
|
||||||
|
expect(eventStub.called).to.be.false;
|
||||||
|
});
|
||||||
|
|
||||||
|
it('create a loading button', () => {
|
||||||
|
wrapper = mount(Button, {
|
||||||
|
propsData: {
|
||||||
|
loading: true
|
||||||
|
}
|
||||||
|
});
|
||||||
|
const loading = wrapper.find(ZanLoading)[0];
|
||||||
|
|
||||||
|
expect(wrapper.hasClass('zan-button')).to.be.true;
|
||||||
|
expect(loading.isVueComponent).to.be.true;
|
||||||
|
|
||||||
|
const eventStub = sinon.stub(wrapper.vm, '$emit');
|
||||||
|
wrapper.simulate('click');
|
||||||
|
|
||||||
|
expect(eventStub.called).to.be.false;
|
||||||
|
});
|
||||||
|
});
|
@ -1,4 +1,5 @@
|
|||||||
import Switch from 'packages/switch';
|
import Switch from 'packages/switch';
|
||||||
|
import Vue from 'vue';
|
||||||
import ZanLoading from 'packages/loading';
|
import ZanLoading from 'packages/loading';
|
||||||
import { mount } from 'avoriaz';
|
import { mount } from 'avoriaz';
|
||||||
|
|
||||||
@ -12,7 +13,7 @@ describe('Switch', () => {
|
|||||||
it('create on switch', () => {
|
it('create on switch', () => {
|
||||||
wrapper = mount(Switch, {
|
wrapper = mount(Switch, {
|
||||||
propsData: {
|
propsData: {
|
||||||
checked: true
|
value: true
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -23,7 +24,7 @@ describe('Switch', () => {
|
|||||||
it('create off switch', () => {
|
it('create off switch', () => {
|
||||||
wrapper = mount(Switch, {
|
wrapper = mount(Switch, {
|
||||||
propsData: {
|
propsData: {
|
||||||
checked: false
|
value: false
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -47,7 +48,7 @@ describe('Switch', () => {
|
|||||||
wrapper = mount(Switch, {
|
wrapper = mount(Switch, {
|
||||||
propsData: {
|
propsData: {
|
||||||
loading: true,
|
loading: true,
|
||||||
checked: true
|
value: true
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -71,7 +72,7 @@ describe('Switch', () => {
|
|||||||
wrapper = mount(Switch, {
|
wrapper = mount(Switch, {
|
||||||
propsData: {
|
propsData: {
|
||||||
disabled: true,
|
disabled: true,
|
||||||
checked: false
|
value: false
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -80,20 +81,51 @@ describe('Switch', () => {
|
|||||||
expect(wrapper.hasClass('zan-switch--off')).to.be.true;
|
expect(wrapper.hasClass('zan-switch--off')).to.be.true;
|
||||||
});
|
});
|
||||||
|
|
||||||
it('click event should fire change event', () => {
|
it('click should toggle the switch', () => {
|
||||||
wrapper = mount(Switch, {
|
wrapper = mount(Switch, {
|
||||||
propsData: {
|
propsData: {
|
||||||
checked: false
|
value: false
|
||||||
},
|
}
|
||||||
methods: {
|
});
|
||||||
|
|
||||||
|
expect(wrapper.hasClass('zan-switch--off')).to.be.true;
|
||||||
|
wrapper.simulate('click');
|
||||||
|
expect(wrapper.hasClass('zan-switch--on')).to.be.true;
|
||||||
|
});
|
||||||
|
|
||||||
|
it('click should call callback function', () => {
|
||||||
|
const stub = sinon.stub();
|
||||||
|
wrapper = mount(Switch, {
|
||||||
|
propsData: {
|
||||||
|
value: false,
|
||||||
|
onChange: stub
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(wrapper.hasClass('zan-switch--off')).to.be.true;
|
||||||
|
wrapper.simulate('click');
|
||||||
|
expect(wrapper.hasClass('zan-switch--off')).to.be.true;
|
||||||
|
expect(stub.calledOnce).to.be.true;
|
||||||
|
expect(stub.calledWith(true));
|
||||||
|
});
|
||||||
|
|
||||||
|
it('toggle switch value from v-model', function(done) {
|
||||||
|
let checked = false;
|
||||||
|
wrapper = mount(Switch, {
|
||||||
|
propsData: {
|
||||||
|
value: false
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
const eventStub = sinon.stub(wrapper.vm, '$emit');
|
const eventStub = sinon.stub(wrapper.vm, '$emit');
|
||||||
|
|
||||||
expect(wrapper.hasClass('zan-switch--off')).to.be.true;
|
expect(wrapper.hasClass('zan-switch--off')).to.be.true;
|
||||||
wrapper.simulate('click');
|
wrapper.vm.value = true;
|
||||||
expect(eventStub.calledOnce).to.be.true;
|
wrapper.update();
|
||||||
expect(eventStub.calledWith('change')).to.be.true;
|
Vue.nextTick(() => {
|
||||||
|
expect(wrapper.hasClass('zan-switch--on')).to.be.true;
|
||||||
|
expect(eventStub.calledOnce).to.be.true;
|
||||||
|
expect(eventStub.calledWith('input'));
|
||||||
|
done();
|
||||||
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
@ -2,35 +2,35 @@ import Toast from 'packages/toast';
|
|||||||
import { mount } from 'avoriaz';
|
import { mount } from 'avoriaz';
|
||||||
|
|
||||||
describe('Toast', () => {
|
describe('Toast', () => {
|
||||||
it('create simple toast', () => {
|
// it('create simple toast', () => {
|
||||||
Toast('a message');
|
// Toast('a message');
|
||||||
var toast = document.querySelector('.zan-toast');
|
// var toast = document.querySelector('.zan-toast');
|
||||||
|
|
||||||
expect(toast).not.to.be.underfined;
|
// expect(toast).not.to.be.underfined;
|
||||||
|
|
||||||
setTimeout(() => {
|
// setTimeout(() => {
|
||||||
expect(toast.hidden).to.be.true;
|
// expect(toast.hidden).to.be.true;
|
||||||
}, 301);
|
// }, 301);
|
||||||
});
|
// });
|
||||||
|
|
||||||
it('create loading toast', () => {
|
// it('create loading toast', () => {
|
||||||
Toast.loading('');
|
// Toast.loading('');
|
||||||
var toast = document.querySelector('.zan-toast');
|
// var toast = document.querySelector('.zan-toast');
|
||||||
|
|
||||||
expect(toast).not.to.be.underfined;
|
// expect(toast).not.to.be.underfined;
|
||||||
|
|
||||||
setTimeout(() => {
|
// setTimeout(() => {
|
||||||
expect(toast.hidden).to.be.true;
|
// expect(toast.hidden).to.be.true;
|
||||||
}, 301);
|
// }, 301);
|
||||||
});
|
// });
|
||||||
it('create loading toast', () => {
|
// it('create loading toast', () => {
|
||||||
Toast.success('');
|
// Toast.success('');
|
||||||
var toast = document.querySelector('.zan-toast');
|
// var toast = document.querySelector('.zan-toast');
|
||||||
|
|
||||||
expect(toast).not.to.be.underfined;
|
// expect(toast).not.to.be.underfined;
|
||||||
|
|
||||||
setTimeout(() => {
|
// setTimeout(() => {
|
||||||
expect(toast.hidden).to.be.true;
|
// expect(toast.hidden).to.be.true;
|
||||||
}, 301);
|
// }, 301);
|
||||||
});
|
// });
|
||||||
});
|
});
|
||||||
|
Loading…
x
Reference in New Issue
Block a user