[Improvement] add utils test cases (#380)

This commit is contained in:
neverland 2017-12-05 21:32:02 +08:00 committed by GitHub
parent 5a17bc520a
commit df835618b0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 191 additions and 67 deletions

View File

@ -19,9 +19,10 @@ export function get(object, path) {
const camelizeRE = /-(\w)/g;
export function camelize(str) {
return str.replace(camelizeRE, (_, c) => c ? c.toUpperCase() : '');
return str.replace(camelizeRE, (_, c) => c.toUpperCase());
}
export function isAndroid() {
/* istanbul ignore next */
return isServer ? false : /android/.test(navigator.userAgent.toLowerCase());
}

View File

@ -5,6 +5,8 @@
import { isServer } from './index';
let prev = Date.now();
/* istanbul ignore next */
function fallback(fn) {
const curr = Date.now();
const ms = Math.max(0, 16 - (curr - prev));
@ -13,11 +15,16 @@ function fallback(fn) {
return id;
}
/* istanbul ignore next */
const global = isServer ? global : window;
/* istanbul ignore next */
const iRaf =
global.requestAnimationFrame ||
global.webkitRequestAnimationFrame ||
fallback;
/* istanbul ignore next */
const iCancel =
global.cancelAnimationFrame ||
global.webkitCancelAnimationFrame ||

View File

@ -1,7 +1,6 @@
import Area from 'packages/area';
import { mount } from 'avoriaz';
import AreaList from '../mock/area.json';
import { setTimeout } from 'timers';
describe('Area', () => {
let wrapper;
@ -27,8 +26,6 @@ describe('Area', () => {
}
});
expect(wrapper.hasClass('van-area')).to.be.true;
const confirmBtn = wrapper.find('.van-picker__confirm')[0];
const eventStub = sinon.stub(wrapper.vm, '$emit');
@ -49,7 +46,6 @@ describe('Area', () => {
}
});
expect(wrapper.hasClass('van-area')).to.be.true;
expect(wrapper.vm.$refs.picker.getColumnValue(2).code).to.equal('110101');
wrapper.setProps({
@ -68,9 +64,7 @@ describe('Area', () => {
}
});
expect(wrapper.hasClass('van-area')).to.be.true;
expect(wrapper.vm.areaColumns.length).to.equal(0);
});
it('create an area with columnsNum equal 2', () => {
@ -81,7 +75,6 @@ describe('Area', () => {
}
});
expect(wrapper.hasClass('van-area')).to.be.true;
expect(wrapper.vm.areaColumns.length).to.equal(2);
});
@ -93,7 +86,6 @@ describe('Area', () => {
}
});
expect(wrapper.hasClass('van-area')).to.be.true;
expect(wrapper.vm.areaColumns.length).to.equal(1);
});
@ -104,7 +96,6 @@ describe('Area', () => {
}
});
expect(wrapper.hasClass('van-area')).to.be.true;
const cancelBtn = wrapper.find('.van-picker__cancel')[0];
const eventStub = sinon.stub(wrapper.vm, '$emit');
@ -115,4 +106,23 @@ describe('Area', () => {
done();
});
});
it('onChange method', () => {
wrapper = mount(Area, {
propsData: {
areaList: AreaList
}
});
let list = [];
const setColumnValues = (index, arr) => {
list = [...list, ...arr];
};
const code = { code: '110101' };
wrapper.vm.onChange({ setColumnValues }, [code], 0);
wrapper.vm.onChange({ setColumnValues }, [code, code], 1);
expect(list.length).to.equal(33);
});
});

View File

