pagination.test.js 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. import { expect } from 'chai'
  2. import { render, screen } from '@testing-library/react'
  3. import Pagination from '../../../../frontend/js/shared/components/pagination'
  4. describe('<Pagination />', function () {
  5. it('renders with current page handled', async function () {
  6. render(
  7. <Pagination currentPage={6} totalPages={10} handlePageClick={() => {}} />
  8. )
  9. await screen.findByLabelText('Page 6, Current Page')
  10. })
  11. it('renders with nearby page buttons and prev/next button', async function () {
  12. render(
  13. <Pagination currentPage={2} totalPages={4} handlePageClick={() => {}} />
  14. )
  15. await screen.findByLabelText('Page 2, Current Page')
  16. await screen.findByLabelText('Go to page 1')
  17. await screen.findByLabelText('Go to page 3')
  18. await screen.findByLabelText('Go to page 4')
  19. await screen.findByLabelText('Go to Previous Page')
  20. await screen.findByLabelText('Go to Next Page')
  21. })
  22. it('does not render the prev button when expected', async function () {
  23. render(
  24. <Pagination currentPage={1} totalPages={2} handlePageClick={() => {}} />
  25. )
  26. await screen.findByLabelText('Page 1, Current Page')
  27. await screen.findByLabelText('Go to Next Page')
  28. expect(screen.queryByLabelText('Go to Prev Page')).to.be.null
  29. })
  30. it('does not render the next button when expected', async function () {
  31. render(
  32. <Pagination currentPage={2} totalPages={2} handlePageClick={() => {}} />
  33. )
  34. await screen.findByLabelText('Page 2, Current Page')
  35. await screen.findByLabelText('Go to Previous Page')
  36. expect(screen.queryByLabelText('Go to Next Page')).to.be.null
  37. })
  38. it('renders 1 ellipses when there are more pages than buttons and on first page', async function () {
  39. render(
  40. <Pagination currentPage={1} totalPages={10} handlePageClick={() => {}} />
  41. )
  42. const ellipses = await screen.findAllByText('…')
  43. expect(ellipses.length).to.equal(1)
  44. })
  45. it('renders 1 ellipses when on last page and there are more previous pages than buttons', async function () {
  46. render(
  47. <Pagination currentPage={10} totalPages={10} handlePageClick={() => {}} />
  48. )
  49. const ellipses = await screen.findAllByText('…')
  50. expect(ellipses.length).to.equal(1)
  51. })
  52. it('renders 2 ellipses when there are more pages than buttons', async function () {
  53. render(
  54. <Pagination currentPage={5} totalPages={10} handlePageClick={() => {}} />
  55. )
  56. const ellipses = await screen.findAllByText('…')
  57. expect(ellipses.length).to.equal(2)
  58. })
  59. it('only renders the number of page buttons set by maxOtherPageButtons', async function () {
  60. render(
  61. <Pagination currentPage={1} totalPages={100} handlePageClick={() => {}} />
  62. )
  63. const items = document.querySelectorAll('button')
  64. expect(items.length).to.equal(6) // 5 page buttons + next button
  65. })
  66. })