getReplayEvent.spec.tsx 4.5 KB

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