[improvement] optimize jest fn called judgement (#3029)

This commit is contained in:
neverland 2019-03-21 18:57:44 +08:00 committed by GitHub
parent 7c4908a2e3
commit 8c051b8e1f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
24 changed files with 63 additions and 64 deletions

View File

@ -7,13 +7,15 @@ test('callback events', () => {
const onCancel = jest.fn();
const onSelect = jest.fn();
const actions = [
{ name: 'Option', callback },
{ name: 'Option', disabled: true }
];
const wrapper = mount(Actionsheet, {
propsData: {
value: true,
actions: [
{ name: 'Option', callback },
{ name: 'Option', disabled: true }
],
actions,
cancelText: 'Cancel'
},
context: {
@ -30,12 +32,10 @@ test('callback events', () => {
options.at(1).trigger('click');
wrapper.find('.van-actionsheet__cancel').trigger('click');
expect(callback.mock.calls.length).toBe(1);
expect(onCancel.mock.calls.length).toBeTruthy();
expect(onInput.mock.calls[0][0]).toBeFalsy();
expect(onSelect.mock.calls[0][0].name).toEqual('Option');
expect(onSelect.mock.calls[0][1]).toEqual(0);
expect(onSelect.mock.calls[0][1]).toBeFalsy();
expect(callback).toHaveBeenCalled();
expect(onCancel).toHaveBeenCalled();
expect(onInput).toHaveBeenCalledWith(false);
expect(onSelect).toHaveBeenCalledWith(actions[0], 0);
expect(wrapper).toMatchSnapshot();
});

View File

@ -26,8 +26,8 @@ test('confirm & cancel event', async () => {
wrapper.find('.van-picker__confirm').trigger('click');
wrapper.find('.van-picker__cancel').trigger('click');
expect(onConfirm.mock.calls[0][0]).toEqual(firstOption);
expect(onCancel.mock.calls[0][0]).toEqual(firstOption);
expect(onConfirm).toHaveBeenCalledWith(firstOption, [0, 0, 0]);
expect(onCancel).toHaveBeenCalledWith(firstOption, [0, 0, 0]);
});
test('watch areaList & code', async () => {

View File

@ -22,8 +22,8 @@ test('event', () => {
});
wrapper.find('.van-badge').trigger('click');
expect(onClick.mock.calls[0][0]).toBe(0);
expect(onChange.mock.calls[0][0]).toBe(0);
expect(onClick).toHaveBeenCalledWith(0);
expect(onChange).toHaveBeenCalledWith(0);
wrapper.vm.$destroy();
});

View File

@ -22,7 +22,7 @@ test('click event', () => {
});
wrapper.trigger('click');
expect(onClick.mock.calls.length).toEqual(1);
expect(onClick).toHaveBeenCalled();
});
test('not trigger click event when disabled', () => {
@ -39,7 +39,7 @@ test('not trigger click event when disabled', () => {
});
wrapper.trigger('click');
expect(onClick.mock.calls.length).toEqual(0);
expect(onClick).toHaveBeenCalledTimes(0);
});
test('not trigger click event when loading', () => {
@ -56,5 +56,5 @@ test('not trigger click event when loading', () => {
});
wrapper.trigger('click');
expect(onClick.mock.calls.length).toEqual(0);
expect(onClick).toHaveBeenCalledTimes(0);
});

View File

@ -12,7 +12,7 @@ test('click event', () => {
});
wrapper.trigger('click');
expect(click.mock.calls.length).toBeTruthy();
expect(click).toHaveBeenCalled();
});
test('arrow direction', () => {

View File

@ -80,9 +80,9 @@ test('click event', () => {
});
wrapper.trigger('click');
expect(onClick.mock.calls.length).toEqual(1);
expect(onClick).toHaveBeenCalledTimes(1);
const icon = wrapper.find('.van-checkbox__icon');
icon.trigger('click');
expect(onClick.mock.calls.length).toEqual(2);
expect(onClick).toHaveBeenCalledTimes(2);
});

View File

@ -20,7 +20,7 @@ describe('ContactCard', () => {
});
wrapper.trigger('click');
expect(click.mock.calls.length).toEqual(1);
expect(click).toHaveBeenCalledTimes(1);
});
test('not editable', () => {
@ -37,7 +37,7 @@ describe('ContactCard', () => {
});
wrapper.trigger('click');
expect(click.mock.calls.length).toEqual(0);
expect(click).toHaveBeenCalledTimes(0);
});
});

View File

