utils.spec.tsx 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. import {
  2. memoizeByReference,
  3. memoizeVariadicByReference,
  4. } from 'sentry/utils/profiling/profile/utils';
  5. describe('memoizeByReference', () => {
  6. it('doesnt crash w/o args', () => {
  7. const spy = jest.fn().mockImplementation(() => 1);
  8. const fn = memoizeByReference(spy);
  9. // @ts-ignore this shouldnt happen, but just in case it somehow gets passed
  10. // in during runtime, we want to eval the function every time. The reason
  11. // for doing so is that we dont know if it is pure or not.
  12. expect(() => fn()).not.toThrow();
  13. // @ts-ignore this shouldnt happen, but in case it does
  14. expect(fn()).toBe(1);
  15. expect(spy).toHaveBeenCalledTimes(2);
  16. });
  17. it('memoizes when values match by reference', () => {
  18. const fn = jest.fn().mockImplementation(v => v);
  19. const val = Math.random();
  20. const memoized = memoizeByReference(fn);
  21. // @ts-ignore we discard result of first call
  22. const _discard = memoized(val);
  23. const result = memoized(val);
  24. expect(result).toEqual(val);
  25. expect(fn).toHaveBeenCalledTimes(1);
  26. });
  27. it('re-evaluates when values do not match by reference', () => {
  28. const fn = jest.fn().mockImplementation(v => v);
  29. const memoized = memoizeByReference(fn);
  30. // @ts-ignore we discard result of first call
  31. const _discard = memoized(1);
  32. const result = memoized(2);
  33. expect(result).toEqual(2);
  34. expect(fn).toHaveBeenCalledTimes(2);
  35. });
  36. });
  37. describe('memoizeVariadicByReference', () => {
  38. it('doesnt crash w/o args', () => {
  39. const spy = jest.fn().mockImplementation(() => 1);
  40. const fn = memoizeVariadicByReference(spy);
  41. // @ts-ignore this shouldnt happen, but just in case it somehow gets passed
  42. // in during runtime, we want to eval the function every time. The reason
  43. // for doing so is that we dont know if it is pure or not.
  44. expect(() => fn()).not.toThrow();
  45. // @ts-ignore this shouldnt happen, but in case it does
  46. expect(fn()).toBe(1);
  47. expect(spy).toHaveBeenCalledTimes(2);
  48. });
  49. it('memoizes when args match by reference', () => {
  50. const fn = jest.fn().mockImplementation((a, b) => a + b);
  51. const memoized = memoizeVariadicByReference(fn);
  52. const a = 1;
  53. const b = 2;
  54. // @ts-ignore we discard result of first call
  55. const _discard = memoized(a, b);
  56. const result = memoized(a, b);
  57. expect(result).toBe(3);
  58. expect(fn).toHaveBeenCalledTimes(1);
  59. });
  60. it('re-evaluates when values do not match by reference', () => {
  61. const fn = jest.fn().mockImplementation((a, b) => a + b);
  62. const memoized = memoizeVariadicByReference(fn);
  63. const a = 1;
  64. const b = 2;
  65. const c = 1;
  66. // @ts-ignore we discard result of first call
  67. const _discard = memoized(a, b);
  68. const result = memoized(a, c);
  69. expect(result).toBe(2);
  70. expect(fn).toHaveBeenCalledTimes(2);
  71. });
  72. });