utils.spec.tsx 3.6 KB

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