useMemoWithPrevious.spec.jsx 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. import {reactHooks} 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} = reactHooks.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} = reactHooks.renderHook(
  17. ({fact, dep}) => useMemoWithPrevious(fact, [dep]),
  18. {
  19. initialProps: {
  20. fact: factory,
  21. dep: firstDependency,
  22. },
  23. }
  24. );
  25. rerender({fact: factory, dep: secondDependency});
  26. expect(result.current).toBe('bar');
  27. expect(factory.mock.calls[1][0]).toBe('foo');
  28. expect(factory).toHaveBeenCalledTimes(2);
  29. });
  30. });