Skip to content

Commit

Permalink
fix(compass-crud): fix nextPage availability logic COMPASS-8239 (#6197)
Browse files Browse the repository at this point in the history
  • Loading branch information
paula-stacho authored Sep 4, 2024
1 parent daaf192 commit a1f8f05
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 17 deletions.
53 changes: 38 additions & 15 deletions packages/compass-crud/src/components/crud-toolbar.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -151,24 +151,47 @@ describe('CrudToolbar Component', function () {
expect(screen.getByTestId('docs-toolbar-prev-page-btn')).to.be.visible;
});

it('should have the next page button disabled when on the first page without more than a page of documents', function () {
const getPageSpy = sinon.spy();
renderCrudToolbar({
getPage: getPageSpy,
count: 5,
page: 0,
start: 1,
end: 5,
context('respecting the docsPerPage setting', () => {
it('should have the next page button disabled when on the first page without more than a page of documents', function () {
const getPageSpy = sinon.spy();
renderCrudToolbar({
getPage: getPageSpy,
docsPerPage: 50,
count: 50,
page: 0,
start: 1,
end: 50,
});
expect(getPageSpy.called).to.be.false;
fireEvent.click(screen.getByTestId('docs-toolbar-next-page-btn'));

expect(
screen.getByTestId('docs-toolbar-next-page-btn')
).to.have.attribute('aria-disabled', 'true');

expect(getPageSpy.calledOnce).to.be.false;
});
expect(getPageSpy.called).to.be.false;
fireEvent.click(screen.getByTestId('docs-toolbar-next-page-btn'));

expect(screen.getByTestId('docs-toolbar-next-page-btn')).to.have.attribute(
'aria-disabled',
'true'
);
it('should have the next page button disabled when on the first page with more than a page of documents', function () {
const getPageSpy = sinon.spy();
renderCrudToolbar({
getPage: getPageSpy,
docsPerPage: 25,
count: 50,
page: 0,
start: 1,
end: 25,
});
expect(getPageSpy.called).to.be.false;
fireEvent.click(screen.getByTestId('docs-toolbar-next-page-btn'));

expect(getPageSpy.calledOnce).to.be.false;
expect(
screen.getByTestId('docs-toolbar-next-page-btn')
).to.have.attribute('aria-disabled', 'false');

expect(getPageSpy.calledOnce).to.be.true;
expect(getPageSpy.firstCall.args[0]).to.equal(1);
});
});

it('should call to get the next page when the prev button is hit on a non-first page', function () {
Expand Down
6 changes: 4 additions & 2 deletions packages/compass-crud/src/components/crud-toolbar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -173,8 +173,10 @@ const CrudToolbar: React.FunctionComponent<CrudToolbarProps> = ({
const nextButtonDisabled = useMemo(
// If we don't know the count, we can't know if there are more pages.
() =>
count === undefined || count === null ? false : 20 * (page + 1) >= count,
[count, page]
count === undefined || count === null
? false
: docsPerPage * (page + 1) >= count,
[count, page, docsPerPage]
);

const enableExplainPlan = usePreference('enableExplainPlan');
Expand Down

0 comments on commit a1f8f05

Please sign in to comment.