usePrevious.spec.tsx 993 B

12345678910111213141516171819202122232425262728293031323334
  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('Initial Value'));
  6. expect(result.current).toBe('Initial Value');
  7. });
  8. it('provides initial value', () => {
  9. const {result} = reactHooks.renderHook(value => usePrevious(value), {
  10. initialProps: 'Initial Value',
  11. });
  12. expect(result.current).toBe('Initial Value');
  13. });
  14. it('provides previous value', () => {
  15. const {result, rerender} = reactHooks.renderHook(
  16. (value: string | undefined) => usePrevious(value),
  17. {
  18. initialProps: undefined,
  19. }
  20. );
  21. rerender('New Value');
  22. // We did not pass anything under initialProps
  23. expect(result.current).toBe(undefined);
  24. rerender('New New Value');
  25. // Result should point to old value
  26. expect(result.current).toBe('New Value');
  27. });
  28. });