Skip to content

Commit

Permalink
fix(pagination): fix pagination update when totalResults or resultsPe…
Browse files Browse the repository at this point in the history
…rPage change

It fixes a bug when `totalResults` or `resultsPerPage` is updated and the component isn't
re-rendered to reflect the change.
  • Loading branch information
estevanmaito committed Nov 8, 2020
1 parent c7d683e commit fbedd18
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/Pagination.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ const Pagination = React.forwardRef<Ref, PaginationProps>(function Pagination(pr
TOTAL_PAGES,
])
}
}, [activePage])
}, [activePage, TOTAL_PAGES])

useEffect(() => {
onChange(activePage)
Expand Down
32 changes: 32 additions & 0 deletions src/__tests__/Pagination.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -266,4 +266,36 @@ describe('Pagination', () => {

expect(onChange).toHaveBeenCalledWith(3)
})

it('should update the pages total when totalResults is updated', () => {
const onChange = () => {}
const expectedBeforeUpdate = 6
const expectedAfterUpdate = 4

const wrapper = mount(
<Pagination totalResults={30} resultsPerPage={5} label="Navigation" onChange={onChange} />
)

expect(wrapper.find(PageButton).children().length).toBe(expectedBeforeUpdate)

wrapper.setProps({ totalResults: 20, resultsPerPage: 5 })
wrapper.update()
expect(wrapper.find(PageButton).children().length).toBe(expectedAfterUpdate)
})

it('should update the pages total when resultsPerPage is updated', () => {
const onChange = () => {}
const expectedBeforeUpdate = 6
const expectedAfterUpdate = 3

const wrapper = mount(
<Pagination totalResults={30} resultsPerPage={5} label="Navigation" onChange={onChange} />
)

expect(wrapper.find(PageButton).children().length).toBe(expectedBeforeUpdate)

wrapper.setProps({ resultsPerPage: 10 })
wrapper.update()
expect(wrapper.find(PageButton).children().length).toBe(expectedAfterUpdate)
})
})

0 comments on commit fbedd18

Please sign in to comment.