reactTestingLibrary.spec.tsx 1000 B

123456789101112131415161718192021222324252627282930
  1. import {useRef} from 'react';
  2. import {render, screen} from 'sentry-test/reactTestingLibrary';
  3. describe('rerender', () => {
  4. // Taken from https://testing-library.com/docs/example-update-props/
  5. let idCounter = 1;
  6. function 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} = render(<NumberDisplay number={1} />);
  17. expect(screen.getByTestId('number-display')).toHaveTextContent('1');
  18. // re-render the same component with different props
  19. rerender(<NumberDisplay number={2} />);
  20. expect(screen.getByTestId('number-display')).toHaveTextContent('2');
  21. expect(screen.getByTestId('instance-id')).toHaveTextContent('1');
  22. });
  23. });