getReplayEvent.spec.tsx 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. import {ReplayClickFrameFixture} from 'sentry-fixture/replay/replayBreadcrumbFrameData';
  2. import {ReplayRecordFixture} from 'sentry-fixture/replayRecord';
  3. import {
  4. getNextReplayFrame,
  5. getPrevReplayFrame,
  6. } from 'sentry/utils/replays/getReplayEvent';
  7. import hydrateBreadcrumbs from 'sentry/utils/replays/hydrateBreadcrumbs';
  8. const frames = hydrateBreadcrumbs(
  9. ReplayRecordFixture({
  10. started_at: new Date('2022-05-04T19:41:30.00Z'),
  11. }),
  12. [
  13. ReplayClickFrameFixture({
  14. timestamp: new Date('2022-05-04T19:41:32.002Z'),
  15. message: 'index 0',
  16. }),
  17. ReplayClickFrameFixture({
  18. timestamp: new Date('2022-05-04T19:47:08.085000Z'),
  19. message: 'index 1',
  20. }),
  21. ReplayClickFrameFixture({
  22. timestamp: new Date('2022-05-04T19:47:11.086000Z'),
  23. message: 'index 2',
  24. }),
  25. ReplayClickFrameFixture({
  26. timestamp: new Date('2022-05-04T19:47:52.915000Z'),
  27. message: 'index 3',
  28. }),
  29. ReplayClickFrameFixture({
  30. timestamp: new Date('2022-05-04T19:47:59.915000Z'),
  31. message: 'index 4',
  32. }),
  33. ]
  34. );
  35. const CURRENT_OFFSET_MS = frames[0].offsetMs + 15000;
  36. describe('getNextReplayFrame', () => {
  37. it('should return the next crumb', () => {
  38. const result = getNextReplayFrame({
  39. frames,
  40. targetOffsetMs: CURRENT_OFFSET_MS,
  41. });
  42. expect(result).toEqual(frames[1]);
  43. });
  44. it('should return the next crumb when the list is not sorted', () => {
  45. const [one, two, three, four, five] = frames;
  46. const result = getNextReplayFrame({
  47. frames: [one, four, five, three, two],
  48. targetOffsetMs: CURRENT_OFFSET_MS,
  49. });
  50. expect(result).toEqual(frames[1]);
  51. });
  52. it('should return undefined when there are no crumbs', () => {
  53. const result = getNextReplayFrame({
  54. frames: [],
  55. targetOffsetMs: CURRENT_OFFSET_MS,
  56. });
  57. expect(result).toBeUndefined();
  58. });
  59. it('should return the first crumb when the timestamp is earlier than any crumbs', () => {
  60. const result = getNextReplayFrame({
  61. frames,
  62. targetOffsetMs: -1,
  63. });
  64. expect(result).toEqual(frames[0]);
  65. });
  66. it('should return undefined when the timestamp is later than any crumbs', () => {
  67. const result = getNextReplayFrame({
  68. frames,
  69. targetOffsetMs: 99999999999,
  70. });
  71. expect(result).toBeUndefined();
  72. });
  73. it('should return the next frame when a timestamp exactly matches', () => {
  74. const exactTime = frames[1].offsetMs;
  75. const result = getNextReplayFrame({
  76. frames,
  77. targetOffsetMs: exactTime,
  78. allowExact: false,
  79. });
  80. expect(result).toEqual(frames[2]);
  81. });
  82. it('should return the same frame if timestamps exactly match and allowMatch is enabled', () => {
  83. const exactTime = frames[1].offsetMs;
  84. const result = getNextReplayFrame({
  85. frames,
  86. targetOffsetMs: exactTime,
  87. allowExact: true,
  88. });
  89. expect(result).toEqual(frames[1]);
  90. });
  91. });
  92. describe('getPrevReplayFrame', () => {
  93. it('should return the previous crumb', () => {
  94. const result = getPrevReplayFrame({
  95. frames,
  96. targetOffsetMs: CURRENT_OFFSET_MS,
  97. });
  98. expect(result).toEqual(frames[0]);
  99. });
  100. it('should return the previous crumb when the list is not sorted', () => {
  101. const [one, two, three, four, five] = frames;
  102. const result = getPrevReplayFrame({
  103. frames: [one, four, five, three, two],
  104. targetOffsetMs: CURRENT_OFFSET_MS,
  105. });
  106. expect(result).toEqual(frames[0]);
  107. });
  108. it('should return undefined when there are no crumbs', () => {
  109. const result = getPrevReplayFrame({
  110. frames: [],
  111. targetOffsetMs: CURRENT_OFFSET_MS,
  112. });
  113. expect(result).toBeUndefined();
  114. });
  115. it('should return undefined when the timestamp is earlier than any crumbs', () => {
  116. const result = getPrevReplayFrame({
  117. frames,
  118. targetOffsetMs: -1,
  119. });
  120. expect(result).toBeUndefined();
  121. });
  122. it('should return the last crumb if timestamp is later than any crumb', () => {
  123. const result = getPrevReplayFrame({
  124. frames,
  125. targetOffsetMs: 99999999999,
  126. });
  127. expect(result).toEqual(frames[4]);
  128. });
  129. it('should return the prev frame if timestamp exactly matches', () => {
  130. const exactTime = frames[1].offsetMs;
  131. const result = getPrevReplayFrame({
  132. frames,
  133. targetOffsetMs: exactTime,
  134. allowExact: false,
  135. });
  136. expect(result).toEqual(frames[0]);
  137. });
  138. it('should return the same frame if timestamps exactly match and allowExact is enabled', () => {
  139. const exactTime = frames[1].offsetMs;
  140. const result = getPrevReplayFrame({
  141. frames,
  142. targetOffsetMs: exactTime,
  143. allowExact: true,
  144. });
  145. expect(result).toEqual(frames[1]);
  146. });
  147. });