|
| 1 | +import { test, expect } from '@playwright/test'; |
| 2 | +import { OnScrollFn } from '@src/components/types/ScrollPosition'; |
| 3 | +import { HorizontalLayoutMatrixBrain } from '@src/components/VirtualBrain/HorizontalLayoutMatrixBrain'; |
| 4 | +import { FnOnRenderRangeChange } from '@src/components/VirtualBrain/MatrixBrain'; |
| 5 | + |
| 6 | +const sinon = require('sinon'); |
| 7 | + |
| 8 | +type ExtraProps = { callCount: number; firstArg: any }; |
| 9 | + |
| 10 | +export default test.describe.parallel('HorizontalLayoutMatrixBrain', () => { |
| 11 | + test.beforeEach(({ page }) => { |
| 12 | + globalThis.__DEV__ = true; |
| 13 | + page.on('console', async (msg) => { |
| 14 | + const values = []; |
| 15 | + for (const arg of msg.args()) values.push(await arg.jsonValue()); |
| 16 | + console.log(...values); |
| 17 | + }); |
| 18 | + }); |
| 19 | + |
| 20 | + test('getMatrixCoordinatesForHorizontalLayoutPosition', async () => { |
| 21 | + const COL_SIZE = 100; |
| 22 | + const ROW_SIZE = 50; |
| 23 | + const WIDTH = 220; |
| 24 | + const HEIGHT = 160; |
| 25 | + const ROWS = 50; |
| 26 | + const COLS = 2; |
| 27 | + |
| 28 | + const brain = new HorizontalLayoutMatrixBrain(); |
| 29 | + |
| 30 | + brain.update({ |
| 31 | + colWidth: COL_SIZE, |
| 32 | + rowHeight: ROW_SIZE, |
| 33 | + width: WIDTH, |
| 34 | + height: HEIGHT, |
| 35 | + cols: COLS, |
| 36 | + rows: ROWS, |
| 37 | + }); |
| 38 | + |
| 39 | + expect(brain.rowsPerPage).toBe(3); |
| 40 | + |
| 41 | + // rows per page = 3 |
| 42 | + expect( |
| 43 | + brain.getMatrixCoordinatesForHorizontalLayoutPosition({ |
| 44 | + rowIndex: 0, |
| 45 | + colIndex: 0, |
| 46 | + }), |
| 47 | + ).toEqual({ |
| 48 | + rowIndex: 0, |
| 49 | + colIndex: 0, |
| 50 | + }); |
| 51 | + |
| 52 | + expect( |
| 53 | + brain.getMatrixCoordinatesForHorizontalLayoutPosition({ |
| 54 | + rowIndex: 5, |
| 55 | + colIndex: 0, |
| 56 | + }), |
| 57 | + ).toEqual({ |
| 58 | + rowIndex: 2, |
| 59 | + colIndex: 2, |
| 60 | + }); |
| 61 | + |
| 62 | + expect( |
| 63 | + brain.getMatrixCoordinatesForHorizontalLayoutPosition({ |
| 64 | + rowIndex: 10, |
| 65 | + colIndex: 1, |
| 66 | + }), |
| 67 | + ).toEqual({ |
| 68 | + rowIndex: 1, |
| 69 | + colIndex: 7, |
| 70 | + }); |
| 71 | + }); |
| 72 | + |
| 73 | + test('getHorizontalLayoutPositionFromMatrixCoordinates', async () => { |
| 74 | + const COL_SIZE = 100; |
| 75 | + const ROW_SIZE = 50; |
| 76 | + const WIDTH = 220; |
| 77 | + const HEIGHT = 160; |
| 78 | + const ROWS = 50; |
| 79 | + const COLS = 2; |
| 80 | + |
| 81 | + const brain = new HorizontalLayoutMatrixBrain(); |
| 82 | + |
| 83 | + brain.update({ |
| 84 | + colWidth: COL_SIZE, |
| 85 | + rowHeight: ROW_SIZE, |
| 86 | + width: WIDTH, |
| 87 | + height: HEIGHT, |
| 88 | + cols: COLS, |
| 89 | + rows: ROWS, |
| 90 | + }); |
| 91 | + |
| 92 | + expect(brain.rowsPerPage).toBe(3); |
| 93 | + |
| 94 | + // rows per page = 3 |
| 95 | + expect( |
| 96 | + brain.getHorizontalLayoutPositionFromMatrixCoordinates({ |
| 97 | + rowIndex: 1, |
| 98 | + colIndex: 3, |
| 99 | + }), |
| 100 | + ).toEqual({ |
| 101 | + rowIndex: 4, |
| 102 | + colIndex: 1, |
| 103 | + }); |
| 104 | + |
| 105 | + expect( |
| 106 | + brain.getHorizontalLayoutPositionFromMatrixCoordinates({ |
| 107 | + rowIndex: 0, |
| 108 | + colIndex: 6, |
| 109 | + }), |
| 110 | + ).toEqual({ |
| 111 | + rowIndex: 9, |
| 112 | + colIndex: 0, |
| 113 | + }); |
| 114 | + }); |
| 115 | + test('should correctly give me the render range', async () => { |
| 116 | + const COL_SIZE = 100; |
| 117 | + const ROW_SIZE = 50; |
| 118 | + const WIDTH = 230; |
| 119 | + const HEIGHT = 420; |
| 120 | + const ROWS = 50; |
| 121 | + const COLS = 2; |
| 122 | + |
| 123 | + const brain = new HorizontalLayoutMatrixBrain(); |
| 124 | + |
| 125 | + brain.update({ |
| 126 | + colWidth: COL_SIZE, |
| 127 | + rowHeight: ROW_SIZE, |
| 128 | + width: WIDTH, |
| 129 | + height: HEIGHT, |
| 130 | + cols: COLS, |
| 131 | + rows: ROWS, |
| 132 | + }); |
| 133 | + |
| 134 | + const initialRange = { |
| 135 | + start: [0, 0], |
| 136 | + end: [8, 4], |
| 137 | + }; |
| 138 | + expect(brain.getRenderRange()).toEqual(initialRange); |
| 139 | + |
| 140 | + // scroll just a bit, to not trigger a render range change |
| 141 | + brain.setScrollPosition({ |
| 142 | + scrollLeft: 20, |
| 143 | + scrollTop: 0, |
| 144 | + }); |
| 145 | + |
| 146 | + expect(brain.getRenderRange()).toEqual(initialRange); |
| 147 | + |
| 148 | + return; |
| 149 | + // scroll horizontally more, to trigger a render range change on horizontal only |
| 150 | + brain.setScrollPosition({ |
| 151 | + scrollLeft: 120, |
| 152 | + scrollTop: 0, |
| 153 | + }); |
| 154 | + |
| 155 | + expect(brain.getRenderRange()).toEqual({ |
| 156 | + start: [0, 1], |
| 157 | + end: [ |
| 158 | + Math.ceil(HEIGHT / ROW_SIZE) + 1, |
| 159 | + Math.min(Math.ceil(WIDTH / COL_SIZE) + 2, COLS), |
| 160 | + ], |
| 161 | + }); |
| 162 | + |
| 163 | + // scroll horizontally even more, to trigger a render range change on horizontal only |
| 164 | + brain.setScrollPosition({ |
| 165 | + scrollLeft: 520, |
| 166 | + scrollTop: 0, |
| 167 | + }); |
| 168 | + |
| 169 | + expect(brain.getRenderRange()).toEqual({ |
| 170 | + start: [0, 3], |
| 171 | + end: [Math.ceil(HEIGHT / ROW_SIZE) + 1, 7], |
| 172 | + }); |
| 173 | + }); |
| 174 | + |
| 175 | + test('should correctly return the render range when scrolling horizontally', async () => { |
| 176 | + const COL_SIZE = 100; |
| 177 | + const ROW_SIZE = 50; |
| 178 | + const WIDTH = 230; |
| 179 | + const HEIGHT = 420; |
| 180 | + const ROWS = 20; |
| 181 | + const COLS = 7; |
| 182 | + |
| 183 | + const brain = new HorizontalLayoutMatrixBrain(); |
| 184 | + |
| 185 | + brain.update({ |
| 186 | + colWidth: COL_SIZE, |
| 187 | + rowHeight: ROW_SIZE, |
| 188 | + width: WIDTH, |
| 189 | + height: HEIGHT, |
| 190 | + cols: COLS, |
| 191 | + rows: ROWS, |
| 192 | + }); |
| 193 | + |
| 194 | + brain.setScrollPosition({ |
| 195 | + scrollLeft: 220, |
| 196 | + scrollTop: 0, |
| 197 | + }); |
| 198 | + |
| 199 | + expect(brain.getRenderRange()).toEqual({ |
| 200 | + start: [0, 2], |
| 201 | + end: [8, 6], |
| 202 | + }); |
| 203 | + }); |
| 204 | + |
| 205 | + test('should correctly have initial render range', async () => { |
| 206 | + const COL_SIZE = 100; |
| 207 | + const ROW_SIZE = 50; |
| 208 | + const WIDTH = 710; |
| 209 | + const HEIGHT = 392; |
| 210 | + const ROWS = 30; |
| 211 | + const COLS = 2; |
| 212 | + |
| 213 | + const brain = new HorizontalLayoutMatrixBrain(); |
| 214 | + |
| 215 | + brain.update({ |
| 216 | + colWidth: COL_SIZE, |
| 217 | + rowHeight: ROW_SIZE, |
| 218 | + width: WIDTH, |
| 219 | + height: HEIGHT, |
| 220 | + cols: COLS, |
| 221 | + rows: ROWS, |
| 222 | + }); |
| 223 | + |
| 224 | + expect(brain.getRenderRange()).toEqual({ |
| 225 | + start: [0, 0], |
| 226 | + end: [7, 9], |
| 227 | + }); |
| 228 | + }); |
| 229 | + |
| 230 | + test.skip('THIS WAS COPIED FROM MATRIX BRAIN AND NOT ADJUSTED - should correctly trigger onRenderRange change when scrolling and changing available size', async ({ |
| 231 | + page, |
| 232 | + }) => { |
| 233 | + const COL_SIZE = 100; |
| 234 | + const ROW_SIZE = 50; |
| 235 | + const WIDTH = 230; |
| 236 | + const HEIGHT = 420; |
| 237 | + const ROWS = 20; |
| 238 | + const COLS = 7; |
| 239 | + |
| 240 | + const brain = new HorizontalLayoutMatrixBrain(); |
| 241 | + |
| 242 | + brain.update({ |
| 243 | + colWidth: COL_SIZE, |
| 244 | + rowHeight: ROW_SIZE, |
| 245 | + width: WIDTH, |
| 246 | + height: HEIGHT, |
| 247 | + cols: COLS, |
| 248 | + rows: ROWS, |
| 249 | + }); |
| 250 | + |
| 251 | + const onRenderRangeChange = sinon.fake() as FnOnRenderRangeChange & |
| 252 | + ExtraProps; |
| 253 | + const onScroll = sinon.fake() as OnScrollFn & ExtraProps; |
| 254 | + |
| 255 | + brain.onRenderRangeChange(onRenderRangeChange); |
| 256 | + brain.onScroll(onScroll); |
| 257 | + |
| 258 | + brain.update({ |
| 259 | + width: WIDTH + 100, |
| 260 | + height: HEIGHT + 100, |
| 261 | + }); |
| 262 | + |
| 263 | + await page.waitForTimeout(5); |
| 264 | + |
| 265 | + expect(onRenderRangeChange.callCount).toBe(1); |
| 266 | + expect(onRenderRangeChange.firstArg).toEqual({ |
| 267 | + start: [0, 0], |
| 268 | + end: [12, 5], |
| 269 | + }); |
| 270 | + |
| 271 | + // scroll down and right a bit, but not too much so the render range stays the same |
| 272 | + brain.setScrollPosition({ |
| 273 | + scrollTop: 10, |
| 274 | + scrollLeft: 30, |
| 275 | + }); |
| 276 | + |
| 277 | + await page.waitForTimeout(5); |
| 278 | + |
| 279 | + expect(onRenderRangeChange.callCount).toBe(1); |
| 280 | + expect(onScroll.callCount).toBe(1); |
| 281 | + |
| 282 | + brain.setScrollPosition({ |
| 283 | + scrollTop: 60, |
| 284 | + scrollLeft: 120, |
| 285 | + }); |
| 286 | + |
| 287 | + await page.waitForTimeout(5); |
| 288 | + |
| 289 | + expect(onRenderRangeChange.callCount).toBe(2); |
| 290 | + expect(onRenderRangeChange.firstArg).toEqual({ |
| 291 | + start: [1, 1], |
| 292 | + end: [13, 6], |
| 293 | + }); |
| 294 | + |
| 295 | + // now set a new size |
| 296 | + |
| 297 | + brain.update({ |
| 298 | + height: HEIGHT + 200, |
| 299 | + width: WIDTH + 200, |
| 300 | + }); |
| 301 | + |
| 302 | + await page.waitForTimeout(5); |
| 303 | + |
| 304 | + // and expect render range to have changed |
| 305 | + expect(onRenderRangeChange.callCount).toBe(3); |
| 306 | + expect(onRenderRangeChange.firstArg).toEqual({ |
| 307 | + start: [1, 1], |
| 308 | + end: [15, 7], |
| 309 | + }); |
| 310 | + }); |
| 311 | + |
| 312 | + test.skip('THIS WAS COPIED FROM MATRIX BRAIN AND NOT ADJUSTED - should correctly trigger onRenderRangeChange when count gets smaller than the max render range', async ({ |
| 313 | + page, |
| 314 | + }) => { |
| 315 | + const COL_SIZE = 100; |
| 316 | + const ROW_SIZE = 50; |
| 317 | + const WIDTH = 230; |
| 318 | + const HEIGHT = 420; |
| 319 | + const ROWS = 20; |
| 320 | + const COLS = 7; |
| 321 | + |
| 322 | + const brain = new HorizontalLayoutMatrixBrain(); |
| 323 | + |
| 324 | + brain.update({ |
| 325 | + colWidth: COL_SIZE, |
| 326 | + rowHeight: ROW_SIZE, |
| 327 | + width: WIDTH, |
| 328 | + height: HEIGHT, |
| 329 | + cols: COLS, |
| 330 | + rows: ROWS, |
| 331 | + }); |
| 332 | + |
| 333 | + const onRenderRangeChange = sinon.fake() as FnOnRenderRangeChange & |
| 334 | + ExtraProps; |
| 335 | + const onScroll = sinon.fake() as OnScrollFn & ExtraProps; |
| 336 | + |
| 337 | + brain.onRenderRangeChange(onRenderRangeChange); |
| 338 | + brain.onScroll(onScroll); |
| 339 | + |
| 340 | + await page.waitForTimeout(5); |
| 341 | + |
| 342 | + expect(brain.getRenderRange()).toEqual({ |
| 343 | + start: [0, 0], |
| 344 | + end: [10, 4], |
| 345 | + }); |
| 346 | + |
| 347 | + brain.update({ |
| 348 | + rows: 5, |
| 349 | + }); |
| 350 | + await page.waitForTimeout(5); |
| 351 | + |
| 352 | + expect(onRenderRangeChange.callCount).toEqual(1); |
| 353 | + expect(onRenderRangeChange.firstArg).toEqual({ |
| 354 | + start: [0, 0], |
| 355 | + end: [5, 4], |
| 356 | + }); |
| 357 | + }); |
| 358 | +}); |
0 commit comments