Skip to content

Commit

Permalink
[tests][cleanup] De-dupe EuiTablePagination and PaginationBar tests
Browse files Browse the repository at this point in the history
- PaginationBar should not be testing props that are basic pass-throughs - EuiTablePagination should (the whole point of unit tests)
+ add test for PaginationBar logic that's actually there (onPageChange)

- Move said missing tests to EuiTablePagination and improve tests to use specific assertions instead of noisy snapshots
  • Loading branch information
cee-chen committed Jul 14, 2023
1 parent d6e9892 commit 23065c7
Show file tree
Hide file tree
Showing 4 changed files with 346 additions and 486 deletions.
Original file line number Diff line number Diff line change
@@ -1,72 +1,5 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`PaginationBar render - custom page size options 1`] = `
<div>
<EuiSpacer
size="m"
/>
<EuiTablePagination
activePage={0}
itemsPerPage={5}
itemsPerPageOptions={
Array [
1,
2,
3,
]
}
onChangeItemsPerPage={[Function]}
onChangePage={[Function]}
pageCount={0}
/>
</div>
`;

exports[`PaginationBar render - hiding per page options 1`] = `
<div>
<EuiSpacer
size="m"
/>
<EuiTablePagination
activePage={0}
itemsPerPage={5}
itemsPerPageOptions={
Array [
10,
25,
50,
]
}
onChangeItemsPerPage={[Function]}
onChangePage={[Function]}
pageCount={0}
showPerPageOptions={false}
/>
</div>
`;

exports[`PaginationBar render - show all pageSize 1`] = `
<div>
<EuiSpacer
size="m"
/>
<EuiTablePagination
activePage={0}
itemsPerPage={0}
itemsPerPageOptions={
Array [
1,
5,
0,
]
}
onChangeItemsPerPage={[Function]}
onChangePage={[Function]}
pageCount={1}
/>
</div>
`;

exports[`PaginationBar renders 1`] = `
<div>
<div
Expand Down
88 changes: 27 additions & 61 deletions src/components/basic_table/pagination_bar.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,78 +7,44 @@
*/

import React from 'react';
import { shallow } from 'enzyme';
import { fireEvent } from '@testing-library/react';
import { render } from '../../test/rtl';
import { requiredProps } from '../../test';

import { PaginationBar } from './pagination_bar';

describe('PaginationBar', () => {
it('renders', () => {
const props = {
...requiredProps,
pagination: {
pageIndex: 0,
pageSize: 5,
totalItemCount: 0,
},
onPageSizeChange: () => {},
onPageChange: () => {},
};
const props = {
...requiredProps,
pagination: {
pageIndex: 0,
pageSize: 5,
totalItemCount: 0,
},
onPageSizeChange: () => {},
onPageChange: () => {},
};

it('renders', () => {
const { container } = render(<PaginationBar {...props} />);

expect(container.firstChild).toMatchSnapshot();
});

test('render - custom page size options', () => {
const props = {
pagination: {
pageIndex: 0,
pageSize: 5,
totalItemCount: 0,
pageSizeOptions: [1, 2, 3],
},
onPageSizeChange: () => {},
onPageChange: () => {},
};

const component = shallow(<PaginationBar {...props} />);

expect(component).toMatchSnapshot();
});

test('render - hiding per page options', () => {
const props = {
pagination: {
pageIndex: 0,
pageSize: 5,
totalItemCount: 0,
showPerPageOptions: false,
},
onPageSizeChange: () => {},
onPageChange: () => {},
};

const component = shallow(<PaginationBar {...props} />);

expect(component).toMatchSnapshot();
});

test('render - show all pageSize', () => {
const props = {
pagination: {
pageIndex: 0,
pageSize: 0,
pageSizeOptions: [1, 5, 0],
totalItemCount: 5,
},
onPageSizeChange: () => {},
onPageChange: () => {},
};

const component = shallow(<PaginationBar {...props} />);

expect(component).toMatchSnapshot();
it('calls onPageChange with the correct off-by-one offset', () => {
const onPageChange = jest.fn();
const { getByLabelText } = render(
<PaginationBar
{...props}
pagination={{
...props.pagination,
totalItemCount: 10,
}}
onPageChange={onPageChange}
/>
);

fireEvent.click(getByLabelText('Page 2 of 2'));
expect(onPageChange).toHaveBeenCalledWith(1);
});
});
Loading

0 comments on commit 23065c7

Please sign in to comment.