diff --git a/test/__mocks__/setUpCanvas.ts b/test/__mocks__/setUpCanvas.ts new file mode 100644 index 00000000..9308a25d --- /dev/null +++ b/test/__mocks__/setUpCanvas.ts @@ -0,0 +1,10 @@ +import { ReactElement } from 'react'; +import { render } from '@testing-library/react'; + +export const setUpCanvas = (element: ReactElement) => { + const result = render(element); + const { container } = result; + const events = container.querySelector('canvas')?.getContext('2d')?.__getEvents(); // eslint-disable-line no-underscore-dangle + + return { ...result, events }; +}; diff --git a/test/visits/charts/DoughnutChart.test.tsx b/test/visits/charts/DoughnutChart.test.tsx index 9f5ffc74..346b6509 100644 --- a/test/visits/charts/DoughnutChart.test.tsx +++ b/test/visits/charts/DoughnutChart.test.tsx @@ -1,47 +1,24 @@ -import { shallow, ShallowWrapper } from 'enzyme'; -import { Doughnut } from 'react-chartjs-2'; -import { keys, values } from 'ramda'; import { DoughnutChart } from '../../../src/visits/charts/DoughnutChart'; +import { setUpCanvas } from '../../__mocks__/setUpCanvas'; +import { screen } from '@testing-library/react'; describe('', () => { - let wrapper: ShallowWrapper; const stats = { foo: 123, bar: 456, }; - afterEach(() => wrapper?.unmount()); - it('renders Doughnut with expected props', () => { - wrapper = shallow(); - const doughnut = wrapper.find(Doughnut); - const cols = wrapper.find('.col-sm-12'); + const { events } = setUpCanvas(); - expect(doughnut).toHaveLength(1); - - const { labels, datasets } = doughnut.prop('data') as any; - const [{ data, backgroundColor, borderColor }] = datasets; - const { plugins, scales } = (doughnut.prop('options') ?? {}) as any; - - expect(labels).toEqual(keys(stats)); - expect(data).toEqual(values(stats)); - expect(datasets).toHaveLength(1); - expect(backgroundColor).toEqual([ - '#97BBCD', - '#F7464A', - '#46BFBD', - '#FDB45C', - '#949FB1', - '#57A773', - '#414066', - '#08B2E3', - '#B6C454', - '#DCDCDC', - '#463730', - ]); - expect(borderColor).toEqual('white'); - expect(plugins.legend).toEqual({ display: false }); - expect(scales).toBeUndefined(); - expect(cols).toHaveLength(2); + expect(events).toBeTruthy(); + expect(events).toMatchSnapshot(); }); + + it('renders expected legend', () => { + setUpCanvas(); + + expect(screen.getByText('foo')).toBeInTheDocument(); + expect(screen.getByText('bar')).toBeInTheDocument(); + }) }); diff --git a/test/visits/charts/HorizontalBarChart.test.tsx b/test/visits/charts/HorizontalBarChart.test.tsx index 44b2bfde..439681cf 100644 --- a/test/visits/charts/HorizontalBarChart.test.tsx +++ b/test/visits/charts/HorizontalBarChart.test.tsx @@ -1,18 +1,15 @@ -import { render } from '@testing-library/react'; import { HorizontalBarChart, HorizontalBarChartProps } from '../../../src/visits/charts/HorizontalBarChart'; +import { setUpCanvas } from '../../__mocks__/setUpCanvas'; describe('', () => { - const setUp = (props: HorizontalBarChartProps) => { - const { container } = render(); - return container.querySelector('canvas')?.getContext('2d')?.__getEvents(); // eslint-disable-line no-underscore-dangle - }; + const setUp = (props: HorizontalBarChartProps) => setUpCanvas(); it.each([ [{ foo: 123, bar: 456 }, undefined], [{ one: 999, two: 131313 }, { one: 30, two: 100 }], [{ one: 999, two: 131313, max: 3 }, { one: 30, two: 100 }], ])('renders chart with expected canvas', (stats, highlightedStats) => { - const events = setUp({ stats, highlightedStats }); + const { events } = setUp({ stats, highlightedStats }); expect(events).toBeTruthy(); expect(events).toMatchSnapshot(); diff --git a/test/visits/charts/__snapshots__/DoughnutChart.test.tsx.snap b/test/visits/charts/__snapshots__/DoughnutChart.test.tsx.snap new file mode 100644 index 00000000..dc824989 --- /dev/null +++ b/test/visits/charts/__snapshots__/DoughnutChart.test.tsx.snap @@ -0,0 +1,25 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[` renders Doughnut with expected props 1`] = ` +Array [ + Object { + "props": Object { + "a": 1, + "b": 0, + "c": 0, + "d": 1, + "e": 0, + "f": 0, + }, + "transform": Array [ + 1, + 0, + 0, + 1, + 0, + 0, + ], + "type": "setTransform", + }, +] +`;