@ -133,7 +133,7 @@ test('render coupon cell', () => {
expect(wrapper).toMatchSnapshot();
wrapper.trigger('click');
expect(onClick.mock.calls.length).toEqual(1);
expect(onClick).toHaveBeenCalledTimes(1);
});
test('render coupon cell with coupon', () => {

View File

@ -26,12 +26,12 @@ test('Dialog function call', async () => {
document.querySelector('.van-dialog__cancel').click();
await later();
expect(callback.mock.calls[0][0]).toEqual('cancel');
expect(callback).toHaveBeenCalledWith('cancel');
Dialog.confirm().then(callback);
document.querySelector('.van-dialog__confirm').click();
await later();
expect(callback.mock.calls[1][0]).toEqual('confirm');
expect(callback).toHaveBeenNthCalledWith(2, 'confirm');
});
test('before close', () => {

View File

@ -26,7 +26,7 @@ test('click icon event', () => {
expect(wrapper.emitted('click-icon')).toBeTruthy();
expect(wrapper.emitted('click-left-icon')).toBeTruthy();
expect(wrapper.emitted('click-right-icon')).toBeTruthy();
expect(onIconClick.mock.calls.length).toBe(1);
expect(onIconClick).toHaveBeenCalled();
});
test('keypress event', () => {
@ -111,7 +111,7 @@ test('blur method', () => {
wrapper.find('input').element.focus();
wrapper.vm.blur();
expect(fn.mock.calls.length).toEqual(1);
expect(fn).toHaveBeenCalledTimes(1);
});
test('focus method', () => {
@ -121,7 +121,7 @@ test('focus method', () => {
wrapper.vm.$on('focus', fn);
wrapper.vm.focus();
expect(fn.mock.calls.length).toEqual(1);
expect(fn).toHaveBeenCalledTimes(1);
});
test('maxlength', async () => {

View File

@ -13,7 +13,7 @@ test('BigBtn click event', () => {
});
wrapper.trigger('click');
expect(click.mock.calls.length).toEqual(1);
expect(click).toHaveBeenCalledTimes(1);
});
test('MiniBtn click event', () => {
@ -27,7 +27,7 @@ test('MiniBtn click event', () => {
});
wrapper.trigger('click');
expect(click.mock.calls.length).toEqual(1);
expect(click).toHaveBeenCalledTimes(1);
});
test('BigBtn render default slot', () => {

View File

@ -77,16 +77,14 @@ test('function call options', done => {
});
instance.$emit('input', true);
expect(onClose.mock.calls.length).toEqual(0);
expect(onClose).toHaveBeenCalledTimes(0);
Vue.nextTick(() => {
const wrapper = document.querySelector('.van-image-preview');
const swipe = wrapper.querySelector('.van-swipe__track');
triggerDrag(swipe, 0, 0);
expect(onClose.mock.calls.length).toEqual(1);
expect(onClose.mock.calls).toEqual([
[{ index: 0, url: 'https://img.yzcdn.cn/1.png' }]
]);
expect(onClose).toHaveBeenCalledTimes(1);
expect(onClose).toHaveBeenCalledWith({ index: 0, url: 'https://img.yzcdn.cn/1.png' });
done();
});
});

View File

@ -12,5 +12,5 @@ test('focus event', () => {
});
wrapper.find('.van-password-input__security').trigger('touchstart');
expect(focus.mock.calls.length).toEqual(1);
expect(focus).toHaveBeenCalledTimes(1);
});

View File

@ -16,8 +16,8 @@ test('change event', () => {
const item4 = wrapper.findAll('.van-rate__item').at(3);
item4.trigger('click');
expect(onInput.mock.calls[0][0]).toEqual(4);
expect(onChange.mock.calls[0][0]).toEqual(4);
expect(onInput).toHaveBeenCalledWith(4);
expect(onChange).toHaveBeenCalledWith(4);
});
test('disabled', () => {
@ -38,8 +38,8 @@ test('disabled', () => {
const item4 = wrapper.findAll('.van-rate__item').at(3);
item4.trigger('click');
expect(onInput.mock.calls.length).toEqual(0);
expect(onChange.mock.calls.length).toEqual(0);
expect(onInput).toHaveBeenCalledTimes(0);
expect(onChange).toHaveBeenCalledTimes(0);
});
test('touchmove', () => {
@ -62,5 +62,7 @@ test('touchmove', () => {
};
triggerDrag(wrapper, 100, 0);
expect(onChange.mock.calls).toEqual([[2], [3], [4]]);
expect(onChange).toHaveBeenNthCalledWith(1, 2);
expect(onChange).toHaveBeenNthCalledWith(2, 3);
expect(onChange).toHaveBeenNthCalledWith(3, 4);
});

View File

@ -15,7 +15,7 @@ test('listen input event', () => {
input.element.value = '1';
input.trigger('input');
expect(onInput).toBeCalledWith('1');
expect(onInput).toHaveBeenCalledWith('1');
});
test('cancel search', () => {
@ -38,8 +38,8 @@ test('cancel search', () => {
const cancel = wrapper.find('.van-search__action div');
cancel.trigger('click');
expect(onInput).toBeCalledWith('');
expect(onCancel).toBeCalled();
expect(onInput).toHaveBeenCalledWith('');
expect(onCancel).toHaveBeenCalled();
});
test('emit a search event', () => {
@ -59,8 +59,8 @@ test('emit a search event', () => {
input.trigger('keypress.enter');
input.trigger('keypress.a');
expect(onSearch).toBeCalled();
expect(onKeypress).toBeCalled();
expect(onSearch).toHaveBeenCalled();
expect(onKeypress).toHaveBeenCalled();
});
test('render label slot', () => {

View File

@ -14,7 +14,7 @@ test('submit', () => {
const button = wrapper.find('.van-button');
button.trigger('click');
expect(submit.mock.calls[0]).toBeTruthy();
expect(submit).toHaveBeenCalled();
});
test('disable submit', () => {
@ -34,5 +34,5 @@ test('disable submit', () => {
// disabled
const button = wrapper.find('.van-button');
button.trigger('click');
expect(submit.mock.calls[0]).toBeFalsy();
expect(submit).toHaveBeenCalledTimes(0);
});

View File

@ -13,5 +13,5 @@ test('change event', () => {
wrapper.find('.van-switch').trigger('click');
expect(onChange.mock.calls[0]).toBeTruthy();
expect(onChange).toHaveBeenCalledWith(true);
});

View File

@ -13,10 +13,9 @@ test('emit event', () => {
}
});
wrapper.trigger('click');
wrapper.trigger('click');
expect(input.mock.calls).toEqual([[true], [true]]);
expect(change.mock.calls).toEqual([[true], [true]]);
expect(input).toHaveBeenCalledWith(true);
expect(change).toHaveBeenCalledWith(true);
});
test('disabled', () => {
@ -35,6 +34,6 @@ test('disabled', () => {
});
wrapper.trigger('click');
expect(input.mock.calls.length).toBeFalsy();
expect(change.mock.calls.length).toBeFalsy();
expect(input).toHaveBeenCalledTimes(0);
expect(change).toHaveBeenCalledTimes(0);
});

View File

@ -58,7 +58,7 @@ test('click to switch tab', async () => {
tabs.at(2).trigger('click');
await later();
expect(wrapper).toMatchSnapshot();
expect(onChange.mock.calls.length).toEqual(1);
expect(onChange).toHaveBeenCalledTimes(1);
});
test('swipe to switch tab', async () => {
@ -74,7 +74,7 @@ test('swipe to switch tab', async () => {
expect(wrapper).toMatchSnapshot();
triggerDrag(content, -100, 0);
expect(wrapper).toMatchSnapshot();
expect(onChange.mock.calls.length).toEqual(1);
expect(onChange).toHaveBeenCalledTimes(1);
triggerDrag(content, -100, 0);
expect(wrapper).toMatchSnapshot();
triggerDrag(content, 100, 0);

View File

@ -12,5 +12,5 @@ test('click event', () => {
});
wrapper.trigger('click');
expect(click.mock.calls.length).toEqual(1);
expect(click).toHaveBeenCalledTimes(1);
});

View File

@ -84,5 +84,5 @@ test('onClose callback', () => {
toast.clear();
Toast.allowMultiple(false);
expect(onClose.mock.calls.length).toEqual(1);
expect(onClose).toHaveBeenCalledTimes(1);
});

View File

@ -31,7 +31,7 @@ test('select item', () => {
const items = wrapper.findAll('.van-tree-select__item');
items.at(0).trigger('click');
expect(onItemClick.mock.calls[0][0]).toEqual(item);
expect(onItemClick).toHaveBeenCalledWith(item);
items.at(1).trigger('click');
expect(onItemClick.mock.calls[1]).toBeFalsy();
expect(onItemClick).toHaveBeenCalledTimes(1);
});

View File

@ -31,7 +31,7 @@ test('disabled', () => {
});
wrapper.vm.onChange(file);
expect(afterRead.mock.calls.length).toBeFalsy();
expect(afterRead).toHaveBeenCalledTimes(0);
});
it('read text', done => {
@ -57,7 +57,7 @@ it('unknown resultType', () => {
}
});
wrapper.vm.onChange(file);
expect(afterRead.mock.calls.length).toBeFalsy();
expect(afterRead).toHaveBeenCalledTimes(0);
});
it('before read return false', () => {
@ -72,7 +72,7 @@ it('before read return false', () => {
const input = wrapper.find('input');
wrapper.vm.onChange(file);
expect(afterRead.mock.calls.length).toBeFalsy();
expect(afterRead).toHaveBeenCalledTimes(0);
expect(input.element.value).toEqual('');
});

View File

@ -53,7 +53,7 @@ test('raf', async () => {
raf(spy);
await later(50);
expect(spy.mock.calls.length).toBe(1);
expect(spy).toHaveBeenCalledTimes(1);
cancel(1);
});