index.spec.tsx 4.2 KB

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