useMemoWithPrevious.spec.tsx 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. import {renderHook} from 'sentry-test/reactTestingLibrary';
  2. import {useMemoWithPrevious} from 'sentry/utils/useMemoWithPrevious';
  3. describe('useMemoWithPrevious', () => {
  4. it('calls factory with null', () => {
  5. const dep = {};
  6. const factory = jest.fn().mockImplementation(() => 'foo');
  7. const {result} = renderHook(() => useMemoWithPrevious(factory, [dep]));
  8. expect(factory).toHaveBeenCalledWith(null);
  9. expect(result.current).toEqual('foo');
  10. });
  11. it('calls factory with previous value', () => {
  12. const factory = jest.fn().mockReturnValueOnce('foo').mockReturnValueOnce('bar');
  13. // New reference will trigger a rerender
  14. const firstDependency = [];
  15. const secondDependency = [];
  16. const {rerender, result} = renderHook(
  17. // eslint-disable-next-line react-hooks/exhaustive-deps
  18. ({fact, dep}) => useMemoWithPrevious(fact, [dep]),
  19. {
  20. initialProps: {
  21. fact: factory,
  22. dep: firstDependency,
  23. },
  24. }
  25. );
  26. rerender({fact: factory, dep: secondDependency});
  27. expect(result.current).toBe('bar');
  28. expect(factory.mock.calls[1][0]).toBe('foo');
  29. expect(factory).toHaveBeenCalledTimes(2);
  30. });
  31. });