index.spec.tsx 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. import {Fragment} from 'react';
  2. import {render, screen, userEvent} from 'sentry-test/reactTestingLibrary';
  3. import BulkController from 'sentry/components/bulkController';
  4. describe('BulkController', function () {
  5. const pageIds = ['1', '2', '3'];
  6. const renderProp = jest.fn();
  7. let toggleRow: any,
  8. selectPage: any,
  9. deselectPage: any,
  10. selectAll: any,
  11. unselectAll: any;
  12. const renderComponent = (pIds = pageIds, defaultSelectedIds?: string[]) => {
  13. const wrapper = render(
  14. <BulkController
  15. allRowsCount={32}
  16. pageIds={pIds}
  17. defaultSelectedIds={defaultSelectedIds}
  18. columnsCount={4}
  19. >
  20. {({
  21. isAllSelected,
  22. isPageSelected,
  23. selectedIds,
  24. onRowToggle,
  25. onAllRowsToggle,
  26. onPageRowsToggle,
  27. }) => {
  28. renderProp(isAllSelected, isPageSelected, selectedIds);
  29. return (
  30. <Fragment>
  31. {isPageSelected && 'whole page selected'}
  32. {isAllSelected && 'everything selected'}
  33. <button data-test-id="selectAll" onClick={() => onAllRowsToggle(true)} />
  34. <button data-test-id="selectPage" onClick={() => onPageRowsToggle(true)} />
  35. <button data-test-id="unselectAll" onClick={() => onAllRowsToggle(false)} />
  36. <button
  37. data-test-id="deselectPage"
  38. onClick={() => onPageRowsToggle(false)}
  39. />
  40. <button data-test-id="toggleRow" onClick={() => onRowToggle('2')} />
  41. </Fragment>
  42. );
  43. }}
  44. </BulkController>
  45. );
  46. toggleRow = screen.getByTestId('toggleRow');
  47. selectPage = screen.getByTestId('selectPage');
  48. deselectPage = screen.getByTestId('deselectPage');
  49. selectAll = screen.getByTestId('selectAll');
  50. unselectAll = screen.getByTestId('unselectAll');
  51. return wrapper;
  52. };
  53. afterEach(() => {
  54. jest.clearAllMocks();
  55. });
  56. it('sets the defaults', function () {
  57. renderComponent();
  58. expect(renderProp).toHaveBeenLastCalledWith(false, false, []);
  59. });
  60. it('toggles single item', function () {
  61. renderComponent();
  62. userEvent.click(toggleRow);
  63. expect(renderProp).toHaveBeenLastCalledWith(false, false, ['2']);
  64. userEvent.click(toggleRow);
  65. expect(renderProp).toHaveBeenLastCalledWith(false, false, []);
  66. });
  67. it('toggles the page', function () {
  68. renderComponent();
  69. userEvent.click(toggleRow);
  70. expect(renderProp).toHaveBeenLastCalledWith(false, false, ['2']);
  71. userEvent.click(selectPage);
  72. expect(renderProp).toHaveBeenLastCalledWith(false, true, ['2', '1', '3']);
  73. userEvent.click(deselectPage);
  74. expect(renderProp).toHaveBeenLastCalledWith(false, false, []);
  75. });
  76. it('toggles everything', function () {
  77. renderComponent();
  78. userEvent.click(toggleRow);
  79. expect(renderProp).toHaveBeenLastCalledWith(false, false, ['2']);
  80. userEvent.click(selectAll);
  81. expect(renderProp).toHaveBeenLastCalledWith(true, true, ['1', '2', '3']);
  82. userEvent.click(unselectAll);
  83. expect(renderProp).toHaveBeenLastCalledWith(false, false, []);
  84. });
  85. it('deselects one after having everything selected', function () {
  86. renderComponent();
  87. userEvent.click(selectAll);
  88. expect(renderProp).toHaveBeenLastCalledWith(true, true, ['1', '2', '3']);
  89. userEvent.click(toggleRow);
  90. expect(renderProp).toHaveBeenLastCalledWith(false, false, ['1', '3']);
  91. });
  92. describe('with default selectIds', function () {
  93. it('sets the defaults', function () {
  94. renderComponent(pageIds, ['2']);
  95. expect(renderProp).toHaveBeenLastCalledWith(false, false, ['2']);
  96. });
  97. it('page is selected by default', function () {
  98. renderComponent(pageIds, pageIds);
  99. expect(renderProp).toHaveBeenLastCalledWith(false, true, pageIds);
  100. });
  101. it('toggle the last unchecked option, should change button selectAll to true', function () {
  102. const defaultSelectedIds = ['1', '3'];
  103. renderComponent(pageIds, defaultSelectedIds);
  104. expect(renderProp).toHaveBeenLastCalledWith(false, false, defaultSelectedIds);
  105. userEvent.click(toggleRow);
  106. expect(renderProp).toHaveBeenLastCalledWith(false, true, [
  107. ...defaultSelectedIds,
  108. '2',
  109. ]);
  110. });
  111. });
  112. });