optionSelector.spec.tsx 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. import {useState} from 'react';
  2. import {initializeOrg} from 'sentry-test/initializeOrg';
  3. import {render, screen, userEvent, within} from 'sentry-test/reactTestingLibrary';
  4. import OptionSelector from 'sentry/components/charts/optionSelector';
  5. import {t} from 'sentry/locale';
  6. describe('EventsV2 > OptionSelector (Multiple)', function () {
  7. const features = ['discover-basic'];
  8. const yAxisValue = ['count()', 'failure_count()'];
  9. const yAxisOptions = [
  10. {label: 'count()', value: 'count()'},
  11. {label: 'failure_count()', value: 'failure_count()'},
  12. {label: 'count_unique(user)', value: 'count_unique(user)'},
  13. {label: 'avg(transaction.duration)', value: 'avg(transaction.duration)'},
  14. ];
  15. const onChangeStub = jest.fn();
  16. const organization = TestStubs.Organization({
  17. features: [...features],
  18. });
  19. afterEach(() => {
  20. jest.resetAllMocks();
  21. });
  22. const TestComponent = () => {
  23. const [currentSelected, setCurrentSelected] = useState([...yAxisValue]);
  24. return (
  25. <OptionSelector
  26. multiple
  27. isOpen
  28. title={t('Y-Axis')}
  29. selected={currentSelected}
  30. options={yAxisOptions}
  31. onChange={newSelected => {
  32. setCurrentSelected(newSelected);
  33. onChangeStub(newSelected);
  34. }}
  35. />
  36. );
  37. };
  38. const renderComponent = () => {
  39. // Start off with an invalid view (empty is invalid)
  40. const initialData = initializeOrg({
  41. organization,
  42. router: {
  43. location: {query: {query: 'tag:value'}},
  44. },
  45. project: 1,
  46. projects: [],
  47. });
  48. return render(<TestComponent />, {context: initialData.routerContext});
  49. };
  50. it('renders yAxisOptions with yAxisValue selected', function () {
  51. renderComponent();
  52. expect(
  53. within(screen.getByTestId('count()')).getByTestId('icon-check-mark')
  54. ).toBeInTheDocument();
  55. expect(
  56. within(screen.getByTestId('failure_count()')).getByTestId('icon-check-mark')
  57. ).toBeInTheDocument();
  58. expect(
  59. // eslint-disable-next-line testing-library/prefer-presence-queries
  60. within(screen.getByTestId('count_unique(user)')).queryByTestId('icon-check-mark')
  61. ).not.toBeInTheDocument();
  62. });
  63. it('calls onChange prop with new checkbox option state', function () {
  64. renderComponent();
  65. userEvent.click(screen.getByTestId('count()'));
  66. expect(onChangeStub).toHaveBeenCalledWith(['failure_count()']);
  67. onChangeStub.mockClear();
  68. userEvent.click(screen.getByTestId('count()'));
  69. expect(onChangeStub).toHaveBeenCalledWith(['failure_count()', 'count()']);
  70. onChangeStub.mockClear();
  71. userEvent.click(screen.getByTestId('failure_count()'));
  72. expect(onChangeStub).toHaveBeenCalledWith(['count()']);
  73. onChangeStub.mockClear();
  74. userEvent.click(screen.getByTestId('failure_count()'));
  75. expect(onChangeStub).toHaveBeenCalledWith(['count()', 'failure_count()']);
  76. onChangeStub.mockClear();
  77. userEvent.click(screen.getByTestId('count_unique(user)'));
  78. expect(onChangeStub).toHaveBeenCalledWith([
  79. 'count()',
  80. 'failure_count()',
  81. 'count_unique(user)',
  82. ]);
  83. });
  84. it('does not uncheck options when clicked if only one option is currently selected', function () {
  85. renderComponent();
  86. userEvent.click(screen.getByTestId('count()'));
  87. expect(onChangeStub).toHaveBeenCalledWith(['failure_count()']);
  88. userEvent.click(screen.getByTestId('failure_count()'));
  89. expect(onChangeStub).toHaveBeenCalledWith(['failure_count()']);
  90. });
  91. it('only allows up to 3 options to be checked at one time', function () {
  92. renderComponent();
  93. userEvent.click(screen.getByTestId('count_unique(user)'));
  94. expect(onChangeStub).toHaveBeenCalledWith([
  95. 'count()',
  96. 'failure_count()',
  97. 'count_unique(user)',
  98. ]);
  99. onChangeStub.mockClear();
  100. userEvent.click(screen.getByTestId('avg(transaction.duration)'));
  101. expect(onChangeStub).not.toHaveBeenCalledWith([
  102. 'count()',
  103. 'failure_count()',
  104. 'count_unique(user)',
  105. 'avg(transaction.duration)',
  106. ]);
  107. onChangeStub.mockClear();
  108. userEvent.click(screen.getByTestId('count_unique(user)'));
  109. expect(onChangeStub).toHaveBeenCalledWith(['count()', 'failure_count()']);
  110. onChangeStub.mockClear();
  111. userEvent.click(screen.getByTestId('count_unique(user)'));
  112. expect(onChangeStub).toHaveBeenCalledWith([
  113. 'count()',
  114. 'failure_count()',
  115. 'count_unique(user)',
  116. ]);
  117. });
  118. });