Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[TablePagination] Out of range warning when "count={-1}" #19874

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
});
});