|
| 1 | +// @flow |
| 2 | +/* eslint-env jest */ |
| 3 | + |
| 4 | +import { |
| 5 | + bottom, |
| 6 | + collides, |
| 7 | + compact, |
| 8 | + moveElement, |
| 9 | + sortLayoutItemsByRowCol, |
| 10 | + validateLayout |
| 11 | + } from "../../src/helpers/utils"; |
| 12 | + |
| 13 | + describe("bottom", () => { |
| 14 | + it("Handles an empty layout as input", () => { |
| 15 | + expect(bottom([])).toEqual(0); |
| 16 | + }); |
| 17 | + |
| 18 | + it("Returns the bottom coordinate of the layout", () => { |
| 19 | + expect( |
| 20 | + bottom([ |
| 21 | + { i: "1", x: 0, y: 1, w: 1, h: 1 }, |
| 22 | + { i: "2", x: 1, y: 2, w: 1, h: 1 } |
| 23 | + ]) |
| 24 | + ).toEqual(3); |
| 25 | + }); |
| 26 | + }); |
| 27 | + |
| 28 | + describe("sortLayoutItemsByRowCol", () => { |
| 29 | + it("should sort by top to bottom right", () => { |
| 30 | + const layout = [ |
| 31 | + { x: 1, y: 1, w: 1, h: 1, i: "2" }, |
| 32 | + { x: 1, y: 0, w: 1, h: 1, i: "1" }, |
| 33 | + { x: 0, y: 1, w: 2, h: 2, i: "3" } |
| 34 | + ]; |
| 35 | + expect(sortLayoutItemsByRowCol(layout)).toEqual([ |
| 36 | + { x: 1, y: 0, w: 1, h: 1, i: "1" }, |
| 37 | + { x: 0, y: 1, w: 2, h: 2, i: "3" }, |
| 38 | + { x: 1, y: 1, w: 1, h: 1, i: "2" } |
| 39 | + ]); |
| 40 | + }); |
| 41 | + }); |
| 42 | + |
| 43 | + describe("collides", () => { |
| 44 | + it("Returns whether the layout items collide", () => { |
| 45 | + expect( |
| 46 | + collides( |
| 47 | + { i: "1", x: 0, y: 1, w: 1, h: 1 }, |
| 48 | + { i: "2", x: 1, y: 2, w: 1, h: 1 } |
| 49 | + ) |
| 50 | + ).toEqual(false); |
| 51 | + expect( |
| 52 | + collides( |
| 53 | + { i: "1", x: 0, y: 1, w: 1, h: 1 }, |
| 54 | + { i: "2", x: 0, y: 1, w: 1, h: 1 } |
| 55 | + ) |
| 56 | + ).toEqual(true); |
| 57 | + }); |
| 58 | + }); |
| 59 | + |
| 60 | + describe("validateLayout", () => { |
| 61 | + it("Validates an empty layout", () => { |
| 62 | + validateLayout([]); |
| 63 | + }); |
| 64 | + it("Validates a populated layout", () => { |
| 65 | + validateLayout([ |
| 66 | + { i: "1", x: 0, y: 1, w: 1, h: 1 }, |
| 67 | + { i: "2", x: 1, y: 2, w: 1, h: 1 } |
| 68 | + ]); |
| 69 | + }); |
| 70 | + it("Throws errors on invalid input", () => { |
| 71 | + expect(() => { |
| 72 | + validateLayout([ |
| 73 | + { i: "1", x: 0, y: 1, w: 1, h: 1 }, |
| 74 | + { i: "2", x: 1, y: 2, w: 1 } |
| 75 | + ]); |
| 76 | + }).toThrowError(/layout\[1\]\.h must be a number!/i); |
| 77 | + }); |
| 78 | + }); |
| 79 | + |
| 80 | + describe("moveElement", () => { |
| 81 | + function compactAndMove( |
| 82 | + layout, |
| 83 | + layoutItem, |
| 84 | + x, |
| 85 | + y, |
| 86 | + isUserAction, |
| 87 | + preventCollision |
| 88 | + ) { |
| 89 | + return compact( |
| 90 | + moveElement( |
| 91 | + layout, |
| 92 | + layoutItem, |
| 93 | + x, |
| 94 | + y, |
| 95 | + isUserAction, |
| 96 | + preventCollision |
| 97 | + ) |
| 98 | + ); |
| 99 | + } |
| 100 | + |
| 101 | + it("Does not change layout when colliding on no rearrangement mode", () => { |
| 102 | + const layout = [ |
| 103 | + { i: "1", x: 0, y: 1, w: 1, h: 1, moved: false }, |
| 104 | + { i: "2", x: 1, y: 2, w: 1, h: 1, moved: false } |
| 105 | + ]; |
| 106 | + const layoutItem = layout[0]; |
| 107 | + expect( |
| 108 | + moveElement( |
| 109 | + layout, |
| 110 | + layoutItem, |
| 111 | + 1, |
| 112 | + 2, // x, y |
| 113 | + true, |
| 114 | + true // isUserAction, preventCollision |
| 115 | + ) |
| 116 | + ).toEqual([ |
| 117 | + { i: "1", x: 0, y: 1, w: 1, h: 1, moved: false }, |
| 118 | + { i: "2", x: 1, y: 2, w: 1, h: 1, moved: false } |
| 119 | + ]); |
| 120 | + }); |
| 121 | + |
| 122 | + it("Does change layout when colliding in rearrangement mode", () => { |
| 123 | + const layout = [ |
| 124 | + { i: "1", x: 0, y: 0, w: 1, h: 1, moved: false }, |
| 125 | + { i: "2", x: 1, y: 0, w: 1, h: 1, moved: false } |
| 126 | + ]; |
| 127 | + const layoutItem = layout[0]; |
| 128 | + expect( |
| 129 | + moveElement( |
| 130 | + layout, |
| 131 | + layoutItem, |
| 132 | + 1, |
| 133 | + 0, // x, y |
| 134 | + true, |
| 135 | + false // isUserAction, preventCollision |
| 136 | + ) |
| 137 | + ).toEqual([ |
| 138 | + { i: "1", x: 1, y: 0, w: 1, h: 1, moved: true }, |
| 139 | + { i: "2", x: 1, y: 1, w: 1, h: 1, moved: true } |
| 140 | + ]); |
| 141 | + }); |
| 142 | + |
| 143 | + it("Moves elements out of the way without causing panel jumps when compaction is vertical", () => { |
| 144 | + const layout = [ |
| 145 | + { x: 0, y: 0, w: 1, h: 10, i: "A" }, |
| 146 | + { x: 0, y: 10, w: 1, h: 1, i: "B" }, |
| 147 | + { x: 0, y: 11, w: 1, h: 1, i: "C" } |
| 148 | + ]; |
| 149 | + // move A down slightly so it collides with C; can cause C to jump above B. |
| 150 | + // We instead want B to jump above A (it has the room) |
| 151 | + const itemA = layout[0]; |
| 152 | + expect( |
| 153 | + compactAndMove( |
| 154 | + layout, |
| 155 | + itemA, |
| 156 | + 0, |
| 157 | + 1, // x, y |
| 158 | + true, |
| 159 | + false // isUserAction, preventCollision |
| 160 | + ) |
| 161 | + ).toEqual([ |
| 162 | + expect.objectContaining({ x: 0, y: 1, w: 1, h: 10, i: "A" }), |
| 163 | + expect.objectContaining({ x: 0, y: 0, w: 1, h: 1, i: "B" }), |
| 164 | + expect.objectContaining({ x: 0, y: 11, w: 1, h: 1, i: "C" }) |
| 165 | + ]); |
| 166 | + }); |
| 167 | + |
| 168 | + it("Calculates the correct collision when moving large object far", () => { |
| 169 | + const layout = [ |
| 170 | + { x: 0, y: 0, w: 1, h: 10, i: "A" }, |
| 171 | + { x: 0, y: 10, w: 1, h: 1, i: "B" }, |
| 172 | + { x: 0, y: 11, w: 1, h: 1, i: "C" } |
| 173 | + ]; |
| 174 | + // Move A down by 2. This should move B above, but since we don't compact in between, |
| 175 | + // C should move below. |
| 176 | + const itemA = layout[0]; |
| 177 | + expect( |
| 178 | + moveElement( |
| 179 | + layout, |
| 180 | + itemA, |
| 181 | + 0, |
| 182 | + 2, // x, y |
| 183 | + true, |
| 184 | + false // isUserAction, preventCollision |
| 185 | + ) |
| 186 | + ).toEqual([ |
| 187 | + expect.objectContaining({ x: 0, y: 2, w: 1, h: 10, i: "A" }), |
| 188 | + expect.objectContaining({ x: 0, y: 1, w: 1, h: 1, i: "B" }), |
| 189 | + expect.objectContaining({ x: 0, y: 12, w: 1, h: 1, i: "C" }) |
| 190 | + ]); |
| 191 | + }); |
| 192 | + |
| 193 | + it("Moves elements out of the way without causing panel jumps when compaction is vertical", () => { |
| 194 | + const layout = [ |
| 195 | + { x: 0, y: 0, w: 1, h: 1, i: "A" }, |
| 196 | + { x: 1, y: 0, w: 1, h: 1, i: "B" }, |
| 197 | + { x: 0, y: 1, w: 2, h: 2, i: "C" } |
| 198 | + ]; |
| 199 | + // move A over slightly so it collides with B; can cause C to jump above B |
| 200 | + // this test will check that that does not happen |
| 201 | + const itemA = layout[0]; |
| 202 | + expect( |
| 203 | + moveElement( |
| 204 | + layout, |
| 205 | + itemA, |
| 206 | + 1, |
| 207 | + 0, // x, y |
| 208 | + true, |
| 209 | + false // isUserAction, preventCollision |
| 210 | + ) |
| 211 | + ).toEqual([ |
| 212 | + { x: 1, y: 0, w: 1, h: 1, i: "A", moved: true }, |
| 213 | + { x: 1, y: 1, w: 1, h: 1, i: "B", moved: true }, |
| 214 | + { x: 0, y: 2, w: 2, h: 2, i: "C", moved: true } |
| 215 | + ]); |
| 216 | + }); |
| 217 | + |
| 218 | + it("Moves one element to another should cause moving down panels, vert compact", () => { |
| 219 | + // | A | B | |
| 220 | + // |C| D | |
| 221 | + const layout = [ |
| 222 | + { x: 0, y: 0, w: 2, h: 1, i: "A" }, |
| 223 | + { x: 2, y: 0, w: 2, h: 1, i: "B" }, |
| 224 | + { x: 0, y: 1, w: 1, h: 1, i: "C" }, |
| 225 | + { x: 1, y: 1, w: 3, h: 1, i: "D" } |
| 226 | + ]; |
| 227 | + // move B left slightly so it collides with A; can cause C to jump above A |
| 228 | + // this test will check that that does not happen |
| 229 | + const itemB = layout[1]; |
| 230 | + expect( |
| 231 | + compactAndMove( |
| 232 | + layout, |
| 233 | + itemB, |
| 234 | + 1, |
| 235 | + 0, // x, y |
| 236 | + true, |
| 237 | + false // isUserAction, preventCollision |
| 238 | + ) |
| 239 | + ).toEqual([ |
| 240 | + expect.objectContaining({ x: 0, y: 1, w: 2, h: 1, i: "A" }), |
| 241 | + expect.objectContaining({ x: 1, y: 0, w: 2, h: 1, i: "B" }), |
| 242 | + expect.objectContaining({ x: 0, y: 2, w: 1, h: 1, i: "C" }), |
| 243 | + expect.objectContaining({ x: 1, y: 2, w: 3, h: 1, i: "D" }) |
| 244 | + ]); |
| 245 | + }); |
| 246 | + |
| 247 | + it("Moves one element to another should cause moving down panels, vert compact", () => { |
| 248 | + // | A | |
| 249 | + // |B|C| |
| 250 | + // | | |
| 251 | + // |
| 252 | + // Moving C above A should not move B above A |
| 253 | + const layout = [ |
| 254 | + { x: 0, y: 0, w: 2, h: 1, i: "A" }, |
| 255 | + { x: 0, y: 1, w: 1, h: 1, i: "B" }, |
| 256 | + { x: 1, y: 1, w: 1, h: 2, i: "C" } |
| 257 | + ]; |
| 258 | + // Move C up. |
| 259 | + const itemB = layout[2]; |
| 260 | + expect( |
| 261 | + compactAndMove( |
| 262 | + layout, |
| 263 | + itemB, |
| 264 | + 1, |
| 265 | + 0, // x, y |
| 266 | + true, |
| 267 | + false // isUserAction, preventCollision |
| 268 | + ) |
| 269 | + ).toEqual([ |
| 270 | + expect.objectContaining({ x: 0, y: 2, w: 2, h: 1, i: "A" }), |
| 271 | + expect.objectContaining({ x: 0, y: 3, w: 1, h: 1, i: "B" }), |
| 272 | + expect.objectContaining({ x: 1, y: 0, w: 1, h: 2, i: "C" }) |
| 273 | + ]); |
| 274 | + }); |
| 275 | + }); |
| 276 | + |
| 277 | + describe("compact vertical", () => { |
| 278 | + it("Removes empty vertical space above item", () => { |
| 279 | + const layout = [{ i: "1", x: 0, y: 1, w: 1, h: 1 }]; |
| 280 | + expect(compact(layout, true)).toEqual([ |
| 281 | + { i: "1", x: 0, y: 0, w: 1, h: 1, moved: false } |
| 282 | + ]); |
| 283 | + }); |
| 284 | + |
| 285 | + it("Resolve collision by moving item further down in array", () => { |
| 286 | + const layout = [ |
| 287 | + { x: 0, y: 0, w: 1, h: 5, i: "1" }, |
| 288 | + { x: 0, y: 1, w: 1, h: 1, i: "2" } |
| 289 | + ]; |
| 290 | + expect(compact(layout, true)).toEqual([ |
| 291 | + { x: 0, y: 0, w: 1, h: 5, i: "1", moved: false }, |
| 292 | + { x: 0, y: 5, w: 1, h: 1, i: "2", moved: false } |
| 293 | + ]); |
| 294 | + }); |
| 295 | + |
| 296 | + it("Handles recursive collision by moving new collisions out of the way before moving item down", () => { |
| 297 | + const layout = [ |
| 298 | + { x: 0, y: 0, w: 2, h: 5, i: "1" }, |
| 299 | + { x: 0, y: 0, w: 10, h: 1, i: "2" }, |
| 300 | + { x: 5, y: 1, w: 1, h: 1, i: "3" }, |
| 301 | + { x: 5, y: 2, w: 1, h: 1, i: "4" }, |
| 302 | + { x: 5, y: 3, w: 1, h: 1, i: "5", static: true } |
| 303 | + ]; |
| 304 | + |
| 305 | + expect(compact(layout, true)).toEqual([ |
| 306 | + { x: 0, y: 0, w: 2, h: 5, i: "1", moved: false }, |
| 307 | + { x: 0, y: 5, w: 10, h: 1, i: "2", moved: false }, |
| 308 | + { x: 5, y: 0, w: 1, h: 1, i: "3", moved: false }, |
| 309 | + { x: 5, y: 1, w: 1, h: 1, i: "4", moved: false }, |
| 310 | + { x: 5, y: 3, w: 1, h: 1, i: "5", moved: false, static: true } |
| 311 | + ]); |
| 312 | + }); |
| 313 | + }); |
0 commit comments