From a7cc8786c3fa1b57bc3ac191c0808253dff414ea Mon Sep 17 00:00:00 2001 From: Alejandro Celaya Date: Sat, 14 May 2022 11:11:50 +0200 Subject: [PATCH] Migrated ExportBtn test to react testing library, and changed icon --- src/utils/ExportBtn.tsx | 4 +- test/utils/ExportBtn.test.tsx | 45 +++++++------------ .../__snapshots__/ExportBtn.test.tsx.snap | 19 ++++++++ 3 files changed, 38 insertions(+), 30 deletions(-) create mode 100644 test/utils/__snapshots__/ExportBtn.test.tsx.snap diff --git a/src/utils/ExportBtn.tsx b/src/utils/ExportBtn.tsx index 2a0a78c9..c6f141bb 100644 --- a/src/utils/ExportBtn.tsx +++ b/src/utils/ExportBtn.tsx @@ -1,7 +1,7 @@ import { FC } from 'react'; import { Button, ButtonProps } from 'reactstrap'; import { FontAwesomeIcon } from '@fortawesome/react-fontawesome'; -import { faFileDownload } from '@fortawesome/free-solid-svg-icons'; +import { faFileCsv } from '@fortawesome/free-solid-svg-icons'; import { prettify } from './helpers/numbers'; interface ExportBtnProps extends Omit { @@ -11,6 +11,6 @@ interface ExportBtnProps extends Omit = ({ amount = 0, loading = false, ...rest }) => ( ); diff --git a/test/utils/ExportBtn.test.tsx b/test/utils/ExportBtn.test.tsx index 03f27420..0ed10837 100644 --- a/test/utils/ExportBtn.test.tsx +++ b/test/utils/ExportBtn.test.tsx @@ -1,29 +1,22 @@ -import { shallow, ShallowWrapper } from 'enzyme'; -import { FontAwesomeIcon } from '@fortawesome/react-fontawesome'; -import { faFileDownload } from '@fortawesome/free-solid-svg-icons'; +import { render, screen } from '@testing-library/react'; import { ExportBtn } from '../../src/utils/ExportBtn'; describe('', () => { - let wrapper: ShallowWrapper; - const createWrapper = (amount?: number, loading = false) => { - wrapper = shallow(); - - return wrapper; - }; - - afterEach(jest.clearAllMocks); - afterEach(() => wrapper?.unmount()); + const setUp = (amount?: number, loading = false) => render(); it.each([ [true, 'Exporting...'], - [false, 'Export ('], - ])('renders a button', (loading, text) => { - const wrapper = createWrapper(undefined, loading); + [false, 'Export (0)'], + ])('renders loading state when expected', async (loading, text) => { + setUp(undefined, loading); + const btn = await screen.findByRole('button'); - expect(wrapper.prop('outline')).toEqual(true); - expect(wrapper.prop('color')).toEqual('primary'); - expect(wrapper.prop('disabled')).toEqual(loading); - expect(wrapper.html()).toContain(text); + expect(btn).toHaveTextContent(text); + if (loading) { + expect(btn).toHaveAttribute('disabled'); + } else { + expect(btn).not.toHaveAttribute('disabled'); + } }); it.each([ @@ -31,17 +24,13 @@ describe('', () => { [10, '10'], [10_000, '10,000'], [10_000_000, '10,000,000'], - ])('renders expected amount', (amount, expectedRenderedAmount) => { - const wrapper = createWrapper(amount); - - expect(wrapper.html()).toContain(`Export (${expectedRenderedAmount})`); + ])('renders expected amount', async (amount, expectedRenderedAmount) => { + setUp(amount); + expect(await screen.findByRole('button')).toHaveTextContent(`Export (${expectedRenderedAmount})`); }); it('renders expected icon', () => { - const wrapper = createWrapper(); - const icon = wrapper.find(FontAwesomeIcon); - - expect(icon).toHaveLength(1); - expect(icon.prop('icon')).toEqual(faFileDownload); + setUp(); + expect(screen.getByRole('img', { hidden: true })).toMatchSnapshot(); }); }); diff --git a/test/utils/__snapshots__/ExportBtn.test.tsx.snap b/test/utils/__snapshots__/ExportBtn.test.tsx.snap new file mode 100644 index 00000000..7484fe5b --- /dev/null +++ b/test/utils/__snapshots__/ExportBtn.test.tsx.snap @@ -0,0 +1,19 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[` renders expected icon 1`] = ` + +`;