selectedGroupStore.spec.tsx 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. import GroupStore from 'sentry/stores/groupStore';
  2. import SelectedGroupStore from 'sentry/stores/selectedGroupStore';
  3. function makeRecords(records: Record<string, boolean>) {
  4. return new Map(Object.entries(records));
  5. }
  6. describe('SelectedGroupStore', function () {
  7. let trigger: jest.SpyInstance;
  8. beforeEach(function () {
  9. SelectedGroupStore.records = new Map();
  10. trigger = jest.spyOn(SelectedGroupStore, 'trigger').mockImplementation(() => {});
  11. });
  12. afterEach(function () {
  13. trigger.mockRestore();
  14. });
  15. describe('prune()', function () {
  16. it('removes records no longer in the GroupStore', function () {
  17. jest.spyOn(GroupStore, 'getAllItemIds').mockImplementation(() => ['3']);
  18. SelectedGroupStore.records = makeRecords({1: true, 2: true, 3: true});
  19. SelectedGroupStore.prune();
  20. expect([...SelectedGroupStore.records.entries()]).toEqual([['3', true]]);
  21. });
  22. it("doesn't have any effect when already in sync", function () {
  23. jest.spyOn(GroupStore, 'getAllItemIds').mockImplementation(() => ['1', '2', '3']);
  24. SelectedGroupStore.records = makeRecords({1: true, 2: true, 3: true});
  25. SelectedGroupStore.prune();
  26. expect([...SelectedGroupStore.records.entries()]).toEqual([
  27. ['1', true],
  28. ['2', true],
  29. ['3', true],
  30. ]);
  31. });
  32. });
  33. describe('add()', function () {
  34. it("defaults value of new ids to 'allSelected()'", function () {
  35. SelectedGroupStore.records = makeRecords({1: true});
  36. SelectedGroupStore.add(['2']);
  37. expect([...SelectedGroupStore.records.entries()]).toEqual([
  38. ['1', true],
  39. ['2', true],
  40. ]);
  41. });
  42. it('does not update existing ids', function () {
  43. SelectedGroupStore.records = makeRecords({1: false, 2: true});
  44. SelectedGroupStore.add(['3']);
  45. expect([...SelectedGroupStore.records.entries()]).toEqual([
  46. ['1', false],
  47. ['2', true],
  48. ['3', false],
  49. ]);
  50. });
  51. });
  52. describe('onGroupChange()', function () {
  53. let prune: jest.SpyInstance;
  54. let add: jest.SpyInstance;
  55. beforeEach(function () {
  56. prune = jest.spyOn(SelectedGroupStore, 'prune');
  57. add = jest.spyOn(SelectedGroupStore, 'add');
  58. });
  59. afterEach(function () {});
  60. it('adds new ids', function () {
  61. SelectedGroupStore.onGroupChange(new Set());
  62. expect(add).toHaveBeenCalled();
  63. });
  64. it('prunes stale records', function () {
  65. SelectedGroupStore.onGroupChange(new Set());
  66. expect(prune).toHaveBeenCalled();
  67. });
  68. it('triggers an update', function () {
  69. SelectedGroupStore.onGroupChange(new Set());
  70. expect(trigger).toHaveBeenCalled();
  71. });
  72. });
  73. describe('allSelected()', function () {
  74. it('returns true when all ids are selected', function () {
  75. SelectedGroupStore.records = makeRecords({1: true, 2: true});
  76. expect(SelectedGroupStore.allSelected()).toBe(true);
  77. });
  78. it('returns false when some ids are selected', function () {
  79. SelectedGroupStore.records = makeRecords({1: true, 2: false});
  80. expect(SelectedGroupStore.allSelected()).toBe(false);
  81. });
  82. it('returns false when no ids are selected', function () {
  83. SelectedGroupStore.records = makeRecords({1: false, 2: false});
  84. expect(SelectedGroupStore.allSelected()).toBe(false);
  85. });
  86. it('returns false when there are no ids', function () {
  87. expect(SelectedGroupStore.allSelected()).toBe(false);
  88. });
  89. });
  90. describe('anySelected()', function () {
  91. it('returns true if any ids are selected', function () {
  92. SelectedGroupStore.records = makeRecords({1: true, 2: false});
  93. expect(SelectedGroupStore.anySelected()).toBe(true);
  94. });
  95. it('returns false when no ids are selected', function () {
  96. SelectedGroupStore.records = makeRecords({1: false, 2: false});
  97. expect(SelectedGroupStore.anySelected()).toBe(false);
  98. });
  99. });
  100. describe('multiSelected()', function () {
  101. it('returns true when multiple ids are selected', function () {
  102. SelectedGroupStore.records = makeRecords({1: true, 2: true, 3: false});
  103. expect(SelectedGroupStore.multiSelected()).toBe(true);
  104. });
  105. it('returns false when a single id is selected', function () {
  106. SelectedGroupStore.records = makeRecords({1: true, 2: false});
  107. expect(SelectedGroupStore.multiSelected()).toBe(false);
  108. });
  109. it('returns false when no ids are selected', function () {
  110. SelectedGroupStore.records = makeRecords({1: false, 2: false});
  111. expect(SelectedGroupStore.multiSelected()).toBe(false);
  112. });
  113. });
  114. describe('getSelectedIds()', function () {
  115. it('returns selected ids', function () {
  116. SelectedGroupStore.records = makeRecords({1: true, 2: false, 3: true});
  117. const ids = SelectedGroupStore.getSelectedIds();
  118. expect(ids.has('1')).toBe(true);
  119. expect(ids.has('3')).toBe(true);
  120. expect(ids.size).toEqual(2);
  121. });
  122. it('returns empty set with no selected ids', function () {
  123. SelectedGroupStore.records = makeRecords({1: false});
  124. const ids = SelectedGroupStore.getSelectedIds();
  125. expect(ids.has('1')).toBe(false);
  126. expect(ids.size).toEqual(0);
  127. });
  128. });
  129. describe('isSelected()', function () {
  130. it('returns true if id is selected', function () {
  131. SelectedGroupStore.records = makeRecords({1: true});
  132. expect(SelectedGroupStore.isSelected('1')).toBe(true);
  133. });
  134. it('returns false if id is unselected or unknown', function () {
  135. SelectedGroupStore.records = makeRecords({1: false});
  136. expect(SelectedGroupStore.isSelected('1')).toBe(false);
  137. expect(SelectedGroupStore.isSelected('2')).toBe(false);
  138. expect(SelectedGroupStore.isSelected('')).toBe(false);
  139. });
  140. });
  141. describe('deselectAll()', function () {
  142. it('sets all records to false', function () {
  143. SelectedGroupStore.records = makeRecords({1: true, 2: true, 3: false});
  144. SelectedGroupStore.deselectAll();
  145. expect([...SelectedGroupStore.records.entries()]).toEqual([
  146. ['1', false],
  147. ['2', false],
  148. ['3', false],
  149. ]);
  150. });
  151. it('triggers an update', function () {
  152. SelectedGroupStore.deselectAll();
  153. expect(trigger).toHaveBeenCalled();
  154. });
  155. });
  156. describe('toggleSelect()', function () {
  157. it('toggles state given pre-existing id', function () {
  158. SelectedGroupStore.records = makeRecords({1: true});
  159. SelectedGroupStore.toggleSelect('1');
  160. expect(SelectedGroupStore.records.get('1')).toBe(false);
  161. });
  162. it('does not toggle state given unknown id', function () {
  163. SelectedGroupStore.toggleSelect('1');
  164. SelectedGroupStore.toggleSelect('');
  165. expect([...SelectedGroupStore.records.entries()]).toEqual([]);
  166. });
  167. it('triggers an update given pre-existing id', function () {
  168. SelectedGroupStore.records = makeRecords({1: true});
  169. SelectedGroupStore.toggleSelect('1');
  170. expect(trigger).toHaveBeenCalled();
  171. });
  172. it('does not trigger an update given unknown id', function () {
  173. SelectedGroupStore.toggleSelect('');
  174. expect(trigger).not.toHaveBeenCalled();
  175. });
  176. });
  177. describe('toggleSelectAll()', function () {
  178. it('selects all ids if any are unselected', function () {
  179. SelectedGroupStore.records = makeRecords({1: true, 2: false});
  180. SelectedGroupStore.toggleSelectAll();
  181. expect([...SelectedGroupStore.records.entries()]).toEqual([
  182. ['1', true],
  183. ['2', true],
  184. ]);
  185. });
  186. it('unselects all ids if all are selected', function () {
  187. SelectedGroupStore.records = makeRecords({1: true, 2: true});
  188. SelectedGroupStore.toggleSelectAll();
  189. expect([...SelectedGroupStore.records.entries()]).toEqual([
  190. ['1', false],
  191. ['2', false],
  192. ]);
  193. });
  194. it('triggers an update', function () {
  195. SelectedGroupStore.toggleSelectAll();
  196. expect(trigger).toHaveBeenCalled();
  197. });
  198. });
  199. describe('shiftSelectItems()', function () {
  200. it('toggles all between last selected and new selection', function () {
  201. const ids = ['11', '12', '13', '14', '15'];
  202. jest.spyOn(GroupStore, 'getAllItemIds').mockImplementation(() => ids);
  203. SelectedGroupStore.add(ids);
  204. SelectedGroupStore.toggleSelect('12');
  205. SelectedGroupStore.shiftToggleItems('14');
  206. expect([...SelectedGroupStore.records.entries()]).toEqual([
  207. ['11', false],
  208. ['12', true],
  209. ['13', true],
  210. ['14', true],
  211. ['15', false],
  212. ]);
  213. });
  214. it('toggles all between last selected and new selection backwards', function () {
  215. const ids = ['11', '12', '13', '14', '15'];
  216. jest.spyOn(GroupStore, 'getAllItemIds').mockImplementation(() => ids);
  217. SelectedGroupStore.add(ids);
  218. SelectedGroupStore.toggleSelect('14');
  219. SelectedGroupStore.shiftToggleItems('12');
  220. expect([...SelectedGroupStore.records.entries()]).toEqual([
  221. ['11', false],
  222. ['12', true],
  223. ['13', true],
  224. ['14', true],
  225. ['15', false],
  226. ]);
  227. });
  228. it('deslects after selecting', function () {
  229. const ids = ['11', '12', '13', '14', '15'];
  230. jest.spyOn(GroupStore, 'getAllItemIds').mockImplementation(() => ids);
  231. SelectedGroupStore.add(ids);
  232. SelectedGroupStore.toggleSelect('11');
  233. // Select everything
  234. SelectedGroupStore.shiftToggleItems('15');
  235. expect([...SelectedGroupStore.records.entries()]).toEqual([
  236. ['11', true],
  237. ['12', true],
  238. ['13', true],
  239. ['14', true],
  240. ['15', true],
  241. ]);
  242. // Deslect between 14 and 12
  243. SelectedGroupStore.toggleSelect('14');
  244. SelectedGroupStore.shiftToggleItems('12');
  245. expect([...SelectedGroupStore.records.entries()]).toEqual([
  246. ['11', true],
  247. ['12', false],
  248. ['13', false],
  249. ['14', false],
  250. ['15', true],
  251. ]);
  252. });
  253. });
  254. });