index.spec.tsx 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. import {createMemoryHistory, Route, Router, RouterContext} from 'react-router';
  2. import {render, screen, userEvent, within} from 'sentry-test/reactTestingLibrary';
  3. import {useResultMode} from 'sentry/views/explore/hooks/useResultsMode';
  4. import {useSampleFields} from 'sentry/views/explore/hooks/useSampleFields';
  5. import {useSorts} from 'sentry/views/explore/hooks/useSorts';
  6. import {ExploreToolbar} from 'sentry/views/explore/toolbar';
  7. import {RouteContext} from 'sentry/views/routeContext';
  8. function renderWithRouter(component) {
  9. const memoryHistory = createMemoryHistory();
  10. render(
  11. <Router
  12. history={memoryHistory}
  13. render={props => {
  14. return (
  15. <RouteContext.Provider value={props}>
  16. <RouterContext {...props} />
  17. </RouteContext.Provider>
  18. );
  19. }}
  20. >
  21. <Route path="/" component={component} />
  22. </Router>
  23. );
  24. }
  25. describe('ExploreToolbar', function () {
  26. beforeEach(function () {
  27. // without this the `CompactSelect` component errors with a bunch of async updates
  28. jest.spyOn(console, 'error').mockImplementation();
  29. });
  30. it('allows changing results mode', async function () {
  31. let resultMode;
  32. function Component() {
  33. [resultMode] = useResultMode();
  34. return <ExploreToolbar />;
  35. }
  36. renderWithRouter(Component);
  37. const section = screen.getByTestId('section-result-mode');
  38. const samples = within(section).getByRole('radio', {name: 'Samples'});
  39. const aggregates = within(section).getByRole('radio', {name: 'Aggregate'});
  40. expect(samples).toBeChecked();
  41. expect(aggregates).not.toBeChecked();
  42. expect(resultMode).toEqual('samples');
  43. await userEvent.click(aggregates);
  44. expect(samples).not.toBeChecked();
  45. expect(aggregates).toBeChecked();
  46. expect(resultMode).toEqual('aggregate');
  47. await userEvent.click(samples);
  48. expect(samples).toBeChecked();
  49. expect(aggregates).not.toBeChecked();
  50. expect(resultMode).toEqual('samples');
  51. // TODO: check other parts of page reflects this
  52. });
  53. it('allows changing sort by', async function () {
  54. let sorts;
  55. function Component() {
  56. const [sampleFields] = useSampleFields();
  57. [sorts] = useSorts({fields: sampleFields});
  58. return <ExploreToolbar />;
  59. }
  60. renderWithRouter(Component);
  61. const section = screen.getByTestId('section-sort-by');
  62. // this is the default
  63. expect(within(section).getByRole('button', {name: 'timestamp'})).toBeInTheDocument();
  64. expect(within(section).getByRole('button', {name: 'Descending'})).toBeInTheDocument();
  65. expect(sorts).toEqual([{field: 'timestamp', kind: 'desc'}]);
  66. // check the default field options
  67. const fields = [
  68. 'project',
  69. 'id',
  70. 'span.op',
  71. 'span.description',
  72. 'span.duration',
  73. 'timestamp',
  74. ];
  75. await userEvent.click(within(section).getByRole('button', {name: 'timestamp'}));
  76. const fieldOptions = await within(section).findAllByRole('option');
  77. expect(fieldOptions).toHaveLength(fields.length);
  78. fieldOptions.forEach((option, i) => {
  79. expect(option).toHaveTextContent(fields[i]);
  80. });
  81. // try changing the field
  82. await userEvent.click(within(section).getByRole('option', {name: 'span.op'}));
  83. expect(within(section).getByRole('button', {name: 'span.op'})).toBeInTheDocument();
  84. expect(within(section).getByRole('button', {name: 'Descending'})).toBeInTheDocument();
  85. expect(sorts).toEqual([{field: 'span.op', kind: 'desc'}]);
  86. // check the kind options
  87. await userEvent.click(within(section).getByRole('button', {name: 'Descending'}));
  88. const kindOptions = await within(section).findAllByRole('option');
  89. expect(kindOptions).toHaveLength(2);
  90. expect(kindOptions[0]).toHaveTextContent('Descending');
  91. expect(kindOptions[1]).toHaveTextContent('Ascending');
  92. // try changing the kind
  93. await userEvent.click(within(section).getByRole('option', {name: 'Ascending'}));
  94. expect(within(section).getByRole('button', {name: 'span.op'})).toBeInTheDocument();
  95. expect(within(section).getByRole('button', {name: 'Ascending'})).toBeInTheDocument();
  96. expect(sorts).toEqual([{field: 'span.op', kind: 'asc'}]);
  97. });
  98. });