usePrevious.spec.tsx 947 B

12345678910111213141516171819202122232425262728293031
  1. import {reactHooks} from 'sentry-test/reactTestingLibrary';
  2. import usePrevious from 'sentry/utils/usePrevious';
  3. describe('usePrevious', () => {
  4. it('stores initial value', () => {
  5. const {result} = reactHooks.renderHook(usePrevious, {initialProps: 'Initial Value'});
  6. expect(result.current).toBe('Initial Value');
  7. });
  8. it('provides initial value', () => {
  9. const {result} = reactHooks.renderHook(usePrevious, {
  10. initialProps: 'Initial Value',
  11. });
  12. expect(result.current).toBe('Initial Value');
  13. });
  14. it('provides previous value', () => {
  15. const {result, rerender} = reactHooks.renderHook(usePrevious<string | undefined>, {
  16. initialProps: undefined,
  17. });
  18. rerender('New Value');
  19. // We did not pass anything under initialProps
  20. expect(result.current).toBe(undefined);
  21. rerender('New New Value');
  22. // Result should point to old value
  23. expect(result.current).toBe('New Value');
  24. });
  25. });