Skip to content

Commit

Permalink
[TablePagination] Out of range warning when "count={-1}" (#19874)
Browse files Browse the repository at this point in the history
  • Loading branch information
tutizaraz committed Mar 1, 2020
1 parent 0033bb3 commit c15f60d
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 32 deletions.
5 changes: 5 additions & 0 deletions packages/material-ui/src/TablePagination/TablePagination.js
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,11 @@ TablePagination.propTypes = {
*/
page: chainPropTypes(PropTypes.number.isRequired, props => {
const { count, page, rowsPerPage } = props;

if (count === -1) {
return null;
}

const newLastPage = Math.max(0, Math.ceil(count / rowsPerPage) - 1);
if (page < 0 || page > newLastPage) {
return new Error(
Expand Down
70 changes: 38 additions & 32 deletions packages/material-ui/src/TablePagination/TablePagination.test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import * as React from 'react';
import { assert } from 'chai';
import { expect, assert } from 'chai';
import PropTypes from 'prop-types';
import { createMount, getClasses } from '@material-ui/core/test-utils';
import { fireEvent, createClientRender } from 'test/utils/createClientRender';
import describeConformance from '../test-utils/describeConformance';
import consoleErrorMock from 'test/utils/consoleErrorMock';
import Select from '../Select';
Expand All @@ -16,6 +17,7 @@ describe('<TablePagination />', () => {
const noop = () => {};
let classes;
let mount;
const render = createClientRender();

function mountInTable(node) {
const wrapper = mount(
Expand All @@ -32,11 +34,14 @@ describe('<TablePagination />', () => {
classes = getClasses(
<TablePagination count={1} onChangePage={() => {}} page={0} rowsPerPage={10} />,
);
// StrictModeViolation: test uses #html()
});

beforeEach(() => {
// StrictModeViolation: test uses #html()()
mount = createMount({ strict: false });
});

after(() => {
afterEach(() => {
mount.cleanUp();
});

Expand Down Expand Up @@ -248,6 +253,36 @@ describe('<TablePagination />', () => {
});
});

describe('prop: count=-1', () => {
it('should display the "of more than" text and keep the nextButton enabled', () => {
const Test = () => {
const [page, setPage] = React.useState(0);
return (
<table>
<TableFooter>
<TableRow>
<TablePagination
page={page}
rowsPerPage={10}
count={-1}
onChangePage={(_, newPage) => {
setPage(newPage);
}}
/>
</TableRow>
</TableFooter>
</table>
);
};

const { container, getByLabelText } = render(<Test />);

expect(container).to.have.text('Rows per page:101-10 of more than 10');
fireEvent.click(getByLabelText('Next page'));
expect(container).to.have.text('Rows per page:1011-20 of more than 20');
});
});

describe('warnings', () => {
before(() => {
consoleErrorMock.spy();
Expand Down Expand Up @@ -282,33 +317,4 @@ describe('<TablePagination />', () => {
);
});
});

it('should display the "of more than" text and keep the nextButton enabled, if count is -1 ', () => {
const wrapper = mount(
<table>
<TableFooter>
<TableRow>
<TablePagination
page={0}
rowsPerPage={5}
rowsPerPageOptions={[5]}
onChangePage={noop}
onChangeRowsPerPage={noop}
count={-1}
/>
</TableRow>
</TableFooter>
</table>,
);

assert.strictEqual(
wrapper
.find(Typography)
.at(0)
.text(),
'1-5 of more than 5',
);
const nextButton = wrapper.find(IconButton).at(1);
assert.strictEqual(nextButton.props().disabled, false);
});
});

0 comments on commit c15f60d

Please sign in to comment.