Pagination.spec.tsx 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. import React from 'react';
  2. import { StaticRouter } from 'react-router';
  3. import Pagination, {
  4. PaginationProps,
  5. } from 'components/common/Pagination/Pagination';
  6. import theme from 'theme/theme';
  7. import { render } from 'lib/testHelpers';
  8. import { screen } from '@testing-library/react';
  9. describe('Pagination', () => {
  10. const setupComponent = (search = '', props: Partial<PaginationProps> = {}) =>
  11. render(
  12. <StaticRouter location={{ pathname: '/my/test/path/23', search }}>
  13. <Pagination totalPages={11} {...props} />
  14. </StaticRouter>
  15. );
  16. describe('next & prev buttons', () => {
  17. it('renders disable prev button and enabled next link', () => {
  18. setupComponent('?page=1');
  19. expect(screen.getByText('Previous')).toBeDisabled();
  20. expect(screen.getByText('Next')).toBeInTheDocument();
  21. });
  22. it('renders disable next button and enabled prev link', () => {
  23. setupComponent('?page=11');
  24. expect(screen.getByText('Previous')).toBeInTheDocument();
  25. expect(screen.getByText('Next')).toBeDisabled();
  26. });
  27. it('renders next & prev links with correct path', () => {
  28. setupComponent('?page=5&perPage=20');
  29. expect(screen.getByText('Previous')).toBeInTheDocument();
  30. expect(screen.getByText('Next')).toBeInTheDocument();
  31. expect(screen.getByText('Previous')).toHaveAttribute(
  32. 'href',
  33. '/my/test/path/23?page=4&perPage=20'
  34. );
  35. expect(screen.getByText('Next')).toHaveAttribute(
  36. 'href',
  37. '/my/test/path/23?page=6&perPage=20'
  38. );
  39. });
  40. });
  41. describe('spread', () => {
  42. it('renders 1 spread element after first page control', () => {
  43. setupComponent('?page=8');
  44. expect(screen.getAllByRole('listitem')[1]).toHaveTextContent('…');
  45. expect(screen.getAllByRole('listitem')[1].firstChild).toHaveClass(
  46. 'pagination-ellipsis'
  47. );
  48. });
  49. it('renders 1 spread element before last spread control', () => {
  50. setupComponent('?page=2');
  51. expect(screen.getAllByRole('listitem')[7]).toHaveTextContent('…');
  52. expect(screen.getAllByRole('listitem')[7].firstChild).toHaveClass(
  53. 'pagination-ellipsis'
  54. );
  55. });
  56. it('renders 2 spread elements', () => {
  57. setupComponent('?page=6');
  58. expect(screen.getAllByText('…').length).toEqual(2);
  59. expect(screen.getAllByRole('listitem')[0]).toHaveTextContent('1');
  60. expect(screen.getAllByRole('listitem')[1]).toHaveTextContent('…');
  61. expect(screen.getAllByRole('listitem')[7]).toHaveTextContent('…');
  62. expect(screen.getAllByRole('listitem')[8]).toHaveTextContent('11');
  63. });
  64. it('renders 0 spread elements', () => {
  65. setupComponent('?page=2', { totalPages: 8 });
  66. expect(screen.queryAllByText('…').length).toEqual(0);
  67. expect(screen.getAllByRole('listitem').length).toEqual(8);
  68. });
  69. });
  70. describe('current page', () => {
  71. it('check if it sets page 8 as current when page param is set', () => {
  72. setupComponent('?page=8');
  73. expect(screen.getByText('8')).toHaveStyle(
  74. `background-color: ${theme.pagination.currentPage}`
  75. );
  76. });
  77. it('check if it sets first page as current when page param not set', () => {
  78. setupComponent('', { totalPages: 8 });
  79. expect(screen.getByText('1')).toHaveStyle(
  80. `background-color: ${theme.pagination.currentPage}`
  81. );
  82. });
  83. });
  84. });