2020-09-04 19:43:26 +03:00
|
|
|
import { shallow, ShallowWrapper } from 'enzyme';
|
|
|
|
import { range } from 'ramda';
|
2021-12-25 12:31:48 +03:00
|
|
|
import { OrderingDropdown } from '../../../src/utils/OrderingDropdown';
|
2020-05-31 18:51:52 +03:00
|
|
|
import PaginationDropdown from '../../../src/utils/PaginationDropdown';
|
2021-11-01 14:48:11 +03:00
|
|
|
import { rangeOf } from '../../../src/utils/utils';
|
|
|
|
import { OrderDir } from '../../../src/utils/helpers/ordering';
|
2020-09-04 19:43:26 +03:00
|
|
|
import { Stats } from '../../../src/visits/types';
|
2021-09-18 20:05:28 +03:00
|
|
|
import { SortableBarChartCard } from '../../../src/visits/charts/SortableBarChartCard';
|
|
|
|
import { HorizontalBarChart } from '../../../src/visits/charts/HorizontalBarChart';
|
2019-01-13 01:47:41 +03:00
|
|
|
|
2021-09-18 20:05:28 +03:00
|
|
|
describe('<SortableBarChartCard />', () => {
|
2020-09-04 19:43:26 +03:00
|
|
|
let wrapper: ShallowWrapper;
|
2019-01-13 01:47:41 +03:00
|
|
|
const sortingItems = {
|
|
|
|
name: 'Name',
|
|
|
|
amount: 'Amount',
|
|
|
|
};
|
|
|
|
const stats = {
|
|
|
|
Foo: 100,
|
|
|
|
Bar: 50,
|
|
|
|
};
|
2019-03-10 19:55:02 +03:00
|
|
|
const createWrapper = (withPagination = false, extraStats = {}) => {
|
2019-01-13 01:47:41 +03:00
|
|
|
wrapper = shallow(
|
2021-09-18 20:05:28 +03:00
|
|
|
<SortableBarChartCard
|
2019-03-10 19:55:02 +03:00
|
|
|
title="Foo"
|
|
|
|
stats={{ ...stats, ...extraStats }}
|
|
|
|
sortingItems={sortingItems}
|
|
|
|
withPagination={withPagination}
|
2020-08-22 09:10:31 +03:00
|
|
|
/>,
|
2019-01-13 01:47:41 +03:00
|
|
|
);
|
|
|
|
|
|
|
|
return wrapper;
|
|
|
|
};
|
|
|
|
|
2020-09-04 19:43:26 +03:00
|
|
|
afterEach(() => wrapper?.unmount());
|
2019-01-13 01:47:41 +03:00
|
|
|
|
|
|
|
it('renders stats unchanged when no ordering is set', () => {
|
|
|
|
const wrapper = createWrapper();
|
2021-09-18 20:05:28 +03:00
|
|
|
const chart = wrapper.find(HorizontalBarChart);
|
2019-01-13 01:47:41 +03:00
|
|
|
|
2021-09-18 20:05:28 +03:00
|
|
|
expect(chart.prop('stats')).toEqual(stats);
|
2019-01-13 01:47:41 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
describe('renders properly ordered stats when ordering is set', () => {
|
2021-10-31 14:33:17 +03:00
|
|
|
let assert: (sortName: string, sortDir: OrderDir, keys: string[], values: number[]) => void;
|
2019-01-13 01:47:41 +03:00
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
const wrapper = createWrapper();
|
2021-12-25 12:31:48 +03:00
|
|
|
const dropdown = wrapper.renderProp('title' as never)().find(OrderingDropdown);
|
2019-01-13 01:47:41 +03:00
|
|
|
|
2021-10-31 14:33:17 +03:00
|
|
|
assert = (sortName: string, sortDir: OrderDir, keys: string[], values: number[]) => {
|
2019-01-13 01:47:41 +03:00
|
|
|
dropdown.prop('onChange')(sortName, sortDir);
|
|
|
|
|
2021-10-31 14:33:17 +03:00
|
|
|
const stats = wrapper.find(HorizontalBarChart).prop('stats');
|
|
|
|
|
|
|
|
expect(Object.keys(stats)).toEqual(keys);
|
|
|
|
expect(Object.values(stats)).toEqual(values);
|
2019-01-13 01:47:41 +03:00
|
|
|
};
|
|
|
|
});
|
|
|
|
|
2022-03-26 14:17:42 +03:00
|
|
|
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]));
|
2019-01-13 01:47:41 +03:00
|
|
|
});
|
|
|
|
|
2019-03-10 19:55:02 +03:00
|
|
|
describe('renders properly paginated stats when pagination is set', () => {
|
2021-10-31 14:33:17 +03:00
|
|
|
let assert: (itemsPerPage: number, expectedStats: string[]) => void;
|
2019-03-10 19:55:02 +03:00
|
|
|
|
|
|
|
beforeEach(() => {
|
2020-09-04 19:43:26 +03:00
|
|
|
const wrapper = createWrapper(true, range(1, 159).reduce<Stats>((accum, value) => {
|
2019-03-10 19:55:02 +03:00
|
|
|
accum[`key_${value}`] = value;
|
|
|
|
|
|
|
|
return accum;
|
|
|
|
}, {}));
|
2020-09-04 19:43:26 +03:00
|
|
|
const dropdown = wrapper.renderProp('title' as never)().find(PaginationDropdown);
|
2019-03-10 19:55:02 +03:00
|
|
|
|
2021-10-31 14:33:17 +03:00
|
|
|
assert = (itemsPerPage: number, expectedStats: string[]) => {
|
2019-03-10 19:55:02 +03:00
|
|
|
dropdown.prop('setValue')(itemsPerPage);
|
|
|
|
|
2021-10-31 14:33:17 +03:00
|
|
|
const stats = wrapper.find(HorizontalBarChart).prop('stats');
|
|
|
|
|
|
|
|
expect(Object.keys(stats)).toEqual(expectedStats);
|
2019-03-10 19:55:02 +03:00
|
|
|
};
|
|
|
|
});
|
|
|
|
|
2022-03-26 14:17:42 +03:00
|
|
|
const buildExpected = (size: number): string[] => ['Foo', 'Bar', ...rangeOf(size - 2, (i) => `key_${i}`)];
|
2019-03-10 19:55:02 +03:00
|
|
|
|
2021-10-31 14:33:17 +03:00
|
|
|
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)));
|
2019-03-10 19:55:02 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
it('renders extra header content', () => {
|
2020-09-04 19:43:26 +03:00
|
|
|
const wrapper = shallow(
|
2019-03-10 19:55:02 +03:00
|
|
|
<span>
|
2021-09-18 20:05:28 +03:00
|
|
|
<SortableBarChartCard
|
2019-03-10 19:55:02 +03:00
|
|
|
title="Foo"
|
|
|
|
stats={stats}
|
|
|
|
sortingItems={sortingItems}
|
|
|
|
extraHeaderContent={() => (
|
|
|
|
<span>
|
|
|
|
<span className="foo-span">Foo</span>
|
|
|
|
<span className="bar-span">Bar</span>
|
|
|
|
</span>
|
|
|
|
)}
|
|
|
|
/>
|
2020-08-22 09:10:31 +03:00
|
|
|
</span>,
|
2021-09-18 20:05:28 +03:00
|
|
|
).find(SortableBarChartCard);
|
2019-03-10 19:55:02 +03:00
|
|
|
const header = wrapper.renderProp('extraHeaderContent')();
|
2019-01-13 01:47:41 +03:00
|
|
|
|
2019-03-10 19:55:02 +03:00
|
|
|
expect(header.find('.foo-span')).toHaveLength(1);
|
|
|
|
expect(header.find('.bar-span')).toHaveLength(1);
|
2019-01-13 01:47:41 +03:00
|
|
|
});
|
|
|
|
});
|