actionsheet code review

This commit is contained in:
陈嘉涵
2017-08-17 14:08:21 +08:00
parent 4a55f341ab
commit 2ba123ed93
4 changed files with 111 additions and 81 deletions

View File

@@ -1,5 +1,6 @@
import ActionSheet from 'packages/actionsheet';
import { mount } from 'avoriaz';
import { DOMChecker } from '../utils';
describe('ActionSheet', () => {
let wrapper;
@@ -26,7 +27,13 @@ describe('ActionSheet', () => {
}
});
expect(wrapper.instance().currentValue).to.be.true;
DOMChecker(wrapper, {
noStyle: {
'.van-actionsheet': {
display: 'none'
}
}
});
});
it('create title type actionsheet', () => {
@@ -98,8 +105,8 @@ describe('ActionSheet', () => {
}
});
const cancelButton = wrapper.find('.van-actionsheet__button')[0];
expect(wrapper.contains('.van-actionsheet__button')).to.be.true;
const cancelButton = wrapper.find('.van-actionsheet__cancel')[0];
expect(wrapper.contains('.van-actionsheet__cancel')).to.be.true;
expect(cancelButton.text()).to.equal('cancel');
});
@@ -111,12 +118,24 @@ describe('ActionSheet', () => {
});
const eventStub = sinon.stub(wrapper.vm, '$emit');
expect(wrapper.data().currentValue).to.be.false;
DOMChecker(wrapper, {
style: {
'.van-actionsheet': {
display: 'none'
}
}
});
wrapper.vm.value = true;
wrapper.update();
wrapper.vm.$nextTick(() => {
expect(wrapper.data().currentValue).to.be.true;
DOMChecker(wrapper, {
noStyle: {
'.van-actionsheet': {
display: 'none'
}
}
});
expect(eventStub.calledWith('input'));
done();
});

46
test/unit/utils.js Normal file
View File

@@ -0,0 +1,46 @@
/**
* 按照一定的规则进行匹配
*/
export function DOMChecker(wrapper, rules) {
const { text, count, src, style, noStyle, value } = rules;
if (text) {
Object.keys(text).forEach(key => {
expect(wrapper.find(key)[0].text().trim()).to.equal(text[key]);
});
}
if (count) {
Object.keys(count).forEach(key => {
expect(wrapper.find(key).length).to.equal(count[key]);
});
}
if (src) {
Object.keys(src).forEach(key => {
expect(wrapper.find(key)[0].element.src).to.equal(src[key]);
});
}
if (value) {
Object.keys(value).forEach(key => {
expect(wrapper.find(key)[0].element.value).to.equal(value[key]);
});
}
if (style) {
Object.keys(style).forEach(key => {
Object.keys(style[key]).forEach(prop => {
expect(wrapper.find(key)[0].hasStyle(prop, style[key][prop])).to.equal(true);
});
});
}
if (noStyle) {
Object.keys(noStyle).forEach(key => {
Object.keys(noStyle[key]).forEach(prop => {
expect(wrapper.find(key)[0].hasStyle(prop, noStyle[key][prop])).to.equal(false);
});
});
}
}