From ad99b4322d333b8a9e33fbc6abc277b76a306136 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=81=8F=E5=8F=B3?= Date: Wed, 3 Apr 2019 15:54:26 +0800 Subject: [PATCH] test: :white_check_mark: Add eslint-plugin-jest (#15825) --- .eslintrc.js | 13 ++++++-- components/__tests__/index.test.js | 2 +- components/_util/__tests__/util.test.js | 6 ++-- components/alert/__tests__/index.test.js | 4 +-- .../auto-complete/__tests__/focus.test.js | 27 ++++++++++------- components/avatar/__tests__/Avatar.test.js | 2 +- components/calendar/__tests__/index.test.js | 8 ++--- components/card/__tests__/index.test.js | 6 ++-- components/carousel/__tests__/index.test.js | 8 ++--- components/cascader/__tests__/index.test.js | 2 +- components/checkbox/__tests__/group.test.js | 27 ++++++++--------- .../__tests__/components.test.js | 2 +- .../date-picker/__tests__/DatePicker.test.js | 6 ++-- .../date-picker/__tests__/RangePicker.test.js | 4 +-- .../drawer/__tests__/MultiDrawer.test.js | 2 +- components/form/__tests__/message.test.js | 2 +- components/grid/__tests__/index.test.js | 2 +- components/icon/__tests__/index.test.js | 8 ++--- components/input/__tests__/Search.test.js | 12 ++++---- components/input/__tests__/index.test.js | 8 ++--- components/layout/__tests__/index.test.js | 6 ++-- components/list/__tests__/pagination.test.js | 2 +- components/mention/__tests__/index.test.js | 10 +++---- components/message/__tests__/index.test.js | 3 +- components/modal/__tests__/Modal.test.js | 4 +-- components/modal/__tests__/confirm.test.js | 2 +- .../page-header/__tests__/index.test.js | 2 +- components/radio/__tests__/group.test.js | 8 ++--- components/statistic/__tests__/index.test.js | 8 ++--- .../table/__tests__/Table.filter.test.js | 6 ++-- .../table/__tests__/Table.pagination.test.js | 4 +-- .../__tests__/Table.rowSelection.test.js | 30 +++++++++++-------- components/table/__tests__/Table.test.js | 2 +- components/tabs/__tests__/index.test.js | 2 +- components/tag/__tests__/index.test.js | 2 +- .../time-picker/__tests__/index.test.js | 2 +- components/tooltip/__tests__/tooltip.test.js | 16 +++++----- components/transfer/__tests__/index.test.js | 2 +- components/transfer/__tests__/list.test.js | 2 +- components/transfer/__tests__/search.test.js | 4 +-- components/typography/__tests__/index.test.js | 12 ++++---- components/upload/__tests__/upload.test.js | 10 +++---- .../upload/__tests__/uploadlist.test.js | 18 +++++------ package.json | 1 + tests/shared/focusTest.js | 6 ++-- 45 files changed, 165 insertions(+), 150 deletions(-) diff --git a/.eslintrc.js b/.eslintrc.js index 8b675f955c..3f0a87c580 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -1,5 +1,9 @@ const eslintrc = { - extends: ['airbnb', 'prettier'], + extends: [ + 'airbnb', + 'prettier', + 'plugin:jest/recommended', + ], env: { browser: true, node: true, @@ -8,7 +12,12 @@ const eslintrc = { es6: true, }, parser: 'babel-eslint', - plugins: ['markdown', 'react', 'babel'], + plugins: [ + 'markdown', + 'react', + 'babel', + 'jest', + ], rules: { 'react/jsx-one-expression-per-line': 0, 'react/prop-types': 0, diff --git a/components/__tests__/index.test.js b/components/__tests__/index.test.js index 1716b3502c..bb11ad69f8 100644 --- a/components/__tests__/index.test.js +++ b/components/__tests__/index.test.js @@ -13,7 +13,7 @@ describe('antd', () => { }); it('should hint when import all components in dev mode', () => { - expect(warnSpy).toBeCalledWith( + expect(warnSpy).toHaveBeenCalledWith( 'You are using a whole package of antd, please use https://www.npmjs.com/package/babel-plugin-import to reduce app bundle size.', ); warnSpy.mockRestore(); diff --git a/components/_util/__tests__/util.test.js b/components/_util/__tests__/util.test.js index 965057d4f5..249a646b69 100644 --- a/components/_util/__tests__/util.test.js +++ b/components/_util/__tests__/util.test.js @@ -19,13 +19,13 @@ describe('Test utils function', () => { it('throttle function should work', () => { const callback = jest.fn(); const throttled = throttleByAnimationFrame(callback); - expect(callback).not.toBeCalled(); + expect(callback).not.toHaveBeenCalled(); throttled(); throttled(); jest.runAllTimers(); - expect(callback).toBeCalled(); + expect(callback).toHaveBeenCalled(); expect(callback.mock.calls.length).toBe(1); }); @@ -37,7 +37,7 @@ describe('Test utils function', () => { throttled.cancel(); jest.runAllTimers(); - expect(callback).not.toBeCalled(); + expect(callback).not.toHaveBeenCalled(); }); describe('getDataOrAriaProps', () => { diff --git a/components/alert/__tests__/index.test.js b/components/alert/__tests__/index.test.js index addaad4b58..9246fe0ef7 100644 --- a/components/alert/__tests__/index.test.js +++ b/components/alert/__tests__/index.test.js @@ -24,9 +24,9 @@ describe('Alert', () => { />, ); wrapper.find('.ant-alert-close-icon').simulate('click'); - expect(onClose).toBeCalled(); + expect(onClose).toHaveBeenCalled(); jest.runAllTimers(); - expect(afterClose).toBeCalled(); + expect(afterClose).toHaveBeenCalled(); }); describe('data and aria props', () => { diff --git a/components/auto-complete/__tests__/focus.test.js b/components/auto-complete/__tests__/focus.test.js index 4d234b869a..7be7624fa4 100644 --- a/components/auto-complete/__tests__/focus.test.js +++ b/components/auto-complete/__tests__/focus.test.js @@ -28,23 +28,28 @@ describe('AutoComplete children could be focus', () => { it('focus() and onFocus', () => { const handleFocus = jest.fn(); - const wrapper = mount( - - , { attachTo: container }); - wrapper.find('input').instance().focus(); + const wrapper = mount(, { attachTo: container }); + wrapper + .find('input') + .instance() + .focus(); jest.runAllTimers(); - expect(handleFocus).toBeCalled(); + expect(handleFocus).toHaveBeenCalled(); }); it('blur() and onBlur', () => { const handleBlur = jest.fn(); - const wrapper = mount( - - , { attachTo: container }); - wrapper.find('input').instance().focus(); + const wrapper = mount(, { attachTo: container }); + wrapper + .find('input') + .instance() + .focus(); jest.runAllTimers(); - wrapper.find('input').instance().blur(); + wrapper + .find('input') + .instance() + .blur(); jest.runAllTimers(); - expect(handleBlur).toBeCalled(); + expect(handleBlur).toHaveBeenCalled(); }); }); diff --git a/components/avatar/__tests__/Avatar.test.js b/components/avatar/__tests__/Avatar.test.js index c703c52a4f..18ccd9ab8c 100644 --- a/components/avatar/__tests__/Avatar.test.js +++ b/components/avatar/__tests__/Avatar.test.js @@ -46,7 +46,7 @@ describe('Avatar Render', () => { const children = wrapper.find('.ant-avatar-string'); expect(children.length).toBe(1); expect(children.text()).toBe('Fallback'); - expect(wrapper.instance().setScale).toBeCalled(); + expect(wrapper.instance().setScale).toHaveBeenCalled(); expect(div.querySelector('.ant-avatar-string').style.transform).toContain('scale(0.5)'); wrapper.detach(); diff --git a/components/calendar/__tests__/index.test.js b/components/calendar/__tests__/index.test.js index 5a25b42e96..0352758673 100644 --- a/components/calendar/__tests__/index.test.js +++ b/components/calendar/__tests__/index.test.js @@ -13,7 +13,7 @@ describe('Calendar', () => { .find('.ant-fullcalendar-cell') .at(0) .simulate('click'); - expect(onSelect).toBeCalledWith(expect.anything()); + expect(onSelect).toHaveBeenCalledWith(expect.anything()); const value = onSelect.mock.calls[0][0]; expect(Moment.isMoment(value)).toBe(true); }); @@ -159,7 +159,7 @@ describe('Calendar', () => { .at(0) .simulate('click'); - expect(onPanelChange).toBeCalled(); + expect(onPanelChange).toHaveBeenCalled(); expect(onPanelChange.mock.calls[0][0].month()).toEqual(date.month() - 1); }); @@ -173,7 +173,7 @@ describe('Calendar', () => { wrapper.find('.ant-radio-button-input[value="year"]').simulate('change'); expect(wrapper.find('.ant-fullcalendar-table').length).toBe(0); expect(wrapper.find('.ant-fullcalendar-month-panel-table').length).toBe(1); - expect(onPanelChange).toBeCalled(); + expect(onPanelChange).toHaveBeenCalled(); expect(onPanelChange.mock.calls[0][1]).toEqual('year'); }); @@ -254,6 +254,6 @@ describe('Calendar', () => { .find('input') .at(1) .simulate('change'); - expect(onTypeChange).toBeCalledWith('year'); + expect(onTypeChange).toHaveBeenCalledWith('year'); }); }); diff --git a/components/card/__tests__/index.test.js b/components/card/__tests__/index.test.js index 30e72ac0ca..dd35ff63e9 100644 --- a/components/card/__tests__/index.test.js +++ b/components/card/__tests__/index.test.js @@ -33,11 +33,11 @@ describe('Card', () => { it('warning', () => { const warnSpy = jest.spyOn(console, 'error').mockImplementation(() => {}); mount(xxx); - expect(warnSpy).toBeCalledWith( + expect(warnSpy).toHaveBeenCalledWith( 'Warning: [antd: Card] `noHovering` is deprecated, you can remove it safely or use `hoverable` instead.', ); mount(xxx); - expect(warnSpy).toBeCalledWith( + expect(warnSpy).toHaveBeenCalledWith( 'Warning: [antd: Card] `noHovering={false}` is deprecated, use `hoverable` instead.', ); warnSpy.mockRestore(); @@ -64,7 +64,7 @@ describe('Card', () => { .find('.ant-tabs-tab') .at(1) .simulate('click'); - expect(onTabChange).toBeCalledWith('tab2'); + expect(onTabChange).toHaveBeenCalledWith('tab2'); }); it('getCompatibleHoverable should work', () => { diff --git a/components/carousel/__tests__/index.test.js b/components/carousel/__tests__/index.test.js index a07af6f740..b7d762e942 100644 --- a/components/carousel/__tests__/index.test.js +++ b/components/carousel/__tests__/index.test.js @@ -58,9 +58,9 @@ describe('Carousel', () => { ); const spy = jest.spyOn(wrapper.instance().slick.innerSlider, 'autoPlay'); window.resizeTo(1000); - expect(spy).not.toBeCalled(); + expect(spy).not.toHaveBeenCalled(); await new Promise(resolve => setTimeout(resolve, 500)); - expect(spy).toBeCalled(); + expect(spy).toHaveBeenCalled(); }); it('cancel resize listener when unmount', async () => { @@ -75,7 +75,7 @@ describe('Carousel', () => { const spy = jest.spyOn(wrapper.instance().onWindowResized, 'cancel'); const spy2 = jest.spyOn(window, 'removeEventListener'); wrapper.unmount(); - expect(spy).toBeCalled(); - expect(spy2).toBeCalledWith('resize', onWindowResized); + expect(spy).toHaveBeenCalled(); + expect(spy2).toHaveBeenCalledWith('resize', onWindowResized); }); }); diff --git a/components/cascader/__tests__/index.test.js b/components/cascader/__tests__/index.test.js index 29f02344f8..973c991338 100644 --- a/components/cascader/__tests__/index.test.js +++ b/components/cascader/__tests__/index.test.js @@ -424,7 +424,7 @@ describe('Cascader', () => { wrapper.find('input').simulate('click'); wrapper.find('input').simulate('change', { target: { value: 'a' } }); expect(wrapper.find('.ant-cascader-menu-item').length).toBe(2); - expect(errorSpy).toBeCalledWith( + expect(errorSpy).toHaveBeenCalledWith( "Warning: [antd: Cascader] 'limit' of showSearch should be positive number or false.", ); }); diff --git a/components/checkbox/__tests__/group.test.js b/components/checkbox/__tests__/group.test.js index 96a05628e3..4b264323b9 100644 --- a/components/checkbox/__tests__/group.test.js +++ b/components/checkbox/__tests__/group.test.js @@ -12,22 +12,22 @@ describe('CheckboxGroup', () => { .find('.ant-checkbox-input') .at(0) .simulate('change'); - expect(onChange).toBeCalledWith(['Apple']); + expect(onChange).toHaveBeenCalledWith(['Apple']); wrapper .find('.ant-checkbox-input') .at(1) .simulate('change'); - expect(onChange).toBeCalledWith(['Apple', 'Pear']); + expect(onChange).toHaveBeenCalledWith(['Apple', 'Pear']); wrapper .find('.ant-checkbox-input') .at(2) .simulate('change'); - expect(onChange).toBeCalledWith(['Apple', 'Pear', 'Orange']); + expect(onChange).toHaveBeenCalledWith(['Apple', 'Pear', 'Orange']); wrapper .find('.ant-checkbox-input') .at(1) .simulate('change'); - expect(onChange).toBeCalledWith(['Apple', 'Orange']); + expect(onChange).toHaveBeenCalledWith(['Apple', 'Orange']); }); it('does not trigger onChange callback of both Checkbox and CheckboxGroup when CheckboxGroup is disabled', () => { @@ -42,12 +42,12 @@ describe('CheckboxGroup', () => { .find('.ant-checkbox-input') .at(0) .simulate('change'); - expect(onChangeGroup).not.toBeCalled(); + expect(onChangeGroup).not.toHaveBeenCalled(); groupWrapper .find('.ant-checkbox-input') .at(1) .simulate('change'); - expect(onChangeGroup).not.toBeCalled(); + expect(onChangeGroup).not.toHaveBeenCalled(); }); it('does not prevent onChange callback from Checkbox when CheckboxGroup is not disabled', () => { @@ -63,22 +63,19 @@ describe('CheckboxGroup', () => { .find('.ant-checkbox-input') .at(0) .simulate('change'); - expect(onChangeGroup).toBeCalledWith(['Apple']); + expect(onChangeGroup).toHaveBeenCalledWith(['Apple']); groupWrapper .find('.ant-checkbox-input') .at(1) .simulate('change'); - expect(onChangeGroup).toBeCalledWith(['Apple']); + expect(onChangeGroup).toHaveBeenCalledWith(['Apple']); }); it('all children should have a name property', () => { const wrapper = mount(); - - expect( - wrapper.find('input[type="checkbox"]').forEach(el => { - expect(el.props().name).toEqual('checkboxgroup'); - }), - ); + wrapper.find('input[type="checkbox"]').forEach(el => { + expect(el.props().name).toEqual('checkboxgroup'); + }); }); it('passes prefixCls down to checkbox', () => { @@ -111,7 +108,7 @@ describe('CheckboxGroup', () => { .find('.ant-checkbox-input') .at(0) .simulate('change'); - expect(onChange).toBeCalled(); + expect(onChange).toHaveBeenCalled(); expect(onChange.mock.calls[0][0].target.value).toEqual('my'); }); }); diff --git a/components/config-provider/__tests__/components.test.js b/components/config-provider/__tests__/components.test.js index 0aed6c1be0..05c7e23744 100644 --- a/components/config-provider/__tests__/components.test.js +++ b/components/config-provider/__tests__/components.test.js @@ -60,7 +60,7 @@ jest.mock('rc-util/lib/Portal'); describe('ConfigProvider', () => { describe('components', () => { function testPair(name, renderComponent) { - describe(name, () => { + describe(`${name}`, () => { // normal it('normal', () => { expect(render(renderComponent({}))).toMatchSnapshot(); diff --git a/components/date-picker/__tests__/DatePicker.test.js b/components/date-picker/__tests__/DatePicker.test.js index 9f8041aef9..dbb3f6e428 100644 --- a/components/date-picker/__tests__/DatePicker.test.js +++ b/components/date-picker/__tests__/DatePicker.test.js @@ -114,11 +114,11 @@ describe('DatePicker', () => { const wrapper = mount(); openPanel(wrapper); nextYear(wrapper); - expect(handleChange).not.toBeCalled(); + expect(handleChange).not.toHaveBeenCalled(); nextMonth(wrapper); - expect(handleChange).not.toBeCalled(); + expect(handleChange).not.toHaveBeenCalled(); selectDate(wrapper, moment('2017-12-22')); - expect(handleChange).toBeCalled(); + expect(handleChange).toHaveBeenCalled(); }); it('clear input', () => { diff --git a/components/date-picker/__tests__/RangePicker.test.js b/components/date-picker/__tests__/RangePicker.test.js index 3ee345fdd1..43e22a99a5 100644 --- a/components/date-picker/__tests__/RangePicker.test.js +++ b/components/date-picker/__tests__/RangePicker.test.js @@ -260,7 +260,7 @@ describe('RangePicker', () => { const wrapper = mount(); wrapper.find('.ant-calendar-picker-input').simulate('click'); wrapper.find('.ant-calendar-range-quick-selector Tag').simulate('click'); - expect(handleOk).toBeCalledWith(range); + expect(handleOk).toHaveBeenCalledWith(range); }); // https://github.com/ant-design/ant-design/issues/9267 @@ -312,7 +312,7 @@ describe('RangePicker', () => { ); wrapper.find('.ant-calendar-picker-input').simulate('click'); wrapper.find('.ant-calendar-range-quick-selector Tag').simulate('click'); - expect(handleOpenChange).toBeCalledWith(false); + expect(handleOpenChange).toHaveBeenCalledWith(false); }); it('customize separator', () => { diff --git a/components/drawer/__tests__/MultiDrawer.test.js b/components/drawer/__tests__/MultiDrawer.test.js index c40325f80b..48cf5c5925 100644 --- a/components/drawer/__tests__/MultiDrawer.test.js +++ b/components/drawer/__tests__/MultiDrawer.test.js @@ -113,7 +113,7 @@ describe('Drawer', () => { expect(wrapper.state().childrenDrawer).toBe(false); }); - it('render left MultiDrawer', () => { + it('render top MultiDrawer', () => { const wrapper = mount(); wrapper.find('button#open_drawer').simulate('click'); wrapper.find('button#open_two_drawer').simulate('click'); diff --git a/components/form/__tests__/message.test.js b/components/form/__tests__/message.test.js index 054302b1cc..bfc63c47f9 100644 --- a/components/form/__tests__/message.test.js +++ b/components/form/__tests__/message.test.js @@ -102,7 +102,7 @@ describe('Form', () => { }); mount(); - expect(errorSpy).toBeCalledWith( + expect(errorSpy).toHaveBeenCalledWith( 'Warning: [antd: Form.Item] Cannot generate `validateStatus` and `help` automatically, while there are more than one `getFieldDecorator` in it.', ); errorSpy.mockRestore(); diff --git a/components/grid/__tests__/index.test.js b/components/grid/__tests__/index.test.js index fdc8d58d54..f0d42d6b67 100644 --- a/components/grid/__tests__/index.test.js +++ b/components/grid/__tests__/index.test.js @@ -47,7 +47,7 @@ describe('Grid', () => { .prop('style'), ).toEqual(undefined); wrapper.unmount(); - expect(enquire.unregister).toBeCalledTimes(6); + expect(enquire.unregister).toHaveBeenCalledTimes(6); }); it('renders wrapped Col correctly', () => { diff --git a/components/icon/__tests__/index.test.js b/components/icon/__tests__/index.test.js index f2623fd553..5162b6447e 100644 --- a/components/icon/__tests__/index.test.js +++ b/components/icon/__tests__/index.test.js @@ -80,11 +80,11 @@ describe('Icon', () => { expect(wrapper.find('i')).toHaveLength(1); const icon = wrapper.find('i').at(0); icon.simulate('mouseenter'); - expect(onVisibleChange).toBeCalledWith(true); + expect(onVisibleChange).toHaveBeenCalledWith(true); expect(wrapper.instance().tooltip.props.visible).toBe(true); icon.simulate('mouseleave'); - expect(onVisibleChange).toBeCalledWith(false); + expect(onVisibleChange).toHaveBeenCalledWith(false); expect(wrapper.instance().tooltip.props.visible).toBe(false); }); @@ -115,12 +115,12 @@ describe('Icon', () => { it('does not warn', () => { mount(); - expect(errorSpy).not.toBeCalled(); + expect(errorSpy).not.toHaveBeenCalled(); }); it('warns', () => { mount(); - expect(errorSpy).toBeCalledWith( + expect(errorSpy).toHaveBeenCalledWith( "Warning: [antd: Icon] The icon name 'clock-circle-o' already specify a theme 'outlined', the 'theme' prop 'filled' will be ignored.", ); }); diff --git a/components/input/__tests__/Search.test.js b/components/input/__tests__/Search.test.js index e469f5714c..ef0cef7c52 100644 --- a/components/input/__tests__/Search.test.js +++ b/components/input/__tests__/Search.test.js @@ -34,7 +34,7 @@ describe('Input.Search', () => { const wrapper = mount(); wrapper.find('.anticon-search').simulate('click'); expect(onSearch).toHaveBeenCalledTimes(1); - expect(onSearch).toBeCalledWith( + expect(onSearch).toHaveBeenCalledWith( 'search text', expect.objectContaining({ type: 'click', @@ -48,7 +48,7 @@ describe('Input.Search', () => { const wrapper = mount(); wrapper.find('Button').simulate('click'); expect(onSearch).toHaveBeenCalledTimes(1); - expect(onSearch).toBeCalledWith( + expect(onSearch).toHaveBeenCalledWith( 'search text', expect.objectContaining({ type: 'click', @@ -64,7 +64,7 @@ describe('Input.Search', () => { ); wrapper.find('Button').simulate('click'); expect(onSearch).toHaveBeenCalledTimes(1); - expect(onSearch).toBeCalledWith( + expect(onSearch).toHaveBeenCalledWith( 'search text', expect.objectContaining({ type: 'click', @@ -84,7 +84,7 @@ describe('Input.Search', () => { ); wrapper.find('Button').simulate('click'); expect(onSearch).toHaveBeenCalledTimes(1); - expect(onSearch).toBeCalledWith( + expect(onSearch).toHaveBeenCalledWith( 'search text', expect.objectContaining({ type: 'click', @@ -104,7 +104,7 @@ describe('Input.Search', () => { ); wrapper.find('button').simulate('click'); expect(onSearch).toHaveBeenCalledTimes(1); - expect(onSearch).toBeCalledWith( + expect(onSearch).toHaveBeenCalledWith( 'search text', expect.objectContaining({ type: 'click', @@ -118,7 +118,7 @@ describe('Input.Search', () => { const wrapper = mount(); wrapper.find('input').simulate('keydown', { key: 'Enter', keyCode: 13 }); expect(onSearch).toHaveBeenCalledTimes(1); - expect(onSearch).toBeCalledWith( + expect(onSearch).toHaveBeenCalledWith( 'search text', expect.objectContaining({ type: 'keydown', diff --git a/components/input/__tests__/index.test.js b/components/input/__tests__/index.test.js index faaca4c6eb..6509815a43 100644 --- a/components/input/__tests__/index.test.js +++ b/components/input/__tests__/index.test.js @@ -41,7 +41,7 @@ describe('Input', () => { wrapper.setProps({ suffix: 'light', }); - expect(errorSpy).not.toBeCalled(); + expect(errorSpy).not.toHaveBeenCalled(); }); it('trigger warning', () => { const wrapper = mount(); @@ -52,7 +52,7 @@ describe('Input', () => { wrapper.setProps({ suffix: 'light', }); - expect(errorSpy).toBeCalledWith( + expect(errorSpy).toHaveBeenCalledWith( 'Warning: [antd: Input] When Input is focused, dynamic add or remove prefix / suffix will make it lose focus caused by dom structure change. Read more: https://ant.design/components/input/#FAQ', ); }); @@ -168,8 +168,8 @@ describe('TextArea', () => {