SuspenseQueryComponent.spec.tsx 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. import React from 'react';
  2. import { render } from 'lib/testHelpers';
  3. import { screen } from '@testing-library/react';
  4. import SuspenseQueryComponent from 'components/common/SuspenseQueryComponent/SuspenseQueryComponent';
  5. const fallback = 'fallback';
  6. jest.mock('react-router-dom', () => ({
  7. ...jest.requireActual('react-router-dom'),
  8. Navigate: () => <div>{fallback}</div>,
  9. }));
  10. describe('SuspenseQueryComponent', () => {
  11. const text = 'text';
  12. it('should render the inner component if no error occurs', () => {
  13. render(<SuspenseQueryComponent>{text}</SuspenseQueryComponent>);
  14. expect(screen.getByText(text)).toBeInTheDocument();
  15. });
  16. it('should not render the inner component and call navigate', () => {
  17. // throwing intentional For error boundaries to work
  18. jest.spyOn(console, 'error').mockImplementation(() => undefined);
  19. const Component = () => {
  20. throw new Error('new Error');
  21. };
  22. render(
  23. <SuspenseQueryComponent>
  24. <Component />
  25. </SuspenseQueryComponent>
  26. );
  27. expect(screen.queryByText(text)).not.toBeInTheDocument();
  28. expect(screen.getByText(fallback)).toBeInTheDocument();
  29. jest.clearAllMocks();
  30. });
  31. });