From 7652355de0c01802624f2a1e030818cd23ffb062 Mon Sep 17 00:00:00 2001 From: Mathieu Suen Date: Wed, 7 Sep 2022 10:21:54 +0200 Subject: [PATCH] [NO JIRA] Migrate Checkbox enzyme test to RTL --- .../main/js/components/controls/Checkbox.tsx | 2 +- .../controls/__tests__/Checkbox-test.tsx | 134 ++++++------------ .../__snapshots__/Checkbox-test.tsx.snap | 39 +++-- 3 files changed, 77 insertions(+), 98 deletions(-) diff --git a/server/sonar-web/src/main/js/components/controls/Checkbox.tsx b/server/sonar-web/src/main/js/components/controls/Checkbox.tsx index ce2940fbfc4..c02a75414c3 100644 --- a/server/sonar-web/src/main/js/components/controls/Checkbox.tsx +++ b/server/sonar-web/src/main/js/components/controls/Checkbox.tsx @@ -93,7 +93,7 @@ export default class Checkbox extends React.PureComponent { } if (loading) { - return ; + return ; } return ( diff --git a/server/sonar-web/src/main/js/components/controls/__tests__/Checkbox-test.tsx b/server/sonar-web/src/main/js/components/controls/__tests__/Checkbox-test.tsx index dcd89ea0f75..cd987b866fa 100644 --- a/server/sonar-web/src/main/js/components/controls/__tests__/Checkbox-test.tsx +++ b/server/sonar-web/src/main/js/components/controls/__tests__/Checkbox-test.tsx @@ -17,101 +17,59 @@ * along with this program; if not, write to the Free Software Foundation, * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ -import { shallow } from 'enzyme'; +import { screen } from '@testing-library/react'; +import userEvent from '@testing-library/user-event'; + import * as React from 'react'; -import { click } from '../../../helpers/testUtils'; +import { renderComponent } from '../../../helpers/testReactTestingUtils'; import Checkbox from '../Checkbox'; -it('should render', () => { - const checkbox = shallow( {}} title="Title value" />); - expect(checkbox).toMatchSnapshot(); -}); - -it('should render unchecked', () => { - const checkbox = shallow( true} />); - expect(checkbox.is('.icon-checkbox-checked')).toBe(false); - expect(checkbox.prop('aria-checked')).toBe(false); -}); - -it('should render checked', () => { - const checkbox = shallow( true} />); - expect(checkbox.is('.icon-checkbox-checked')).toBe(true); - expect(checkbox.prop('aria-checked')).toBe(true); -}); - -it('should render disabled', () => { - const checkbox = shallow( true} />); - expect(checkbox.is('.icon-checkbox-disabled')).toBe(true); -}); - -it('should render unchecked third state', () => { - const checkbox = shallow( true} thirdState={true} />); - expect(checkbox.is('.icon-checkbox-single')).toBe(true); - expect(checkbox.is('.icon-checkbox-checked')).toBe(false); - expect(checkbox.prop('aria-checked')).toBe('mixed'); -}); - -it('should render checked third state', () => { - const checkbox = shallow( true} thirdState={true} />); - expect(checkbox.is('.icon-checkbox-single')).toBe(true); - expect(checkbox.is('.icon-checkbox-checked')).toBe(true); - expect(checkbox.prop('aria-checked')).toBe('mixed'); -}); - -it('should render with a spinner', () => { - const checkbox = shallow( true} />); - expect(checkbox.find('DeferredSpinner').exists()).toBe(true); -}); +describe.each([ + { children: null, describtion: 'with no children' }, + { children: child, describtion: 'with children' } +])('Checkbox $describtion', ({ children }) => { + it('should call check function', async () => { + const user = userEvent.setup(); + const onCheck = jest.fn(); + const rerender = renderCheckbox({ + label: 'me', + children, + onCheck, + checked: false, + title: 'title' + }); + await user.click(screen.getByRole('checkbox', { name: 'me' })); + expect(onCheck).toHaveBeenCalledWith(true, undefined); + expect(screen.getByTitle('title')).toBeInTheDocument(); + rerender({ checked: true }); + await user.click(screen.getByRole('checkbox', { name: 'me' })); + expect(onCheck).toHaveBeenCalledWith(false, undefined); + }); -it('should render children', () => { - const checkbox = shallow( - true}> - foo - - ); - expect(checkbox.hasClass('link-checkbox')).toBe(true); - expect(checkbox.find('span').exists()).toBe(true); -}); + it('should accept partial state', () => { + renderCheckbox({ label: 'me', thirdState: true, children, checked: false }); + expect(screen.getByRole('checkbox', { name: 'me' })).toHaveAttribute('aria-checked', 'mixed'); + }); -it('should render children with a spinner', () => { - const checkbox = shallow( - true}> - foo - - ); - expect(checkbox.hasClass('link-checkbox')).toBe(true); - expect(checkbox.find('span').exists()).toBe(true); - expect(checkbox.find('DeferredSpinner').exists()).toBe(true); + it('should render loading state', () => { + jest.useFakeTimers(); + renderCheckbox({ label: 'me', children, loading: true }); + jest.runAllTimers(); + expect(screen.getByLabelText('me')).toMatchSnapshot(); + jest.useRealTimers(); + }); }); -it('should call onCheck', () => { - const onCheck = jest.fn(); - const checkbox = shallow(); - click(checkbox); - expect(onCheck).toBeCalledWith(true, undefined); -}); - -it('should not call onCheck when disabled', () => { - const onCheck = jest.fn(); - const checkbox = shallow(); - click(checkbox); - expect(onCheck).toHaveBeenCalledTimes(0); -}); - -it('should call onCheck with id as second parameter', () => { - const onCheck = jest.fn(); - const checkbox = shallow(); - click(checkbox); - expect(onCheck).toBeCalledWith(true, 'foo'); +it('should render the checkbox on the right', () => { + renderCheckbox({ label: 'me', children: child, right: true }); + expect(screen.getByRole('checkbox', { name: 'me' })).toMatchSnapshot(); }); -it('should apply custom class', () => { - const checkbox = shallow( - true} /> +function renderCheckbox(override?: Partial) { + const { rerender } = renderComponent( + ); - expect(checkbox.is('.customclass')).toBe(true); -}); - -it('should render the checkbox on the right', () => { - expect(shallow( true} right={true} />)).toMatchSnapshot(); -}); + return function(reoverride?: Partial) { + rerender(); + }; +} diff --git a/server/sonar-web/src/main/js/components/controls/__tests__/__snapshots__/Checkbox-test.tsx.snap b/server/sonar-web/src/main/js/components/controls/__tests__/__snapshots__/Checkbox-test.tsx.snap index 69c0c60af8c..30df5c3539c 100644 --- a/server/sonar-web/src/main/js/components/controls/__tests__/__snapshots__/Checkbox-test.tsx.snap +++ b/server/sonar-web/src/main/js/components/controls/__tests__/__snapshots__/Checkbox-test.tsx.snap @@ -1,22 +1,43 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP -exports[`should render 1`] = ` +exports[`Checkbox with children should render loading state 1`] = ` + + + child + + +`; + +exports[`Checkbox with no children should render loading state 1`] = ` + `; exports[`should render the checkbox on the right 1`] = ` +> + + child + + + `; -- 2.39.5