util.spec.tsx 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. import type {Location} from 'history';
  2. import {EventFixture} from 'sentry-fixture/event';
  3. import {initializeOrg} from 'sentry-test/initializeOrg';
  4. import {
  5. EMPTY_HIGHLIGHT_DEFAULT,
  6. getHighlightContextData,
  7. getHighlightTagData,
  8. } from 'sentry/components/events/highlights/util';
  9. export const TEST_EVENT_CONTEXTS = {
  10. keyboard: {
  11. type: 'default',
  12. brand: 'keychron',
  13. percent: 75,
  14. switches: {
  15. form: 'tactile',
  16. brand: 'wuque studios',
  17. },
  18. },
  19. client_os: {
  20. type: 'os',
  21. name: 'Mac OS X',
  22. version: '10.15',
  23. },
  24. runtime: {
  25. type: 'runtime',
  26. name: 'CPython',
  27. version: '3.8.13',
  28. },
  29. };
  30. export const TEST_EVENT_TAGS = [
  31. {
  32. key: 'browser',
  33. value: 'Chrome 1.2.3',
  34. },
  35. {
  36. key: 'browser.name',
  37. value: 'Chrome',
  38. },
  39. {
  40. key: 'device.family',
  41. value: 'Mac',
  42. },
  43. {
  44. key: 'environment',
  45. value: 'production',
  46. },
  47. {
  48. key: 'handled',
  49. value: 'no',
  50. },
  51. {
  52. key: 'level',
  53. value: 'error',
  54. },
  55. {
  56. key: 'release',
  57. value: '1.8',
  58. },
  59. {
  60. key: 'runtime',
  61. value: 'CPython 3.8.13',
  62. },
  63. {
  64. key: 'runtime.name',
  65. value: 'CPython',
  66. },
  67. {
  68. key: 'url',
  69. value: 'https://example.com',
  70. },
  71. ];
  72. describe('getHighlightContextData', function () {
  73. it('returns only highlight context data', function () {
  74. const {organization, project} = initializeOrg();
  75. const event = EventFixture({
  76. contexts: TEST_EVENT_CONTEXTS,
  77. });
  78. const missingContextKey = 'color';
  79. const highlightContext = {
  80. keyboard: ['brand', 'switches', missingContextKey],
  81. };
  82. const highlightCtxData = getHighlightContextData({
  83. event,
  84. highlightContext,
  85. project,
  86. organization,
  87. location: {query: {}} as Location,
  88. });
  89. expect(highlightCtxData).toHaveLength(1);
  90. expect(highlightCtxData[0].alias).toBe('keyboard');
  91. expect(highlightCtxData[0].type).toBe('default');
  92. expect(highlightCtxData[0].data).toHaveLength(highlightContext.keyboard.length);
  93. const highlightCtxDataKeys = new Set(highlightCtxData[0].data.map(({key}) => key));
  94. for (const ctxKey of highlightContext.keyboard) {
  95. expect(highlightCtxDataKeys.has(ctxKey)).toBe(true);
  96. }
  97. const missingCtxHighlightFromEvent = highlightCtxData[0].data?.find(
  98. d => d.key === missingContextKey
  99. );
  100. expect(missingCtxHighlightFromEvent?.value).toBe(EMPTY_HIGHLIGHT_DEFAULT);
  101. });
  102. it.each([
  103. ['alias', {client_os: ['version']}],
  104. ['type', {os: ['version']}],
  105. ['title', {'Operating System': ['version']}],
  106. ])('matches highlights on context %s', (_type, highlightContext) => {
  107. const {organization, project} = initializeOrg();
  108. const event = EventFixture({
  109. contexts: TEST_EVENT_CONTEXTS,
  110. });
  111. const highlightCtxData = getHighlightContextData({
  112. event,
  113. highlightContext,
  114. project,
  115. organization,
  116. location: {query: {}} as Location,
  117. });
  118. expect(highlightCtxData).toHaveLength(1);
  119. expect(highlightCtxData[0].type).toBe('os');
  120. });
  121. });
  122. describe('getHighlightTagData', function () {
  123. it('returns only highlight tag data', function () {
  124. const event = EventFixture({
  125. tags: TEST_EVENT_TAGS,
  126. });
  127. const missingTag = 'zamboni';
  128. const highlightTags = ['release', 'url', 'environment', missingTag];
  129. const highlightTagsSet = new Set(highlightTags);
  130. const highlightTagData = getHighlightTagData({event, highlightTags});
  131. expect(highlightTagData).toHaveLength(highlightTagsSet.size);
  132. for (const content of highlightTagData) {
  133. expect(highlightTagsSet.has(content.originalTag.key)).toBe(true);
  134. }
  135. const missingTagHighlightFromEvent = highlightTagData.find(
  136. td => td.originalTag.key === missingTag
  137. );
  138. expect(missingTagHighlightFromEvent?.value).toBe(EMPTY_HIGHLIGHT_DEFAULT);
  139. });
  140. });