reactTestingLibrary.spec.tsx 1.1 KB

123456789101112131415161718192021222324252627282930313233
  1. import React, {useRef} from 'react';
  2. import {mountWithTheme, screen} from 'sentry-test/reactTestingLibrary';
  3. describe('rerender', () => {
  4. // Taken from https://testing-library.com/docs/example-update-props/
  5. let idCounter = 1;
  6. const NumberDisplay = ({number}) => {
  7. const id = useRef(idCounter++); // to ensure we don't remount a different instance
  8. return (
  9. <div>
  10. <span data-test-id="number-display">{number}</span>
  11. <span data-test-id="instance-id">{id.current}</span>
  12. </div>
  13. );
  14. };
  15. test('calling render with the same component on the same container does not remount', () => {
  16. const {rerender} = mountWithTheme(<NumberDisplay number={1} />, {
  17. // Passing a context caused rerender to re-mount if the wrapper is not setup correctly
  18. context: TestStubs.routerContext(),
  19. });
  20. expect(screen.getByTestId('number-display')).toHaveTextContent('1');
  21. // re-render the same component with different props
  22. rerender(<NumberDisplay number={2} />);
  23. expect(screen.getByTestId('number-display')).toHaveTextContent('2');
  24. expect(screen.getByTestId('instance-id')).toHaveTextContent('1');
  25. });
  26. });