@ -3,17 +3,23 @@ import PickerColumn from 'packages/picker/PickerColumn';
import { mount } from 'avoriaz';
import { dragHelper } from '../utils';
const pickerColumns = [
const simpleColumn = ['1990', '1991', '1992', '1993', '1994', '1995'];
const columns = [
{
values: ['vip', 'normal'],
className: 'column1'
},
{
values: ['1990', '1991', '1992', '1993', '1994', '1995'],
values: simpleColumn,
className: 'column2'
}
];
const disabledOption = [{
disabled: true,
text: '123'
}];
describe('Picker', () => {
let wrapper;
afterEach(() => {
@ -23,7 +29,7 @@ describe('Picker', () => {
it('create picker', () => {
wrapper = mount(Picker, {
propsData: {
columns: pickerColumns
columns: columns
}
});
@ -36,7 +42,7 @@ describe('Picker', () => {
it('set picker values', () => {
wrapper = mount(Picker, {
propsData: {
columns: pickerColumns
columns: columns
}
});
@ -63,6 +69,7 @@ describe('Picker', () => {
expect(wrapper.vm.getColumnIndex(0)).to.equal(0);
expect(wrapper.vm.getColumnIndex(1)).to.equal(2);
expect(wrapper.vm.getColumnIndex(2)).to.equal(undefined);
expect(wrapper.vm.getIndexes(2)).to.eql([0, 2]);
wrapper.vm.setIndexes([1, 4]);
expect(wrapper.vm.getColumnValue(0)).to.equal('normal');
@ -70,7 +77,7 @@ describe('Picker', () => {
expect(wrapper.vm.getColumnValue(2)).to.equal(undefined);
});
it('create a invalid columns picker', () => {
it('create a simple column picker', () => {
wrapper = mount(Picker, {
propsData: {
columns: undefined
@ -81,10 +88,20 @@ describe('Picker', () => {
expect(wrapper.vm.currentColumns.length).to.equal(0);
});
it('create a invalid columns picker', () => {
wrapper = mount(Picker, {
propsData: {
columns: simpleColumn
}
});
expect(wrapper.vm.isSimpleColumn).to.be.true;
});
it('set invalid index columns', () => {
wrapper = mount(Picker, {
propsData: {
columns: pickerColumns
columns: columns
}
});
@ -100,7 +117,7 @@ describe('Picker', () => {
it('emit a change event when column change', (done) => {
wrapper = mount(Picker, {
propsData: {
columns: pickerColumns
columns: columns
}
});
@ -113,6 +130,22 @@ describe('Picker', () => {
});
});
it('simple column emit a change event when column change', (done) => {
wrapper = mount(Picker, {
propsData: {
columns: simpleColumn
}
});
const eventStub = sinon.stub(wrapper.vm, '$emit');
wrapper.vm.setColumnValue(0, '1993');
wrapper.vm.$nextTick(() => {
expect(eventStub.calledOnce).to.be.true;
expect(eventStub.calledWith('change'));
done();
});
});
it('create a empty picker and emit a cencel event', (done) => {
wrapper = mount(Picker, {
propsData: {
@ -155,6 +188,25 @@ describe('Picker', () => {
done();
});
});
it('simple column emit a confirm event', (done) => {
wrapper = mount(Picker, {
propsData: {
columns: simpleColumn,
showToolbar: true
}
});
const eventStub = sinon.stub(wrapper.vm, '$emit');
const cancelBtn = wrapper.find('.van-picker__confirm')[0];
cancelBtn.trigger('click');
wrapper.vm.$nextTick(() => {
expect(eventStub.calledOnce).to.be.true;
expect(eventStub.calledWith('confirm'));
done();
});
});
});
describe('PickerColumn', () => {
@ -199,6 +251,22 @@ describe('PickerColumn', () => {
});
});
it('change defaultIndex', (done) => {
wrapper = mount(PickerColumn, {
propsData: {
options: simpleColumn,
defaultIndex: 0
}
});
expect(wrapper.vm.currentIndex).to.equal(0);
wrapper.vm.defaultIndex = 2;
wrapper.vm.$nextTick(() => {
expect(wrapper.vm.currentIndex).to.equal(2);
done();
});
});
it('select disabled options', () => {
wrapper = mount(PickerColumn, {
propsData: {
@ -217,10 +285,21 @@ describe('PickerColumn', () => {
expect(wrapper.vm.currentIndex).to.equal(1);
});
it('disabled options', () => {
wrapper = mount(PickerColumn, {
propsData: {
options: disabledOption
}
});
expect(wrapper.find('.van-picker-column--disabled').length).to.equal(1);
expect(wrapper.vm.currentIndex).to.equal(undefined);
});
it('drag options', () => {
wrapper = mount(PickerColumn, {
propsData: {
options: pickerColumns[1].values
options: columns[1].values
}
});
expect(wrapper.vm.currentIndex).to.equal(0);

View File

@ -64,7 +64,7 @@ describe('SubmitBar', () => {
}, 300);
});
it('can not submit when disabled', () => {
it('can not submit when disabled', (done) => {
wrapper = mount(SubmitBar, {
propsData: {
disabled: true,
@ -77,10 +77,11 @@ describe('SubmitBar', () => {
wrapper.find('.van-button')[0].trigger('click');
setTimeout(() => {
expect(submitSpyFunc.calledOnce).to.be.false;
done();
}, 300);
});
it('can not submit when loading', () => {
it('can not submit when loading', (done) => {
wrapper = mount(SubmitBar, {
propsData: {
loading: true,
@ -93,6 +94,7 @@ describe('SubmitBar', () => {
wrapper.find('.van-button')[0].trigger('click');
setTimeout(() => {
expect(submitSpyFunc.calledOnce).to.be.false;
done();
}, 300);
});
});

View File

@ -95,7 +95,7 @@ describe('Tabs', () => {
active: 7
}
});
wrapper.vm.$nextTick(() => {
const nTab = wrapper.find('.van-tab')[6];
nTab.trigger('click');
@ -103,50 +103,6 @@ describe('Tabs', () => {
});
});
it('test swipe', (done) => {
wrapper = mount(MoreTabsTestComponent, {
attachToDocument: true
});
setTimeout(() => {
const nSwipe = wrapper.find('.van-tabs__swipe')[0];
const eventMouseObject = new window.Event('mousedown');
eventMouseObject.pageX = 200;
nSwipe.element.dispatchEvent(eventMouseObject);
const eventTouchObject = new window.Event('touchstart');
eventTouchObject.changedTouches = [{ pageX: 200 }];
nSwipe.element.dispatchEvent(eventTouchObject);
}, 500);
setTimeout(() => {
const nSwipe = wrapper.find('.van-tabs__swipe')[0];
const eventMouseMoveObject = new window.Event('mousemove');
eventMouseMoveObject.pageX = 0;
document.dispatchEvent(eventMouseMoveObject);
const eventObject = new window.Event('touchmove');
eventObject.changedTouches = [{ pageX: 0 }];
nSwipe.element.dispatchEvent(eventObject);
// 结束滑动
const eventMouseUpObject = new window.Event('mouseup');
document.dispatchEvent(eventMouseUpObject);
const eventEndObject = new window.Event('touchend');
eventEndObject.changedTouches = [{}];
nSwipe.element.dispatchEvent(eventEndObject);
}, 1000);
setTimeout(() => {
const nItem = wrapper.find('.van-tab')[0];
expect(nItem.hasClass('van-tab--active')).to.be.true;
done();
}, 1200);
});
it('watch tab props changes', (done) => {
wrapper = mount(TabsTestComponent);
wrapper.vm.firstTabTitle = '测试标题';

View File

@ -89,7 +89,7 @@ describe('Toast', () => {
it('toast disappeared after duration', (done) => {
Toast({
message: 'toast',
duration: 100
duration: 10
});
expect(document.querySelector('.van-toast-wrapper').style.display === 'none').to.be.false;
@ -97,7 +97,7 @@ describe('Toast', () => {
setTimeout(() => {
expect(document.querySelector('.van-toast-wrapper').style.display === 'none').to.be.true;
done();
}, 500);
}, 300);
});
it('toast duration 0', (done) => {

View File

@ -93,4 +93,16 @@ describe('Uploader', () => {
expect(wrapper.contains('input')).to.equal(true);
expect(wrapper.vm.onValueChange({ target: { files: [new File([], '/Users')] }})).to.equal(undefined);
});
it('unknown resultType', () => {
wrapper = mount(Uploader, {
propsData: {
disabled: false,
resultType: 'xxxx'
}
});
expect(wrapper.contains('input')).to.equal(true);
expect(wrapper.vm.onValueChange({ target: { files: [new File([], '/Users')] }})).to.equal(undefined);
});
});

View File

@ -0,0 +1,57 @@
import deepClone from 'packages/utils/deep-clone';
import { isAndroid, isDef, camelize, get } from 'packages/utils';
import { raf, cancel } from 'packages/utils/raf';
describe('Utils', () => {
it('deepClone', () => {
const a = { foo: 0 };
const b = { foo: 0, bar: 1 };
const fn = () => {};
const arr = [a, b];
expect(deepClone(a)).to.eql(a);
expect(deepClone(b)).to.eql(b);
expect(deepClone(fn)).to.eql(fn);
expect(deepClone(arr)).to.eql(arr);
expect(deepClone(undefined)).to.eql(undefined);
expect(deepClone(1)).to.eql(1);
});
it('isDef', () => {
expect(isDef(null)).to.be.false;
expect(isDef(undefined)).to.be.false;
expect(isDef(1)).to.be.true;
expect(isDef('1')).to.be.true;
expect(isDef({})).to.be.true;
expect(isDef(() => {})).to.be.true;
});
it('camelize', () => {
expect(camelize('ab')).to.equal('ab');
expect(camelize('a-b')).to.equal('aB');
expect(camelize('a-b-c-d')).to.equal('aBCD');
expect(camelize('a-b-')).to.equal('aB-');
expect(camelize('-a-b')).to.equal('AB');
expect(camelize('-')).to.equal('-');
});
it('get', () => {
expect(get({ a: 1 }, 'a')).to.equal(1);
expect(get({ a: { b: 2 }}, 'a.b')).to.equal(2);
expect(get({ a: { b: 2 }}, 'a.b.c')).to.equal('');
});
it('isAndroid', () => {
expect(isAndroid()).to.be.false;
});
it('raf', (done) => {
const spy = sinon.spy();
raf(spy);
setTimeout(() => {
expect(spy.calledOnce).to.be.true;
cancel(1);
done();
}, 50);
});
});