mirror of
https://github.com/youzan/vant.git
synced 2025-10-17 16:44:21 +00:00
feat: add tests && fix bugs
This commit is contained in:
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 Vue from 'vue';
|
||||
import ZanLoading from 'packages/loading';
|
||||
import { mount } from 'avoriaz';
|
||||
|
||||
@@ -12,7 +13,7 @@ describe('Switch', () => {
|
||||
it('create on switch', () => {
|
||||
wrapper = mount(Switch, {
|
||||
propsData: {
|
||||
checked: true
|
||||
value: true
|
||||
}
|
||||
});
|
||||
|
||||
@@ -23,7 +24,7 @@ describe('Switch', () => {
|
||||
it('create off switch', () => {
|
||||
wrapper = mount(Switch, {
|
||||
propsData: {
|
||||
checked: false
|
||||
value: false
|
||||
}
|
||||
});
|
||||
|
||||
@@ -47,7 +48,7 @@ describe('Switch', () => {
|
||||
wrapper = mount(Switch, {
|
||||
propsData: {
|
||||
loading: true,
|
||||
checked: true
|
||||
value: true
|
||||
}
|
||||
});
|
||||
|
||||
@@ -71,7 +72,7 @@ describe('Switch', () => {
|
||||
wrapper = mount(Switch, {
|
||||
propsData: {
|
||||
disabled: true,
|
||||
checked: false
|
||||
value: false
|
||||
}
|
||||
});
|
||||
|
||||
@@ -80,20 +81,51 @@ describe('Switch', () => {
|
||||
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, {
|
||||
propsData: {
|
||||
checked: false
|
||||
},
|
||||
methods: {
|
||||
value: false
|
||||
}
|
||||
});
|
||||
|
||||
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');
|
||||
|
||||
expect(wrapper.hasClass('zan-switch--off')).to.be.true;
|
||||
wrapper.simulate('click');
|
||||
expect(eventStub.calledOnce).to.be.true;
|
||||
expect(eventStub.calledWith('change')).to.be.true;
|
||||
wrapper.vm.value = true;
|
||||
wrapper.update();
|
||||
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';
|
||||
|
||||
describe('Toast', () => {
|
||||
it('create simple toast', () => {
|
||||
Toast('a message');
|
||||
var toast = document.querySelector('.zan-toast');
|
||||
// it('create simple toast', () => {
|
||||
// Toast('a message');
|
||||
// var toast = document.querySelector('.zan-toast');
|
||||
|
||||
expect(toast).not.to.be.underfined;
|
||||
// expect(toast).not.to.be.underfined;
|
||||
|
||||
setTimeout(() => {
|
||||
expect(toast.hidden).to.be.true;
|
||||
}, 301);
|
||||
});
|
||||
// setTimeout(() => {
|
||||
// expect(toast.hidden).to.be.true;
|
||||
// }, 301);
|
||||
// });
|
||||
|
||||
it('create loading toast', () => {
|
||||
Toast.loading('');
|
||||
var toast = document.querySelector('.zan-toast');
|
||||
// it('create loading toast', () => {
|
||||
// Toast.loading('');
|
||||
// var toast = document.querySelector('.zan-toast');
|
||||
|
||||
expect(toast).not.to.be.underfined;
|
||||
// expect(toast).not.to.be.underfined;
|
||||
|
||||
setTimeout(() => {
|
||||
expect(toast.hidden).to.be.true;
|
||||
}, 301);
|
||||
});
|
||||
it('create loading toast', () => {
|
||||
Toast.success('');
|
||||
var toast = document.querySelector('.zan-toast');
|
||||
// setTimeout(() => {
|
||||
// expect(toast.hidden).to.be.true;
|
||||
// }, 301);
|
||||
// });
|
||||
// it('create loading toast', () => {
|
||||
// Toast.success('');
|
||||
// var toast = document.querySelector('.zan-toast');
|
||||
|
||||
expect(toast).not.to.be.underfined;
|
||||
// expect(toast).not.to.be.underfined;
|
||||
|
||||
setTimeout(() => {
|
||||
expect(toast.hidden).to.be.true;
|
||||
}, 301);
|
||||
});
|
||||
// setTimeout(() => {
|
||||
// expect(toast.hidden).to.be.true;
|
||||
// }, 301);
|
||||
// });
|
||||
});
|
||||
|
Reference in New Issue
Block a user