utils.spec.tsx 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. import {
  2. createSentrySampleProfileFrameIndex,
  3. memoizeByReference,
  4. memoizeVariadicByReference,
  5. } from 'sentry/utils/profiling/profile/utils';
  6. describe('createSentrySampleProfileFrameIndex', () => {
  7. it('dedupes frames', () => {
  8. const frames = [
  9. {
  10. in_app: true,
  11. function: 'foo',
  12. lineno: 100,
  13. },
  14. {
  15. in_app: true,
  16. function: 'bar',
  17. lineno: 105,
  18. },
  19. {
  20. in_app: true,
  21. function: 'foo',
  22. lineno: 100,
  23. },
  24. ];
  25. const frameIndex = createSentrySampleProfileFrameIndex(frames, 'javascript');
  26. expect(frameIndex).toEqual({
  27. 0: frameIndex[0],
  28. 1: frameIndex[1],
  29. 2: frameIndex[0],
  30. });
  31. });
  32. });
  33. describe('memoizeByReference', () => {
  34. it('doesnt crash w/o args', () => {
  35. const spy = jest.fn().mockImplementation(() => 1);
  36. const fn = memoizeByReference(spy);
  37. // @ts-expect-error this shouldnt happen, but just in case it somehow gets passed
  38. // in during runtime, we want to eval the function every time. The reason
  39. // for doing so is that we dont know if it is pure or not.
  40. expect(() => fn()).not.toThrow();
  41. // @ts-expect-error this shouldnt happen, but in case it does
  42. expect(fn()).toBe(1);
  43. expect(spy).toHaveBeenCalledTimes(2);
  44. });
  45. it('memoizes when values match by reference', () => {
  46. const fn = jest.fn().mockImplementation(v => v);
  47. const val = Math.random();
  48. const memoized = memoizeByReference(fn);
  49. // @ts-expect-error we discard result of first call
  50. const _discard = memoized(val);
  51. const result = memoized(val);
  52. expect(result).toEqual(val);
  53. expect(fn).toHaveBeenCalledTimes(1);
  54. });
  55. it('re-evaluates when values do not match by reference', () => {
  56. const fn = jest.fn().mockImplementation(v => v);
  57. const memoized = memoizeByReference(fn);
  58. // @ts-expect-error we discard result of first call
  59. const _discard = memoized(1);
  60. const result = memoized(2);
  61. expect(result).toEqual(2);
  62. expect(fn).toHaveBeenCalledTimes(2);
  63. });
  64. });
  65. describe('memoizeVariadicByReference', () => {
  66. it('doesnt crash w/o args', () => {
  67. const spy = jest.fn().mockImplementation(() => 1);
  68. const fn = memoizeVariadicByReference(spy);
  69. // this shouldnt happen, but just in case it somehow gets passed
  70. // in during runtime, we want to eval the function every time. The reason
  71. // for doing so is that we dont know if it is pure or not.
  72. expect(() => fn()).not.toThrow();
  73. expect(fn()).toBe(1);
  74. expect(spy).toHaveBeenCalledTimes(2);
  75. });
  76. it('memoizes when args match by reference', () => {
  77. const fn = jest.fn().mockImplementation((a, b) => a + b);
  78. const memoized = memoizeVariadicByReference(fn);
  79. const a = 1;
  80. const b = 2;
  81. // @ts-expect-error we discard result of first call
  82. const _discard = memoized(a, b);
  83. const result = memoized(a, b);
  84. expect(result).toBe(3);
  85. expect(fn).toHaveBeenCalledTimes(1);
  86. });
  87. it('re-evaluates when values do not match by reference', () => {
  88. const fn = jest.fn().mockImplementation((a, b) => a + b);
  89. const memoized = memoizeVariadicByReference(fn);
  90. const a = 1;
  91. const b = 2;
  92. const c = 1;
  93. // @ts-expect-error we discard result of first call
  94. const _discard = memoized(a, b);
  95. const result = memoized(a, c);
  96. expect(result).toBe(2);
  97. expect(fn).toHaveBeenCalledTimes(2);
  98. });
  99. });