selectedGroupStore.spec.jsx 8.8 KB

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