From 11583dbff0170c0aacfd78c791920a4f9c34a148 Mon Sep 17 00:00:00 2001 From: John Mitchell Date: Tue, 11 Dec 2018 16:53:44 -0500 Subject: [PATCH] update pagination tests --- __tests__/components/Pagination.test.jsx | 93 ++++++++++++++++++++++++ src/components/Pagination/Pagination.jsx | 23 +++--- 2 files changed, 102 insertions(+), 14 deletions(-) diff --git a/__tests__/components/Pagination.test.jsx b/__tests__/components/Pagination.test.jsx index 93dbead8ec..6360098c7a 100644 --- a/__tests__/components/Pagination.test.jsx +++ b/__tests__/components/Pagination.test.jsx @@ -72,4 +72,97 @@ describe('', () => { expect(onSetPage).toHaveBeenCalledTimes(2); expect(onSetPage).toBeCalledWith(1, 5); }); + + test('previous button does not work on page 1', () => { + const previous = 'button[aria-label="First"]'; + const onSetPage = jest.fn(); + + pagination = mount( + + + + ); + pagination.find(previous).simulate('click'); + expect(onSetPage).toHaveBeenCalledTimes(0); + }); + + test('changing pageSize works', () => { + const pageSizeDropdownToggleSelector = 'DropdownToggle DropdownToggle[className="togglePageSize"]'; + const pageSizeDropdownItemsSelector = 'DropdownItem'; + const onSetPage = jest.fn(); + + pagination = mount( + + + + ); + const pageSizeDropdownToggle = pagination.find(pageSizeDropdownToggleSelector); + expect(pageSizeDropdownToggle.length).toBe(1); + pageSizeDropdownToggle.at(0).simulate('click'); + + const pageSizeDropdownItems = pagination.find(pageSizeDropdownItemsSelector); + expect(pageSizeDropdownItems.length).toBe(3); + pageSizeDropdownItems.at(1).simulate('click'); + }); + + test('submit a new page by typing in input works', () => { + const textInputSelector = '.pf-l-split__item.pf-m-main .pf-c-form-control'; + const submitFormSelector = '.pf-l-split__item.pf-m-main form'; + + const onSetPage = jest.fn(); + + pagination = mount( + + + + ); + const textInput = pagination.find(textInputSelector); + expect(textInput.length).toBe(1); + textInput.simulate('change'); + pagination.setProps({ page: 2 }); + + const submitForm = pagination.find(submitFormSelector); + expect(submitForm.length).toBe(1); + submitForm.simulate('submit'); + pagination.setState({ value: 'invalid' }); + submitForm.simulate('submit'); + }); + + test('text input page change is disabled when only 1 page', () => { + const onSetPage = jest.fn(); + + pagination = mount( + + + + ); + }); }); diff --git a/src/components/Pagination/Pagination.jsx b/src/components/Pagination/Pagination.jsx index d48f69e72c..001f02f504 100644 --- a/src/components/Pagination/Pagination.jsx +++ b/src/components/Pagination/Pagination.jsx @@ -7,14 +7,9 @@ import { DropdownDirection, DropdownItem, DropdownToggle, - Form, - FormGroup, Level, LevelItem, TextInput, - Toolbar, - ToolbarGroup, - ToolbarItem, Split, SplitItem, } from '@patternfly/react-core'; @@ -32,7 +27,7 @@ class Pagination extends Component { const { page } = this.props; if (prevProps.page !== page) { - this.setState({ value: page }); + this.onPageChange(page); } } @@ -51,13 +46,13 @@ class Pagination extends Component { if (isValid) { onSetPage(value, page_size); - } else{ + } else { this.setState({ value: page }); } }; onFirst = () => { - const { onSetPage, page_size} = this.props; + const { onSetPage, page_size } = this.props; onSetPage(1, page_size); }; @@ -67,7 +62,7 @@ class Pagination extends Component { const previousPage = page - 1; if (previousPage >= 1) { - onSetPage(previousPage, page_size) + onSetPage(previousPage, page_size); } }; @@ -76,7 +71,7 @@ class Pagination extends Component { const nextPage = page + 1; if (nextPage <= pageCount) { - onSetPage(nextPage, page_size) + onSetPage(nextPage, page_size); } }; @@ -139,18 +134,18 @@ class Pagination extends Component { direction={up} isOpen={isOpen} toggle={( - + { page_size } - )}> + )} + > {opts.map(option => ( { option } ))} - Per Page + Per Page