menu-sort-helpers-spec.js 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. const { sortMenuItems } = require('../src/menu-sort-helpers');
  2. describe('contextMenu', () => {
  3. describe('dedupes separators', () => {
  4. it('preserves existing submenus', () => {
  5. const items = [{ submenu: [] }];
  6. expect(sortMenuItems(items)).toEqual(items);
  7. });
  8. });
  9. describe('dedupes separators', () => {
  10. it('trims leading separators', () => {
  11. const items = [{ type: 'separator' }, { command: 'core:one' }];
  12. const expected = [{ command: 'core:one' }];
  13. expect(sortMenuItems(items)).toEqual(expected);
  14. });
  15. it('preserves separators at the beginning of set two', () => {
  16. const items = [
  17. { command: 'core:one' },
  18. { type: 'separator' },
  19. { command: 'core:two' }
  20. ];
  21. const expected = [
  22. { command: 'core:one' },
  23. { type: 'separator' },
  24. { command: 'core:two' }
  25. ];
  26. expect(sortMenuItems(items)).toEqual(expected);
  27. });
  28. it('trims trailing separators', () => {
  29. const items = [{ command: 'core:one' }, { type: 'separator' }];
  30. const expected = [{ command: 'core:one' }];
  31. expect(sortMenuItems(items)).toEqual(expected);
  32. });
  33. it('removes duplicate separators across sets', () => {
  34. const items = [
  35. { command: 'core:one' },
  36. { type: 'separator' },
  37. { type: 'separator' },
  38. { command: 'core:two' }
  39. ];
  40. const expected = [
  41. { command: 'core:one' },
  42. { type: 'separator' },
  43. { command: 'core:two' }
  44. ];
  45. expect(sortMenuItems(items)).toEqual(expected);
  46. });
  47. });
  48. describe('can move an item to a different group by merging groups', () => {
  49. it('can move a group of one item', () => {
  50. const items = [
  51. { command: 'core:one' },
  52. { type: 'separator' },
  53. { command: 'core:two' },
  54. { type: 'separator' },
  55. { command: 'core:three', after: ['core:one'] },
  56. { type: 'separator' }
  57. ];
  58. const expected = [
  59. { command: 'core:one' },
  60. { command: 'core:three', after: ['core:one'] },
  61. { type: 'separator' },
  62. { command: 'core:two' }
  63. ];
  64. expect(sortMenuItems(items)).toEqual(expected);
  65. });
  66. it("moves all items in the moving item's group", () => {
  67. const items = [
  68. { command: 'core:one' },
  69. { type: 'separator' },
  70. { command: 'core:two' },
  71. { type: 'separator' },
  72. { command: 'core:three', after: ['core:one'] },
  73. { command: 'core:four' },
  74. { type: 'separator' }
  75. ];
  76. const expected = [
  77. { command: 'core:one' },
  78. { command: 'core:three', after: ['core:one'] },
  79. { command: 'core:four' },
  80. { type: 'separator' },
  81. { command: 'core:two' }
  82. ];
  83. expect(sortMenuItems(items)).toEqual(expected);
  84. });
  85. it("ignores positions relative to commands that don't exist", () => {
  86. const items = [
  87. { command: 'core:one' },
  88. { type: 'separator' },
  89. { command: 'core:two' },
  90. { type: 'separator' },
  91. { command: 'core:three', after: ['core:does-not-exist'] },
  92. { command: 'core:four', after: ['core:one'] },
  93. { type: 'separator' }
  94. ];
  95. const expected = [
  96. { command: 'core:one' },
  97. { command: 'core:three', after: ['core:does-not-exist'] },
  98. { command: 'core:four', after: ['core:one'] },
  99. { type: 'separator' },
  100. { command: 'core:two' }
  101. ];
  102. expect(sortMenuItems(items)).toEqual(expected);
  103. });
  104. it('can handle recursive group merging', () => {
  105. const items = [
  106. { command: 'core:one', after: ['core:three'] },
  107. { command: 'core:two', before: ['core:one'] },
  108. { command: 'core:three' }
  109. ];
  110. const expected = [
  111. { command: 'core:three' },
  112. { command: 'core:two', before: ['core:one'] },
  113. { command: 'core:one', after: ['core:three'] }
  114. ];
  115. expect(sortMenuItems(items)).toEqual(expected);
  116. });
  117. it('can merge multiple groups when given a list of before/after commands', () => {
  118. const items = [
  119. { command: 'core:one' },
  120. { type: 'separator' },
  121. { command: 'core:two' },
  122. { type: 'separator' },
  123. { command: 'core:three', after: ['core:one', 'core:two'] }
  124. ];
  125. const expected = [
  126. { command: 'core:two' },
  127. { command: 'core:one' },
  128. { command: 'core:three', after: ['core:one', 'core:two'] }
  129. ];
  130. expect(sortMenuItems(items)).toEqual(expected);
  131. });
  132. it('can merge multiple groups based on both before/after commands', () => {
  133. const items = [
  134. { command: 'core:one' },
  135. { type: 'separator' },
  136. { command: 'core:two' },
  137. { type: 'separator' },
  138. { command: 'core:three', after: ['core:one'], before: ['core:two'] }
  139. ];
  140. const expected = [
  141. { command: 'core:one' },
  142. { command: 'core:three', after: ['core:one'], before: ['core:two'] },
  143. { command: 'core:two' }
  144. ];
  145. expect(sortMenuItems(items)).toEqual(expected);
  146. });
  147. });
  148. describe('sorts items within their ultimate group', () => {
  149. it('does a simple sort', () => {
  150. const items = [
  151. { command: 'core:two', after: ['core:one'] },
  152. { command: 'core:one' }
  153. ];
  154. expect(sortMenuItems(items)).toEqual([
  155. { command: 'core:one' },
  156. { command: 'core:two', after: ['core:one'] }
  157. ]);
  158. });
  159. it('resolves cycles by ignoring things that conflict', () => {
  160. const items = [
  161. { command: 'core:two', after: ['core:one'] },
  162. { command: 'core:one', after: ['core:two'] }
  163. ];
  164. expect(sortMenuItems(items)).toEqual([
  165. { command: 'core:one', after: ['core:two'] },
  166. { command: 'core:two', after: ['core:one'] }
  167. ]);
  168. });
  169. });
  170. describe('sorts groups', () => {
  171. it('does a simple sort', () => {
  172. const items = [
  173. { command: 'core:two', afterGroupContaining: ['core:one'] },
  174. { type: 'separator' },
  175. { command: 'core:one' }
  176. ];
  177. expect(sortMenuItems(items)).toEqual([
  178. { command: 'core:one' },
  179. { type: 'separator' },
  180. { command: 'core:two', afterGroupContaining: ['core:one'] }
  181. ]);
  182. });
  183. it('resolves cycles by ignoring things that conflict', () => {
  184. const items = [
  185. { command: 'core:two', afterGroupContaining: ['core:one'] },
  186. { type: 'separator' },
  187. { command: 'core:one', afterGroupContaining: ['core:two'] }
  188. ];
  189. expect(sortMenuItems(items)).toEqual([
  190. { command: 'core:one', afterGroupContaining: ['core:two'] },
  191. { type: 'separator' },
  192. { command: 'core:two', afterGroupContaining: ['core:one'] }
  193. ]);
  194. });
  195. it('ignores references to commands that do not exist', () => {
  196. const items = [
  197. { command: 'core:one' },
  198. { type: 'separator' },
  199. {
  200. command: 'core:two',
  201. afterGroupContaining: ['core:does-not-exist']
  202. }
  203. ];
  204. expect(sortMenuItems(items)).toEqual([
  205. { command: 'core:one' },
  206. { type: 'separator' },
  207. { command: 'core:two', afterGroupContaining: ['core:does-not-exist'] }
  208. ]);
  209. });
  210. it('only respects the first matching [before|after]GroupContaining rule in a given group', () => {
  211. const items = [
  212. { command: 'core:one' },
  213. { type: 'separator' },
  214. { command: 'core:three', beforeGroupContaining: ['core:one'] },
  215. { command: 'core:four', afterGroupContaining: ['core:two'] },
  216. { type: 'separator' },
  217. { command: 'core:two' }
  218. ];
  219. expect(sortMenuItems(items)).toEqual([
  220. { command: 'core:three', beforeGroupContaining: ['core:one'] },
  221. { command: 'core:four', afterGroupContaining: ['core:two'] },
  222. { type: 'separator' },
  223. { command: 'core:one' },
  224. { type: 'separator' },
  225. { command: 'core:two' }
  226. ]);
  227. });
  228. });
  229. });