mirror of
https://github.com/shlinkio/shlink-web-client.git
synced 2024-12-23 17:40:23 +03:00
Merge pull request #685 from acelaya-forks/feature/testing-lib
Migrated SortableBarChartCard test to react testing library
This commit is contained in:
commit
0b155b1d20
3 changed files with 1277 additions and 84 deletions
|
@ -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>}
|
||||||
|
|
|
@ -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('name - DESC', () => assert('name', 'DESC', ['Foo', 'Bar'], [100, 50]));
|
|
||||||
it('value - ASC', () => assert('value', 'ASC', ['Bar', 'Foo'], [50, 100]));
|
|
||||||
it('value - DESC', () => assert('value', 'DESC', ['Foo', 'Bar'], [100, 50]));
|
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('renders properly paginated stats when pagination is set', () => {
|
it.each([
|
||||||
let assert: (itemsPerPage: number, expectedStats: string[]) => void;
|
[0],
|
||||||
|
[1],
|
||||||
|
[2],
|
||||||
|
[3],
|
||||||
|
])('renders properly paginated stats when pagination is set', async (itemIndex) => {
|
||||||
|
const { user } = setUp(true, range(1, 159).reduce<Stats>((accum, value) => {
|
||||||
|
accum[`key_${value}`] = value;
|
||||||
|
return accum;
|
||||||
|
}, {}));
|
||||||
|
|
||||||
beforeEach(() => {
|
await user.click(screen.getAllByRole('button')[1]);
|
||||||
const wrapper = createWrapper(true, range(1, 159).reduce<Stats>((accum, value) => {
|
await user.click(screen.getAllByRole('menuitem')[itemIndex]);
|
||||||
accum[`key_${value}`] = value;
|
|
||||||
|
|
||||||
return accum;
|
expect(screen.getByRole('document')).toMatchSnapshot();
|
||||||
}, {}));
|
|
||||||
const dropdown = wrapper.renderProp('title' as never)().find(PaginationDropdown);
|
|
||||||
|
|
||||||
assert = (itemsPerPage: number, expectedStats: string[]) => {
|
|
||||||
dropdown.prop('setValue')(itemsPerPage);
|
|
||||||
|
|
||||||
const stats = wrapper.find(HorizontalBarChart).prop('stats');
|
|
||||||
|
|
||||||
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}
|
</span>
|
||||||
sortingItems={sortingItems}
|
));
|
||||||
extraHeaderContent={() => (
|
|
||||||
<span>
|
|
||||||
<span className="foo-span">Foo</span>
|
|
||||||
<span className="bar-span">Bar</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');
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
1231
test/visits/charts/__snapshots__/SortableBarChartCard.test.tsx.snap
Normal file
1231
test/visits/charts/__snapshots__/SortableBarChartCard.test.tsx.snap
Normal file
File diff suppressed because it is too large
Load diff
Loading…
Reference in a new issue