|
| 1 | +import { setupIgnoreWindowResizeObserverLoopErrors } from '@lit-labs/virtualizer/support/resize-observer-errors.js'; |
1 | 2 | import { expect } from '@open-wc/testing'; |
2 | 3 | import GridTestFixture from './utils/grid-fixture.js'; |
3 | 4 | import data, { generateFieldPaths } from './utils/test-data.js'; |
4 | 5 |
|
5 | | -const TDD = new GridTestFixture(data); |
| 6 | +// reduced width to force scroll: |
| 7 | +const TDD = new GridTestFixture(data, { width: '300px' }); |
6 | 8 | const keys = generateFieldPaths(data[0]); |
7 | 9 |
|
| 10 | +function isVisibleGrid(element: Element): boolean { |
| 11 | + const rect = element.getBoundingClientRect(); |
| 12 | + const { top, bottom } = TDD.gridBody.getBoundingClientRect(); |
| 13 | + const { left, right } = TDD.grid.getBoundingClientRect(); |
| 14 | + |
| 15 | + return rect.top >= top && rect.bottom <= bottom && rect.left >= left && rect.right <= right; |
| 16 | +} |
| 17 | + |
8 | 18 | describe('Grid activation', () => { |
| 19 | + setupIgnoreWindowResizeObserverLoopErrors(beforeEach, afterEach); |
9 | 20 | beforeEach(async () => await TDD.setUp()); |
10 | 21 | afterEach(() => TDD.tearDown()); |
11 | 22 |
|
@@ -174,6 +185,110 @@ describe('Grid activation', () => { |
174 | 185 |
|
175 | 186 | await TDD.fireNavigationEvent({ key: 'ArrowUp' }); |
176 | 187 | expect(firstRowCityCell.active).to.be.true; |
| 188 | + }); |
| 189 | + }); |
| 190 | + |
| 191 | + describe('navigateTo API', () => { |
| 192 | + // extend test data to move into virtualized scroll: |
| 193 | + const testData = [ |
| 194 | + ...data, |
| 195 | + ...data.map((x) => ({ ...x, id: x.id + data.length })), |
| 196 | + ...data.map((x) => ({ ...x, id: x.id + data.length * 2 })), |
| 197 | + ]; |
| 198 | + |
| 199 | + beforeEach(async () => { |
| 200 | + await TDD.updateProperty('data', testData); |
| 201 | + // expect horizontal and vertical scroll to ensure test setup doesn't change |
| 202 | + expect(TDD.grid.scrollWidth > TDD.grid.clientWidth).to.be.true; |
| 203 | + expect(TDD.gridBody.scrollHeight > TDD.gridBody.clientHeight).to.be.true; |
| 204 | + }); |
| 205 | + |
| 206 | + it('navigates to rows only', async () => { |
| 207 | + await TDD.grid.navigateTo(testData.length - 1); |
| 208 | + |
| 209 | + let cell = TDD.rows.last.cells.first; |
| 210 | + expect(cell.active).to.be.false; |
| 211 | + expect(isVisibleGrid(cell.element)).to.be.true; |
| 212 | + |
| 213 | + await TDD.grid.navigateTo(0); |
| 214 | + |
| 215 | + cell = TDD.rows.first.cells.first; |
| 216 | + expect(cell.active).to.be.false; |
| 217 | + expect(isVisibleGrid(cell.element)).to.be.true; |
| 218 | + }); |
| 219 | + |
| 220 | + it('navigates to a specific row and column', async () => { |
| 221 | + await TDD.grid.navigateTo(testData.length - 2, 'name'); |
| 222 | + |
| 223 | + const cell = TDD.rows.get(-2).cells.get('name'); |
| 224 | + expect(cell.active).to.be.false; |
| 225 | + expect(isVisibleGrid(cell.element)).to.be.true; |
| 226 | + }); |
| 227 | + |
| 228 | + it('navigates to a cell within already scrolled-to row', async () => { |
| 229 | + await TDD.grid.navigateTo(testData.length - 1, 'id'); |
| 230 | + |
| 231 | + let cell = TDD.rows.last.cells.get('id'); |
| 232 | + expect(isVisibleGrid(cell.element)).to.be.true; |
| 233 | + |
| 234 | + // last cell |
| 235 | + cell = TDD.rows.last.cells.last; |
| 236 | + expect(isVisibleGrid(cell.element)).to.be.false; |
| 237 | + await TDD.grid.navigateTo(testData.length - 1, keys.at(-1)); |
| 238 | + |
| 239 | + expect(isVisibleGrid(cell.element)).to.be.true; |
| 240 | + }); |
| 241 | + |
| 242 | + it('navigates through all columns', async () => { |
| 243 | + for (const key of keys) { |
| 244 | + await TDD.grid.navigateTo(0, key); |
| 245 | + const cell = TDD.rows.first.cells.get(key); |
| 246 | + expect(isVisibleGrid(cell.element)).to.be.true; |
| 247 | + } |
| 248 | + }); |
| 249 | + |
| 250 | + it('updates active state when navigating', async () => { |
| 251 | + await TDD.grid.navigateTo(0, 'id', true); |
| 252 | + expect(TDD.rows.first.cells.get('id').active).to.be.true; |
| 253 | + |
| 254 | + await TDD.grid.navigateTo(1, 'name', true); |
| 255 | + expect(TDD.rows.first.cells.get('id').active).to.be.false; |
| 256 | + |
| 257 | + let cell = TDD.rows.get(1).cells.get('name'); |
| 258 | + expect(cell.active).to.be.true; |
| 259 | + expect(isVisibleGrid(cell.element)).to.be.true; |
| 260 | + |
| 261 | + // last cell |
| 262 | + await TDD.grid.navigateTo(testData.length - 1, keys.at(-1), true); |
| 263 | + |
| 264 | + cell = TDD.rows.last.cells.last; |
| 265 | + expect(cell.active).to.be.true; |
| 266 | + expect(isVisibleGrid(cell.element)).to.be.true; |
| 267 | + }); |
| 268 | + |
| 269 | + it('can navigate after click activation', async () => { |
| 270 | + await TDD.clickCell(TDD.rows.first.cells.first); |
| 271 | + expect(TDD.rows.first.cells.first.active).to.be.true; |
| 272 | + |
| 273 | + await TDD.grid.navigateTo(3, 'active', true); |
| 274 | + expect(TDD.rows.first.cells.first.active).to.be.false; |
| 275 | + |
| 276 | + const cell = TDD.rows.get(3).cells.get('active'); |
| 277 | + expect(cell.active).to.be.true; |
| 278 | + expect(isVisibleGrid(cell.element)).to.be.true; |
| 279 | + }); |
| 280 | + |
| 281 | + it('keyboard navigation works after navigateTo', async () => { |
| 282 | + await TDD.grid.navigateTo(2, 'id', true); |
| 283 | + expect(TDD.rows.get(2).cells.get('id').active).to.be.true; |
| 284 | + |
| 285 | + await TDD.fireNavigationEvent({ key: 'ArrowRight' }); |
| 286 | + expect(TDD.rows.get(2).cells.get('name').active).to.be.true; |
| 287 | + |
| 288 | + await TDD.fireNavigationEvent({ key: 'ArrowDown' }); |
| 289 | + const cell = TDD.rows.get(3).cells.get('name'); |
| 290 | + expect(cell.active).to.be.true; |
| 291 | + expect(isVisibleGrid(cell.element)).to.be.true; |
177 | 292 | }); |
178 | 293 | }); |
179 | 294 | }); |
0 commit comments