optionSelector.spec.tsx 4.6 KB

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