getBreadcrumb.spec.tsx 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. import {BreadcrumbLevelType, BreadcrumbType, Crumb} from 'sentry/types/breadcrumbs';
  2. import {getNextBreadcrumb, getPrevBreadcrumb} from 'sentry/utils/replays/getBreadcrumb';
  3. const START_TIMESTAMP_SEC = 1651693622.951;
  4. const CURRENT_TIME_MS = 15000;
  5. function createCrumbs(): Crumb[] {
  6. return [
  7. {
  8. color: 'gray300',
  9. data: {url: 'https://dev.getsentry.net:7999/organizations/sentry/performance/'},
  10. description: 'Default',
  11. id: 0,
  12. level: BreadcrumbLevelType.INFO,
  13. message: 'Start recording',
  14. timestamp: '2022-05-11T22:41:32.002Z',
  15. type: BreadcrumbType.INIT,
  16. },
  17. {
  18. category: 'ui.click',
  19. color: 'purple300',
  20. data: undefined,
  21. description: 'User Action',
  22. event_id: null,
  23. id: 3,
  24. level: BreadcrumbLevelType.INFO,
  25. message: 'div.App > section.padding-b-2 > div.makeStyles-search-input-2 > input',
  26. timestamp: '2022-05-04T19:47:08.085000Z',
  27. type: BreadcrumbType.UI,
  28. },
  29. {
  30. category: 'ui.input',
  31. color: 'purple300',
  32. data: undefined,
  33. description: 'User Action',
  34. event_id: null,
  35. id: 4,
  36. level: BreadcrumbLevelType.INFO,
  37. message: 'div.App > section.padding-b-2 > div.makeStyles-search-input-2 > input',
  38. timestamp: '2022-05-04T19:47:11.086000Z',
  39. type: BreadcrumbType.UI,
  40. },
  41. {
  42. category: 'ui.click',
  43. color: 'purple300',
  44. data: undefined,
  45. description: 'User Action',
  46. event_id: null,
  47. id: 20,
  48. level: BreadcrumbLevelType.INFO,
  49. message: 'div.App > section.padding-b-2 > div.makeStyles-search-input-2 > input',
  50. timestamp: '2022-05-04T19:47:52.915000Z',
  51. type: BreadcrumbType.UI,
  52. },
  53. {
  54. category: 'navigation',
  55. color: 'green300',
  56. data: {
  57. from: '/organizations/sentry/user-feedback/?project=6380506',
  58. to: '/organizations/sentry/issues/',
  59. },
  60. description: 'Navigation',
  61. event_id: null,
  62. id: 166,
  63. level: BreadcrumbLevelType.INFO,
  64. message: undefined,
  65. timestamp: '2022-05-04T19:47:59.915000Z',
  66. type: BreadcrumbType.NAVIGATION,
  67. },
  68. ];
  69. }
  70. describe('getNextBreadcrumb', () => {
  71. it('should return the next crumb', () => {
  72. const crumbs = createCrumbs();
  73. const results = getNextBreadcrumb({
  74. crumbs,
  75. targetTimestampMs: START_TIMESTAMP_SEC * 1000 + CURRENT_TIME_MS,
  76. });
  77. expect(results?.id).toEqual(20);
  78. });
  79. it('should return the next crumb when the the list is not sorted', () => {
  80. const [one, two, three, four, five] = createCrumbs();
  81. const results = getNextBreadcrumb({
  82. crumbs: [one, four, five, three, two],
  83. targetTimestampMs: START_TIMESTAMP_SEC * 1000 + CURRENT_TIME_MS,
  84. });
  85. expect(results?.id).toEqual(20);
  86. });
  87. it('should return undefined when there are no crumbs', () => {
  88. const crumbs = [];
  89. const results = getNextBreadcrumb({
  90. crumbs,
  91. targetTimestampMs: START_TIMESTAMP_SEC * 1000 + CURRENT_TIME_MS,
  92. });
  93. expect(results).toBeUndefined();
  94. });
  95. it('should return undefined when the timestamp is later than any crumbs', () => {
  96. const crumbs = createCrumbs();
  97. const results = getNextBreadcrumb({
  98. crumbs,
  99. targetTimestampMs: START_TIMESTAMP_SEC * 1000 + 99999999999,
  100. });
  101. expect(results).toBeUndefined();
  102. });
  103. it('should return the crumb after when a timestamp exactly matches', () => {
  104. const crumbs = createCrumbs();
  105. const exactCrumbTime = 8135;
  106. const results = getNextBreadcrumb({
  107. crumbs,
  108. targetTimestampMs: START_TIMESTAMP_SEC * 1000 + exactCrumbTime,
  109. });
  110. expect(results?.id).toEqual(20);
  111. });
  112. it('should return the crumb if timestamps exactly match and allowMatch is enabled', () => {
  113. const crumbs = createCrumbs();
  114. const exactCrumbTime = 8135;
  115. const results = getNextBreadcrumb({
  116. crumbs,
  117. targetTimestampMs: START_TIMESTAMP_SEC * 1000 + exactCrumbTime,
  118. });
  119. expect(results?.id).toEqual(20);
  120. });
  121. });
  122. describe('getPrevBreadcrumb', () => {
  123. it('should return the previous crumb even if the timestamp is closer to the next crumb', () => {
  124. const crumbs = createCrumbs();
  125. const results = getPrevBreadcrumb({
  126. crumbs,
  127. targetTimestampMs: START_TIMESTAMP_SEC * 1000 + CURRENT_TIME_MS,
  128. });
  129. expect(results?.id).toEqual(4);
  130. });
  131. it('should return the previous crumb when the list is not sorted', () => {
  132. const [one, two, three, four, five] = createCrumbs();
  133. const results = getPrevBreadcrumb({
  134. crumbs: [one, four, five, three, two],
  135. targetTimestampMs: START_TIMESTAMP_SEC * 1000 + CURRENT_TIME_MS,
  136. });
  137. expect(results?.id).toEqual(4);
  138. });
  139. it('should return undefined when there are no crumbs', () => {
  140. const crumbs = [];
  141. const results = getPrevBreadcrumb({
  142. crumbs,
  143. targetTimestampMs: START_TIMESTAMP_SEC * 1000 + CURRENT_TIME_MS,
  144. });
  145. expect(results).toBeUndefined();
  146. });
  147. it('should return undefined when the timestamp is earlier than any crumbs', () => {
  148. const crumbs = createCrumbs();
  149. const results = getPrevBreadcrumb({
  150. crumbs,
  151. targetTimestampMs: START_TIMESTAMP_SEC * 1000 - CURRENT_TIME_MS,
  152. });
  153. expect(results).toBeUndefined();
  154. });
  155. it('should return the crumb previous when a crumbs timestamp exactly matches', () => {
  156. const crumbs = createCrumbs();
  157. const exactCrumbTime = 8135;
  158. const results = getPrevBreadcrumb({
  159. crumbs,
  160. targetTimestampMs: START_TIMESTAMP_SEC * 1000 + exactCrumbTime,
  161. });
  162. expect(results?.id).toEqual(3);
  163. });
  164. it('should return the crumb if timestamps exactly match and allowExact is enabled', () => {
  165. const crumbs = createCrumbs();
  166. const exactCrumbTime = 8135;
  167. const results = getPrevBreadcrumb({
  168. crumbs,
  169. targetTimestampMs: START_TIMESTAMP_SEC * 1000 + exactCrumbTime,
  170. allowExact: true,
  171. });
  172. expect(results?.id).toEqual(4);
  173. });
  174. });