fieldRenderers.spec.jsx 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. import {initializeOrg} from 'sentry-test/initializeOrg';
  2. import {act, render, screen} from 'sentry-test/reactTestingLibrary';
  3. import ConfigStore from 'sentry/stores/configStore';
  4. import ProjectsStore from 'sentry/stores/projectsStore';
  5. import {getFieldRenderer} from 'sentry/utils/discover/fieldRenderers';
  6. import {SPAN_OP_RELATIVE_BREAKDOWN_FIELD} from 'sentry/utils/discover/fields';
  7. describe('getFieldRenderer', function () {
  8. let location, context, project, organization, data, user;
  9. beforeEach(function () {
  10. context = initializeOrg({
  11. project: TestStubs.Project(),
  12. });
  13. organization = context.organization;
  14. project = context.project;
  15. act(() => ProjectsStore.loadInitialData([project]));
  16. user = 'email:text@example.com';
  17. location = {
  18. pathname: '/events',
  19. query: {},
  20. };
  21. data = {
  22. id: '1',
  23. team_key_transaction: 1,
  24. title: 'ValueError: something bad',
  25. transaction: 'api.do_things',
  26. boolValue: 1,
  27. numeric: 1.23,
  28. createdAt: new Date(2019, 9, 3, 12, 13, 14),
  29. url: '/example',
  30. project: project.slug,
  31. release: 'F2520C43515BD1F0E8A6BD46233324641A370BF6',
  32. user,
  33. 'span_ops_breakdown.relative': '',
  34. 'spans.browser': 10,
  35. 'spans.db': 30,
  36. 'spans.http': 15,
  37. 'spans.resource': 20,
  38. 'spans.total.time': 75,
  39. 'transaction.duration': 75,
  40. 'timestamp.to_day': '2021-09-05T00:00:00+00:00',
  41. lifetimeCount: 10000,
  42. filteredCount: 3000,
  43. count: 6000,
  44. selectionDateString: 'last 7 days',
  45. };
  46. MockApiClient.addMockResponse({
  47. url: `/organizations/${organization.slug}/projects/${project.slug}/`,
  48. body: project,
  49. });
  50. MockApiClient.addMockResponse({
  51. url: `/organizations/${organization.slug}/key-transactions/`,
  52. method: 'POST',
  53. });
  54. MockApiClient.addMockResponse({
  55. url: `/organizations/${organization.slug}/key-transactions/`,
  56. method: 'DELETE',
  57. });
  58. MockApiClient.addMockResponse({
  59. url: `/organizations/${organization.slug}/projects/`,
  60. body: [project],
  61. });
  62. });
  63. it('can render string fields', function () {
  64. const renderer = getFieldRenderer('url', {url: 'string'});
  65. render(renderer(data, {location, organization}));
  66. expect(screen.getByText(data.url)).toBeInTheDocument();
  67. });
  68. it('can render empty string fields', function () {
  69. const renderer = getFieldRenderer('url', {url: 'string'});
  70. data.url = '';
  71. render(renderer(data, {location, organization}));
  72. expect(screen.getByText('(empty string)')).toBeInTheDocument();
  73. });
  74. it('can render boolean fields', function () {
  75. const renderer = getFieldRenderer('boolValue', {boolValue: 'boolean'});
  76. render(renderer(data, {location, organization}));
  77. expect(screen.getByText('true')).toBeInTheDocument();
  78. });
  79. it('can render integer fields', function () {
  80. const renderer = getFieldRenderer('numeric', {numeric: 'integer'});
  81. render(renderer(data, {location, organization}));
  82. expect(screen.getByText(data.numeric)).toBeInTheDocument();
  83. });
  84. describe('date', function () {
  85. beforeEach(function () {
  86. ConfigStore.loadInitialData({
  87. user: {
  88. options: {
  89. timezone: 'America/Los_Angeles',
  90. },
  91. },
  92. });
  93. });
  94. it('can render date fields', function () {
  95. const renderer = getFieldRenderer('createdAt', {createdAt: 'date'});
  96. render(renderer(data, {location, organization}));
  97. expect(screen.getByText('Oct 3, 2019 9:13:14 AM PDT')).toBeInTheDocument();
  98. });
  99. it('can render date fields using utc when query string has utc set to true', function () {
  100. const renderer = getFieldRenderer('createdAt', {createdAt: 'date'});
  101. render(
  102. renderer(data, {location: {...location, query: {utc: 'true'}}, organization})
  103. );
  104. expect(screen.getByText('Oct 3, 2019 4:13:14 PM UTC')).toBeInTheDocument();
  105. });
  106. });
  107. it('can render null date fields', function () {
  108. const renderer = getFieldRenderer('nope', {nope: 'date'});
  109. render(renderer(data, {location, organization}));
  110. expect(screen.getByText('(no value)')).toBeInTheDocument();
  111. });
  112. it('can render timestamp.to_day', function () {
  113. // Set timezone
  114. ConfigStore.loadInitialData({
  115. user: {
  116. options: {
  117. timezone: 'America/Los_Angeles',
  118. },
  119. },
  120. });
  121. const renderer = getFieldRenderer('timestamp.to_day', {'timestamp.to_day': 'date'});
  122. render(renderer(data, {location, organization}));
  123. expect(screen.getByText('Sep 5, 2021')).toBeInTheDocument();
  124. });
  125. it('can render error.handled values', function () {
  126. const renderer = getFieldRenderer('error.handled', {'error.handled': 'boolean'});
  127. function validate(value, expectText) {
  128. const {unmount} = render(
  129. renderer({'error.handled': value}, {location, organization})
  130. );
  131. expect(screen.getByText(expectText)).toBeInTheDocument();
  132. unmount();
  133. }
  134. // Should render the same as the filter.
  135. // ie. all 1 or null
  136. validate([0, 1], 'false');
  137. validate([1, 0], 'false');
  138. validate([null, 0], 'false');
  139. validate([0, null], 'false');
  140. validate([null, 1], 'true');
  141. validate([1, null], 'true');
  142. // null = true for error.handled data.
  143. validate([null], 'true');
  144. // Default events won't have error.handled and will return an empty list.
  145. validate([], '(no value)');
  146. // Transactions will have null for error.handled as the 'tag' won't be set.
  147. validate(null, '(no value)');
  148. });
  149. it('can render user fields with aliased user', function () {
  150. const renderer = getFieldRenderer('user', {user: 'string'});
  151. render(renderer(data, {location, organization}));
  152. expect(screen.getByTestId('letter_avatar-avatar')).toBeInTheDocument();
  153. expect(screen.getByText('text@example.com')).toBeInTheDocument();
  154. });
  155. it('can render null user fields', function () {
  156. const renderer = getFieldRenderer('user', {user: 'string'});
  157. delete data.user;
  158. render(renderer(data, {location, organization}));
  159. expect(screen.queryByTestId('letter_avatar-avatar')).not.toBeInTheDocument();
  160. expect(screen.getByText('(no value)')).toBeInTheDocument();
  161. });
  162. it('can render null release fields', function () {
  163. const renderer = getFieldRenderer('release', {release: 'string'});
  164. delete data.release;
  165. render(renderer(data, {location, organization}));
  166. expect(screen.getByText('(no value)')).toBeInTheDocument();
  167. });
  168. it('can render project as an avatar', function () {
  169. const renderer = getFieldRenderer('project', {project: 'string'});
  170. render(renderer(data, {location, organization}), {
  171. context: context.routerContext,
  172. });
  173. expect(screen.queryByTestId('letter_avatar-avatar')).not.toBeInTheDocument();
  174. expect(screen.getByText(project.slug)).toBeInTheDocument();
  175. });
  176. it('can render project id as an avatar', function () {
  177. const renderer = getFieldRenderer('project', {project: 'number'});
  178. data = {...data, project: parseInt(project.id, 10)};
  179. render(renderer(data, {location, organization}), {
  180. context: context.routerContext,
  181. });
  182. expect(screen.queryByTestId('letter_avatar-avatar')).not.toBeInTheDocument();
  183. expect(screen.getByText(project.slug)).toBeInTheDocument();
  184. });
  185. it('can render team key transaction as a star with the dropdown', function () {
  186. const renderer = getFieldRenderer('team_key_transaction', {
  187. team_key_transaction: 'boolean',
  188. });
  189. render(renderer(data, {location, organization}), {
  190. context: context.routerContext,
  191. });
  192. const star = screen.getByRole('button', {name: 'Toggle star for team'});
  193. // Enabled, can't open the menu in the test without setting up the
  194. // TeamKeyTransactionManager
  195. expect(star).toBeEnabled();
  196. });
  197. it('can render team key transaction as a star without the dropdown', function () {
  198. const renderer = getFieldRenderer('team_key_transaction', {
  199. team_key_transaction: 'boolean',
  200. });
  201. delete data.project;
  202. render(renderer(data, {location, organization}), {
  203. context: context.routerContext,
  204. });
  205. const star = screen.getByRole('button', {name: 'Toggle star for team'});
  206. // Not enabled without a project
  207. expect(star).toBeDisabled();
  208. });
  209. describe('ops breakdown', () => {
  210. const getWidths = () =>
  211. [...screen.getByTestId('relative-ops-breakdown').children].map(
  212. node => node.style.width
  213. );
  214. it('can render operation breakdowns', function () {
  215. const renderer = getFieldRenderer(SPAN_OP_RELATIVE_BREAKDOWN_FIELD, {
  216. [SPAN_OP_RELATIVE_BREAKDOWN_FIELD]: 'string',
  217. });
  218. render(renderer(data, {location, organization}), {
  219. context: context.routerContext,
  220. });
  221. expect(getWidths()).toEqual(['13.333%', '40.000%', '20.000%', '26.667%', '0.000%']);
  222. });
  223. it('renders operation breakdowns in sorted order when a sort field is provided', function () {
  224. const renderer = getFieldRenderer(SPAN_OP_RELATIVE_BREAKDOWN_FIELD, {
  225. [SPAN_OP_RELATIVE_BREAKDOWN_FIELD]: 'string',
  226. });
  227. render(
  228. renderer(data, {
  229. location,
  230. organization,
  231. eventView: {sorts: [{field: 'spans.db'}]},
  232. }),
  233. {context: context.routerContext}
  234. );
  235. expect(getWidths()).toEqual(['40.000%', '13.333%', '20.000%', '26.667%', '0.000%']);
  236. });
  237. });
  238. });