diff --git a/packages/actionsheet/test/index.spec.js b/packages/actionsheet/test/index.spec.js index 01f8e42b2..5b969b609 100644 --- a/packages/actionsheet/test/index.spec.js +++ b/packages/actionsheet/test/index.spec.js @@ -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(); }); diff --git a/packages/area/test/index.spec.js b/packages/area/test/index.spec.js index 198b3733e..b56fde89a 100644 --- a/packages/area/test/index.spec.js +++ b/packages/area/test/index.spec.js @@ -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 () => { diff --git a/packages/badge/test/index.spec.js b/packages/badge/test/index.spec.js index 4374d1179..039c60b6f 100644 --- a/packages/badge/test/index.spec.js +++ b/packages/badge/test/index.spec.js @@ -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(); }); diff --git a/packages/button/test/index.spec.js b/packages/button/test/index.spec.js index 15e380f44..7a03f4604 100644 --- a/packages/button/test/index.spec.js +++ b/packages/button/test/index.spec.js @@ -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); }); diff --git a/packages/cell/test/index.spec.js b/packages/cell/test/index.spec.js index 6ba3f7c10..8fa2bf153 100644 --- a/packages/cell/test/index.spec.js +++ b/packages/cell/test/index.spec.js @@ -12,7 +12,7 @@ test('click event', () => { }); wrapper.trigger('click'); - expect(click.mock.calls.length).toBeTruthy(); + expect(click).toHaveBeenCalled(); }); test('arrow direction', () => { diff --git a/packages/checkbox/test/index.spec.js b/packages/checkbox/test/index.spec.js index bb9311ad7..5c2572b60 100644 --- a/packages/checkbox/test/index.spec.js +++ b/packages/checkbox/test/index.spec.js @@ -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); }); diff --git a/packages/contact-card/test/index.spec.js b/packages/contact-card/test/index.spec.js index 4874af3a2..158e249ef 100644 --- a/packages/contact-card/test/index.spec.js +++ b/packages/contact-card/test/index.spec.js @@ -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); }); }); diff --git a/packages/coupon-list/test/index.spec.js b/packages/coupon-list/test/index.spec.js index b61c53f91..633b240fa 100644 --- a/packages/coupon-list/test/index.spec.js +++ b/packages/coupon-list/test/index.spec.js @@ -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', () => { diff --git a/packages/dialog/test/index.spec.js b/packages/dialog/test/index.spec.js index 271be399a..073256d08 100644 --- a/packages/dialog/test/index.spec.js +++ b/packages/dialog/test/index.spec.js @@ -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', () => { diff --git a/packages/field/test/index.spec.js b/packages/field/test/index.spec.js index 373621dce..c340e7f37 100644 --- a/packages/field/test/index.spec.js +++ b/packages/field/test/index.spec.js @@ -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 () => { diff --git a/packages/goods-action/test/index.spec.js b/packages/goods-action/test/index.spec.js index b5e3bc2c5..db7d97116 100644 --- a/packages/goods-action/test/index.spec.js +++ b/packages/goods-action/test/index.spec.js @@ -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', () => { diff --git a/packages/image-preview/test/index.spec.js b/packages/image-preview/test/index.spec.js index 01ac87d0c..60f4332ea 100644 --- a/packages/image-preview/test/index.spec.js +++ b/packages/image-preview/test/index.spec.js @@ -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(); }); }); diff --git a/packages/password-input/test/index.spec.js b/packages/password-input/test/index.spec.js index ece87241d..759caebac 100644 --- a/packages/password-input/test/index.spec.js +++ b/packages/password-input/test/index.spec.js @@ -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); }); diff --git a/packages/rate/test/index.spec.js b/packages/rate/test/index.spec.js index 33aff6b57..f0d5b379b 100644 --- a/packages/rate/test/index.spec.js +++ b/packages/rate/test/index.spec.js @@ -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); }); diff --git a/packages/search/test/index.spec.js b/packages/search/test/index.spec.js index c9ffcf998..9be9ecc9a 100644 --- a/packages/search/test/index.spec.js +++ b/packages/search/test/index.spec.js @@ -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', () => { diff --git a/packages/submit-bar/test/index.spec.js b/packages/submit-bar/test/index.spec.js index 5184bfe83..7d86306e1 100644 --- a/packages/submit-bar/test/index.spec.js +++ b/packages/submit-bar/test/index.spec.js @@ -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); }); diff --git a/packages/switch-cell/test/index.spec.js b/packages/switch-cell/test/index.spec.js index c7ae02914..70dda9556 100644 --- a/packages/switch-cell/test/index.spec.js +++ b/packages/switch-cell/test/index.spec.js @@ -13,5 +13,5 @@ test('change event', () => { wrapper.find('.van-switch').trigger('click'); - expect(onChange.mock.calls[0]).toBeTruthy(); + expect(onChange).toHaveBeenCalledWith(true); }); diff --git a/packages/switch/test/index.spec.js b/packages/switch/test/index.spec.js index 1b46f6a72..a05bdc9ff 100644 --- a/packages/switch/test/index.spec.js +++ b/packages/switch/test/index.spec.js @@ -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); }); diff --git a/packages/tab/test/index.spec.js b/packages/tab/test/index.spec.js index ee61313a9..c51f47b20 100644 --- a/packages/tab/test/index.spec.js +++ b/packages/tab/test/index.spec.js @@ -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); diff --git a/packages/tag/test/index.spec.js b/packages/tag/test/index.spec.js index fa8c4f304..cd3598103 100644 --- a/packages/tag/test/index.spec.js +++ b/packages/tag/test/index.spec.js @@ -12,5 +12,5 @@ test('click event', () => { }); wrapper.trigger('click'); - expect(click.mock.calls.length).toEqual(1); + expect(click).toHaveBeenCalledTimes(1); }); diff --git a/packages/toast/test/index.spec.js b/packages/toast/test/index.spec.js index 198297a6a..d2bc734c1 100644 --- a/packages/toast/test/index.spec.js +++ b/packages/toast/test/index.spec.js @@ -84,5 +84,5 @@ test('onClose callback', () => { toast.clear(); Toast.allowMultiple(false); - expect(onClose.mock.calls.length).toEqual(1); + expect(onClose).toHaveBeenCalledTimes(1); }); diff --git a/packages/tree-select/test/index.spec.js b/packages/tree-select/test/index.spec.js index 79f8b9295..17d130dbf 100644 --- a/packages/tree-select/test/index.spec.js +++ b/packages/tree-select/test/index.spec.js @@ -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); }); diff --git a/packages/uploader/test/index.spec.js b/packages/uploader/test/index.spec.js index 9863b08c9..b0891f82f 100644 --- a/packages/uploader/test/index.spec.js +++ b/packages/uploader/test/index.spec.js @@ -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(''); }); diff --git a/packages/utils/test/index.spec.js b/packages/utils/test/index.spec.js index 0689dbc9a..d696783d8 100644 --- a/packages/utils/test/index.spec.js +++ b/packages/utils/test/index.spec.js @@ -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); });