Merge pull request #685 from acelaya-forks/feature/testing-lib

Migrated SortableBarChartCard test to react testing library
This commit is contained in:
Alejandro Celaya 2022-07-14 21:55:29 +02:00 committed by GitHub
commit 0b155b1d20
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 1277 additions and 84 deletions

View file

@ -8,7 +8,7 @@ type ChartCardProps = PropsWithChildren<{
}>; }>;
export const ChartCard: FC<ChartCardProps> = ({ title, footer, children }) => ( export const ChartCard: FC<ChartCardProps> = ({ title, footer, children }) => (
<Card> <Card role="document">
<CardHeader className="chart-card__header">{typeof title === 'function' ? title() : title}</CardHeader> <CardHeader className="chart-card__header">{typeof title === 'function' ? title() : title}</CardHeader>
<CardBody>{children}</CardBody> <CardBody>{children}</CardBody>
{footer && <CardFooter className="chart-card__footer--sticky">{footer}</CardFooter>} {footer && <CardFooter className="chart-card__footer--sticky">{footer}</CardFooter>}

View file

@ -1,15 +1,12 @@
import { shallow, ShallowWrapper } from 'enzyme'; import { ReactNode } from 'react';
import { screen } from '@testing-library/react';
import { range } from 'ramda'; import { range } from 'ramda';
import { OrderingDropdown } from '../../../src/utils/OrderingDropdown';
import { PaginationDropdown } from '../../../src/utils/PaginationDropdown';
import { rangeOf } from '../../../src/utils/utils'; import { rangeOf } from '../../../src/utils/utils';
import { OrderDir } from '../../../src/utils/helpers/ordering';
import { Stats } from '../../../src/visits/types'; import { Stats } from '../../../src/visits/types';
import { SortableBarChartCard } from '../../../src/visits/charts/SortableBarChartCard'; import { SortableBarChartCard } from '../../../src/visits/charts/SortableBarChartCard';
import { HorizontalBarChart } from '../../../src/visits/charts/HorizontalBarChart'; import { renderWithEvents } from '../../__helpers__/setUpTest';
describe('<SortableBarChartCard />', () => { describe('<SortableBarChartCard />', () => {
let wrapper: ShallowWrapper;
const sortingItems = { const sortingItems = {
name: 'Name', name: 'Name',
amount: 'Amount', amount: 'Amount',
@ -18,98 +15,63 @@ describe('<SortableBarChartCard />', () => {
Foo: 100, Foo: 100,
Bar: 50, Bar: 50,
}; };
const createWrapper = (withPagination = false, extraStats = {}) => { const setUp = (withPagination = false, extraStats = {}, extra?: (foo?: string[]) => ReactNode) => renderWithEvents(
wrapper = shallow(
<SortableBarChartCard <SortableBarChartCard
title="Foo" title="Foo"
stats={{ ...stats, ...extraStats }} stats={{ ...stats, ...extraStats }}
sortingItems={sortingItems} sortingItems={sortingItems}
withPagination={withPagination} withPagination={withPagination}
extraHeaderContent={extra}
/>, />,
); );
return wrapper;
};
afterEach(() => wrapper?.unmount());
it('renders stats unchanged when no ordering is set', () => { it('renders stats unchanged when no ordering is set', () => {
const wrapper = createWrapper(); const { container } = setUp();
const chart = wrapper.find(HorizontalBarChart);
expect(chart.prop('stats')).toEqual(stats); expect(container.firstChild).not.toBeNull();
expect(container.firstChild).toMatchSnapshot();
}); });
describe('renders properly ordered stats when ordering is set', () => { it.each([
let assert: (sortName: string, sortDir: OrderDir, keys: string[], values: number[]) => void; ['Name', 1],
['Amount', 1],
['Name', 2],
['Amount', 2],
])('renders properly ordered stats when ordering is set', async (name, clicks) => {
const { user } = setUp();
beforeEach(() => { await user.click(screen.getByRole('button'));
const wrapper = createWrapper(); await Promise.all(rangeOf(clicks, async () => user.click(screen.getByRole('menuitem', { name }))));
const dropdown = wrapper.renderProp('title' as never)().find(OrderingDropdown);
assert = (sortName: string, sortDir: OrderDir, keys: string[], values: number[]) => { expect(screen.getByRole('document')).toMatchSnapshot();
dropdown.prop('onChange')(sortName, sortDir);
const stats = wrapper.find(HorizontalBarChart).prop('stats');
expect(Object.keys(stats)).toEqual(keys);
expect(Object.values(stats)).toEqual(values);
};
}); });
it('name - ASC', () => assert('name', 'ASC', ['Bar', 'Foo'], [50, 100])); it.each([
it('name - DESC', () => assert('name', 'DESC', ['Foo', 'Bar'], [100, 50])); [0],
it('value - ASC', () => assert('value', 'ASC', ['Bar', 'Foo'], [50, 100])); [1],
it('value - DESC', () => assert('value', 'DESC', ['Foo', 'Bar'], [100, 50])); [2],
}); [3],
])('renders properly paginated stats when pagination is set', async (itemIndex) => {
describe('renders properly paginated stats when pagination is set', () => { const { user } = setUp(true, range(1, 159).reduce<Stats>((accum, value) => {
let assert: (itemsPerPage: number, expectedStats: string[]) => void;
beforeEach(() => {
const wrapper = createWrapper(true, range(1, 159).reduce<Stats>((accum, value) => {
accum[`key_${value}`] = value; accum[`key_${value}`] = value;
return accum; return accum;
}, {})); }, {}));
const dropdown = wrapper.renderProp('title' as never)().find(PaginationDropdown);
assert = (itemsPerPage: number, expectedStats: string[]) => { await user.click(screen.getAllByRole('button')[1]);
dropdown.prop('setValue')(itemsPerPage); await user.click(screen.getAllByRole('menuitem')[itemIndex]);
const stats = wrapper.find(HorizontalBarChart).prop('stats'); expect(screen.getByRole('document')).toMatchSnapshot();
expect(Object.keys(stats)).toEqual(expectedStats);
};
});
const buildExpected = (size: number): string[] => ['Foo', 'Bar', ...rangeOf(size - 2, (i) => `key_${i}`)];
it('50 items per page', () => assert(50, buildExpected(50)));
it('100 items per page', () => assert(100, buildExpected(100)));
it('200 items per page', () => assert(200, buildExpected(160)));
it('500 items per page', () => assert(500, buildExpected(160)));
}); });
it('renders extra header content', () => { it('renders extra header content', () => {
const wrapper = shallow( setUp(false, {}, () => (
<span> <span>
<SortableBarChartCard <span className="foo-span">Foo in header</span>
title="Foo" <span className="bar-span">Bar in header</span>
stats={stats}
sortingItems={sortingItems}
extraHeaderContent={() => (
<span>
<span className="foo-span">Foo</span>
<span className="bar-span">Bar</span>
</span> </span>
)} ));
/>
</span>,
).find(SortableBarChartCard);
const header = wrapper.renderProp('extraHeaderContent')();
expect(header.find('.foo-span')).toHaveLength(1); expect(screen.getByText('Foo in header')).toHaveClass('foo-span');
expect(header.find('.bar-span')).toHaveLength(1); expect(screen.getByText('Bar in header')).toHaveClass('bar-span');
}); });
}); });

File diff suppressed because it is too large Load diff