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