forked from react-grid-layout/react-grid-layout
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils-test.js
414 lines (390 loc) · 12 KB
/
utils-test.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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
// @flow
/* eslint-env jest */
import {
bottom,
collides,
validateLayout,
moveElement,
compact,
sortLayoutItemsByRowCol
} from "../../lib/utils.js";
/*:: import type { Layout } from "../../lib/utils.js"; */
/*:: declare function describe(name: string, fn: Function): void; */
/*:: declare function it(name: string, fn: Function): void; */
/*:: declare function expect(any): any; */
describe("bottom", () => {
it("Handles an empty layout as input", () => {
expect(bottom([])).toEqual(0);
});
it("Returns the bottom coordinate of the layout", () => {
expect(
bottom([
{ i: "1", x: 0, y: 1, w: 1, h: 1 },
{ i: "2", x: 1, y: 2, w: 1, h: 1 }
])
).toEqual(3);
});
});
describe("sortLayoutItemsByRowCol", () => {
it("should sort by top to bottom right", () => {
const layout = [
{ x: 1, y: 1, w: 1, h: 1, i: "2" },
{ x: 1, y: 0, w: 1, h: 1, i: "1" },
{ x: 0, y: 1, w: 2, h: 2, i: "3" }
];
expect(sortLayoutItemsByRowCol(layout)).toEqual([
{ x: 1, y: 0, w: 1, h: 1, i: "1" },
{ x: 0, y: 1, w: 2, h: 2, i: "3" },
{ x: 1, y: 1, w: 1, h: 1, i: "2" }
]);
});
});
describe("collides", () => {
it("Returns whether the layout items collide", () => {
expect(
collides(
{ i: "1", x: 0, y: 1, w: 1, h: 1 },
{ i: "2", x: 1, y: 2, w: 1, h: 1 }
)
).toEqual(false);
expect(
collides(
{ i: "1", x: 0, y: 1, w: 1, h: 1 },
{ i: "2", x: 0, y: 1, w: 1, h: 1 }
)
).toEqual(true);
});
});
describe("validateLayout", () => {
it("Validates an empty layout", () => {
validateLayout([]);
});
it("Validates a populated layout", () => {
validateLayout([
{ i: "1", x: 0, y: 1, w: 1, h: 1 },
{ i: "2", x: 1, y: 2, w: 1, h: 1 }
]);
});
it("Throws errors on invalid input", () => {
expect(() => {
// $FlowFixMe: dynamic check
validateLayout([
{ i: "1", x: 0, y: 1, w: 1, h: 1 },
{ i: "2", x: 1, y: 2, w: 1 }
]);
}).toThrowError(/layout\[1\]\.h must be a number!/i);
});
});
describe("moveElement", () => {
function compactAndMove(
layout,
layoutItem,
x,
y,
isUserAction,
preventCollision,
compactType,
cols
) {
return compact(
moveElement(
layout,
layoutItem,
x,
y,
isUserAction,
preventCollision,
compactType,
cols
),
compactType,
cols
);
}
it("Does not change layout when colliding on no rearrangement mode", () => {
const layout = [
{ i: "1", x: 0, y: 1, w: 1, h: 1, moved: false },
{ i: "2", x: 1, y: 2, w: 1, h: 1, moved: false }
];
const layoutItem = layout[0];
expect(
moveElement(
layout,
layoutItem,
1,
2, // x, y
true,
true, // isUserAction, preventCollision
null,
2 // compactType, cols
)
).toEqual([
{ i: "1", x: 0, y: 1, w: 1, h: 1, moved: false },
{ i: "2", x: 1, y: 2, w: 1, h: 1, moved: false }
]);
});
it("Does change layout when colliding in rearrangement mode", () => {
const layout = [
{ i: "1", x: 0, y: 0, w: 1, h: 1, moved: false },
{ i: "2", x: 1, y: 0, w: 1, h: 1, moved: false }
];
const layoutItem = layout[0];
expect(
moveElement(
layout,
layoutItem,
1,
0, // x, y
true,
false, // isUserAction, preventCollision
"vertical",
2 // compactType, cols
)
).toEqual([
{ i: "1", x: 1, y: 0, w: 1, h: 1, moved: true },
{ i: "2", x: 1, y: 1, w: 1, h: 1, moved: true }
]);
});
it("Moves elements out of the way without causing panel jumps when compaction is vertical", () => {
const layout = [
{ x: 0, y: 0, w: 1, h: 10, i: "A" },
{ x: 0, y: 10, w: 1, h: 1, i: "B" },
{ x: 0, y: 11, w: 1, h: 1, i: "C" }
];
// move A down slightly so it collides with C; can cause C to jump above B.
// We instead want B to jump above A (it has the room)
const itemA = layout[0];
expect(
compactAndMove(
layout,
itemA,
0,
1, // x, y
true,
false, // isUserAction, preventCollision
"vertical",
10 // compactType, cols
)
).toEqual([
expect.objectContaining({ x: 0, y: 1, w: 1, h: 10, i: "A" }),
expect.objectContaining({ x: 0, y: 0, w: 1, h: 1, i: "B" }),
expect.objectContaining({ x: 0, y: 11, w: 1, h: 1, i: "C" })
]);
});
it("Calculates the correct collision when moving large object far", () => {
const layout = [
{ x: 0, y: 0, w: 1, h: 10, i: "A" },
{ x: 0, y: 10, w: 1, h: 1, i: "B" },
{ x: 0, y: 11, w: 1, h: 1, i: "C" }
];
// Move A down by 2. This should move B above, but since we don't compact in between,
// C should move below.
const itemA = layout[0];
expect(
moveElement(
layout,
itemA,
0,
2, // x, y
true,
false, // isUserAction, preventCollision
"vertical",
10 // compactType, cols
)
).toEqual([
expect.objectContaining({ x: 0, y: 2, w: 1, h: 10, i: "A" }),
expect.objectContaining({ x: 0, y: 1, w: 1, h: 1, i: "B" }),
expect.objectContaining({ x: 0, y: 12, w: 1, h: 1, i: "C" })
]);
});
it("Moves elements out of the way without causing panel jumps when compaction is vertical (example case 13)", () => {
const layout = [
{ x: 0, y: 0, w: 1, h: 1, i: "A" },
{ x: 1, y: 0, w: 1, h: 1, i: "B" },
{ x: 0, y: 1, w: 2, h: 2, i: "C" }
];
// move A over slightly so it collides with B; can cause C to jump above B
// this test will check that that does not happen
const itemA = layout[0];
expect(
moveElement(
layout,
itemA,
1,
0, // x, y
true,
false, // isUserAction, preventCollision
"vertical",
2 // compactType, cols
)
).toEqual([
{ x: 1, y: 0, w: 1, h: 1, i: "A", moved: true },
{ x: 1, y: 1, w: 1, h: 1, i: "B", moved: true },
{ x: 0, y: 2, w: 2, h: 2, i: "C", moved: true }
]);
});
it("Moves elements out of the way without causing panel jumps when compaction is horizontal", () => {
const layout = [
{ y: 0, x: 0, h: 1, w: 10, i: "A" },
{ y: 0, x: 11, h: 1, w: 1, i: "B" },
{ y: 0, x: 12, h: 1, w: 1, i: "C" }
];
// move A over slightly so it collides with C; can cause C to jump left of B
// this test will check that that does not happen
const itemA = layout[0];
expect(
moveElement(
layout,
itemA,
2,
0, // x, y
true,
false, // isUserAction, preventCollision
"horizontal",
10 // compactType, cols
)
).toEqual([
{ y: 0, x: 2, h: 1, w: 10, moved: true, i: "A" },
{ y: 0, x: 1, h: 1, w: 1, moved: true, i: "B" },
{ y: 0, x: 12, h: 1, w: 1, i: "C" }
]);
});
it("Moves one element to another should cause moving down panels, vert compact, example 1", () => {
// | A | B |
// |C| D |
const layout = [
{ x: 0, y: 0, w: 2, h: 1, i: "A" },
{ x: 2, y: 0, w: 2, h: 1, i: "B" },
{ x: 0, y: 1, w: 1, h: 1, i: "C" },
{ x: 1, y: 1, w: 3, h: 1, i: "D" }
];
// move B left slightly so it collides with A; can cause C to jump above A
// this test will check that that does not happen
const itemB = layout[1];
expect(
compactAndMove(
layout,
itemB,
1,
0, // x, y
true,
false, // isUserAction, preventCollision
"vertical",
4 // compactType, cols
)
).toEqual([
expect.objectContaining({ x: 0, y: 1, w: 2, h: 1, i: "A" }),
expect.objectContaining({ x: 1, y: 0, w: 2, h: 1, i: "B" }),
expect.objectContaining({ x: 0, y: 2, w: 1, h: 1, i: "C" }),
expect.objectContaining({ x: 1, y: 2, w: 3, h: 1, i: "D" })
]);
});
it("Moves one element to another should cause moving down panels, vert compact, example 2", () => {
// | A |
// |B|C|
// | |
//
// Moving C above A should not move B above A
const layout = [
{ x: 0, y: 0, w: 2, h: 1, i: "A" },
{ x: 0, y: 1, w: 1, h: 1, i: "B" },
{ x: 1, y: 1, w: 1, h: 2, i: "C" }
];
// Move C up.
const itemB = layout[2];
expect(
compactAndMove(
layout,
itemB,
1,
0, // x, y
true,
false, // isUserAction, preventCollision
"vertical",
4 // compactType, cols
)
).toEqual([
expect.objectContaining({ x: 0, y: 2, w: 2, h: 1, i: "A" }),
expect.objectContaining({ x: 0, y: 3, w: 1, h: 1, i: "B" }),
expect.objectContaining({ x: 1, y: 0, w: 1, h: 2, i: "C" })
]);
});
});
describe("compact vertical", () => {
it("Removes empty vertical space above item", () => {
const layout = [{ i: "1", x: 0, y: 1, w: 1, h: 1 }];
expect(compact(layout, "vertical", 10)).toEqual([
{ i: "1", x: 0, y: 0, w: 1, h: 1, moved: false, static: false }
]);
});
it("Resolve collision by moving item further down in array", () => {
const layout = [
{ x: 0, y: 0, w: 1, h: 5, i: "1" },
{ x: 0, y: 1, w: 1, h: 1, i: "2" }
];
expect(compact(layout, "vertical", 10)).toEqual([
{ x: 0, y: 0, w: 1, h: 5, i: "1", moved: false, static: false },
{ x: 0, y: 5, w: 1, h: 1, i: "2", moved: false, static: false }
]);
});
it("Handles recursive collision by moving new collisions out of the way before moving item down", () => {
const layout = [
{ x: 0, y: 0, w: 2, h: 5, i: "1" },
{ x: 0, y: 0, w: 10, h: 1, i: "2" },
{ x: 5, y: 1, w: 1, h: 1, i: "3" },
{ x: 5, y: 2, w: 1, h: 1, i: "4" },
{ x: 5, y: 3, w: 1, h: 1, i: "5", static: true }
];
expect(compact(layout, "vertical", 10)).toEqual([
{ x: 0, y: 0, w: 2, h: 5, i: "1", moved: false, static: false },
{ x: 0, y: 5, w: 10, h: 1, i: "2", moved: false, static: false },
{ x: 5, y: 6, w: 1, h: 1, i: "3", moved: false, static: false },
{ x: 5, y: 7, w: 1, h: 1, i: "4", moved: false, static: false },
{ x: 5, y: 3, w: 1, h: 1, i: "5", moved: false, static: true }
]);
});
it("Clones layout items (does not modify input)", () => {
const layout = [
{ x: 0, y: 0, w: 2, h: 5, i: "1" },
{ x: 0, y: 0, w: 10, h: 1, i: "2" }
];
const out = compact(layout, "vertical", 10);
layout.forEach(item => {
expect(out.includes(item)).toEqual(false);
});
});
});
describe("compact horizontal", () => {
it("compact horizontal should remove empty horizontal space to left of item", () => {
const layout = [{ x: 5, y: 5, w: 1, h: 1, i: "1" }];
expect(compact(layout, "horizontal", 10)).toEqual([
{ x: 0, y: 0, w: 1, h: 1, i: "1", moved: false, static: false }
]);
});
it("Resolve collision by moving item further to the right in array", () => {
const layout = [
{ y: 0, x: 0, h: 1, w: 5, i: "1" },
{ y: 0, x: 1, h: 1, w: 1, i: "2" }
];
expect(compact(layout, "horizontal", 10)).toEqual([
{ y: 0, x: 0, h: 1, w: 5, i: "1", moved: false, static: false },
{ y: 0, x: 5, h: 1, w: 1, i: "2", moved: false, static: false }
]);
});
it("Handles recursive collision by moving new collisions out of the way before moving item to the right", () => {
const layout = [
{ y: 0, x: 0, h: 2, w: 5, i: "1" },
{ y: 1, x: 0, h: 10, w: 1, i: "2" },
{ y: 5, x: 1, h: 1, w: 1, i: "3" },
{ y: 5, x: 2, h: 1, w: 1, i: "4" },
{ y: 5, x: 2, h: 1, w: 1, i: "5", static: true }
];
expect(compact(layout, "horizontal", 10)).toEqual([
{ y: 0, x: 0, h: 2, w: 5, i: "1", moved: false, static: false },
{ y: 1, x: 5, h: 10, w: 1, i: "2", moved: false, static: false },
{ y: 5, x: 6, h: 1, w: 1, i: "3", moved: false, static: false },
{ y: 5, x: 7, h: 1, w: 1, i: "4", moved: false, static: false },
{ y: 5, x: 2, h: 1, w: 1, i: "5", moved: false, static: true }
]);
});
});