datasetSelector.spec.tsx 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. import {initializeOrg} from 'sentry-test/initializeOrg';
  2. import {render, screen, userEvent} from 'sentry-test/reactTestingLibrary';
  3. import EventView, {type EventViewOptions} from 'sentry/utils/discover/eventView';
  4. import {DiscoverDatasets} from 'sentry/utils/discover/types';
  5. import {DatasetSelector} from 'sentry/views/discover/savedQuery/datasetSelector';
  6. const EVENT_VIEW_CONSTRUCTOR_PROPS: EventViewOptions = {
  7. createdBy: undefined,
  8. end: undefined,
  9. environment: [],
  10. fields: [{field: 'transaction'}, {field: 'project'}],
  11. name: 'Test',
  12. project: [],
  13. query: 'foo:bar',
  14. start: undefined,
  15. team: [],
  16. sorts: [],
  17. statsPeriod: undefined,
  18. topEvents: undefined,
  19. id: undefined,
  20. display: undefined,
  21. };
  22. describe('Discover DatasetSelector', function () {
  23. const {router} = initializeOrg({
  24. organization: {features: ['performance-view']},
  25. });
  26. it('renders selector and options', async function () {
  27. const eventView = new EventView(EVENT_VIEW_CONSTRUCTOR_PROPS);
  28. render(
  29. <DatasetSelector isHomepage={false} savedQuery={undefined} eventView={eventView} />,
  30. {
  31. router,
  32. }
  33. );
  34. await userEvent.click(screen.getByText('Dataset'));
  35. const menuOptions = await screen.findAllByRole('option');
  36. expect(menuOptions.map(e => e.textContent)).toEqual([
  37. 'Errors',
  38. 'Transactions',
  39. 'Unknown',
  40. ]);
  41. });
  42. it('pushes new dafault event view if not a saved query', async function () {
  43. const eventView = new EventView(EVENT_VIEW_CONSTRUCTOR_PROPS);
  44. render(
  45. <DatasetSelector isHomepage={false} savedQuery={undefined} eventView={eventView} />,
  46. {
  47. router,
  48. }
  49. );
  50. await userEvent.click(screen.getByText('Dataset'));
  51. await userEvent.click(screen.getByRole('option', {name: 'Transactions'}));
  52. expect(router.push).toHaveBeenCalledWith(
  53. expect.objectContaining({
  54. query: expect.objectContaining({
  55. project: undefined,
  56. field: ['transaction', 'project'],
  57. query: 'foo:bar',
  58. queryDataset: 'transaction-like',
  59. }),
  60. })
  61. );
  62. });
  63. it('only updates dataset if saved query', async function () {
  64. const eventView = new EventView({
  65. ...EVENT_VIEW_CONSTRUCTOR_PROPS,
  66. dataset: DiscoverDatasets.ERRORS,
  67. id: '1',
  68. });
  69. render(
  70. <DatasetSelector isHomepage={false} savedQuery={undefined} eventView={eventView} />,
  71. {
  72. router,
  73. }
  74. );
  75. await userEvent.click(screen.getByText('Dataset'));
  76. await userEvent.click(screen.getByRole('option', {name: 'Transactions'}));
  77. expect(router.push).toHaveBeenCalledWith(
  78. expect.objectContaining({
  79. query: expect.objectContaining({
  80. project: undefined,
  81. field: ['transaction', 'project'],
  82. query: 'foo:bar',
  83. queryDataset: 'transaction-like',
  84. }),
  85. })
  86. );
  87. });
  88. });