|
1 | 1 | import { expect } from '@vaadin/chai-plugins';
|
| 2 | +import { sendKeys } from '@vaadin/test-runner-commands'; |
2 | 3 | import { fixtureSync, nextFrame } from '@vaadin/testing-helpers';
|
3 | 4 | import '../all-imports.js';
|
4 | 5 | import { flushGrid } from './helpers.js';
|
@@ -218,6 +219,103 @@ describe('accessibility', () => {
|
218 | 219 | grid.collapseItem({ name: '0' });
|
219 | 220 | expect(grid.$.items.children[1].getAttribute('aria-level')).to.be.null;
|
220 | 221 | });
|
| 222 | + |
| 223 | + describe('toggle cell', () => { |
| 224 | + function setupTreeGrid(additionalColumns = []) { |
| 225 | + grid = fixtureSync(` |
| 226 | + <vaadin-grid item-id-path="name"> |
| 227 | + ${additionalColumns.join('')} |
| 228 | + <vaadin-grid-tree-column path="name" width="200px" header="Name" flex-shrink="0"></vaadin-grid-tree-column> |
| 229 | + <vaadin-grid-column path="value" header="Value"></vaadin-grid-column> |
| 230 | + </vaadin-grid> |
| 231 | + `); |
| 232 | + grid.dataProvider = ({ parentItem }, callback) => { |
| 233 | + const itemsOnEachLevel = 5; |
| 234 | + const items = [...Array(itemsOnEachLevel)].map((_, i) => { |
| 235 | + return { |
| 236 | + name: `${parentItem ? `${parentItem.name}-` : ''}${i}`, |
| 237 | + value: `value-${i}`, |
| 238 | + children: i % 2 === 0, |
| 239 | + id: `${parentItem ? `${parentItem.id}-` : ''}${i}`, |
| 240 | + }; |
| 241 | + }); |
| 242 | + callback(items, itemsOnEachLevel); |
| 243 | + }; |
| 244 | + flushGrid(grid); |
| 245 | + return grid; |
| 246 | + } |
| 247 | + |
| 248 | + function getToggleCell(row) { |
| 249 | + for (const cell of row.querySelectorAll('td')) { |
| 250 | + if (cell._content && cell._content.querySelector('vaadin-grid-tree-toggle')) { |
| 251 | + return cell; |
| 252 | + } |
| 253 | + } |
| 254 | + return null; |
| 255 | + } |
| 256 | + |
| 257 | + function getExpandableRows() { |
| 258 | + return Array.from(grid.$.items.children).filter((row) => row.getAttribute('aria-expanded') !== null); |
| 259 | + } |
| 260 | + |
| 261 | + describe('aria-expanded', () => { |
| 262 | + beforeEach(() => { |
| 263 | + setupTreeGrid(); |
| 264 | + }); |
| 265 | + |
| 266 | + it('should reflect aria-expanded state of rows', () => { |
| 267 | + Array.from(grid.$.items.children).forEach((row) => { |
| 268 | + const toggleCell = getToggleCell(row); |
| 269 | + expect(toggleCell).to.exist; |
| 270 | + expect(toggleCell.getAttribute('aria-expanded')).to.equal(row.getAttribute('aria-expanded')); |
| 271 | + }); |
| 272 | + }); |
| 273 | + |
| 274 | + it('should update cell aria-expanded when row expanded state changes', () => { |
| 275 | + const row = getExpandableRows()[0]; |
| 276 | + const toggleCell = getToggleCell(row); |
| 277 | + const itemName = row._item.name; |
| 278 | + |
| 279 | + expect(row.getAttribute('aria-expanded')).to.equal('false'); |
| 280 | + expect(toggleCell.getAttribute('aria-expanded')).to.equal('false'); |
| 281 | + |
| 282 | + grid.expandItem({ name: itemName }); |
| 283 | + expect(row.getAttribute('aria-expanded')).to.equal('true'); |
| 284 | + expect(toggleCell.getAttribute('aria-expanded')).to.equal('true'); |
| 285 | + |
| 286 | + grid.collapseItem({ name: itemName }); |
| 287 | + expect(row.getAttribute('aria-expanded')).to.equal('false'); |
| 288 | + expect(toggleCell.getAttribute('aria-expanded')).to.equal('false'); |
| 289 | + }); |
| 290 | + |
| 291 | + it('should handle expansion via keyboard interaction', async () => { |
| 292 | + const toggleCell = getToggleCell(getExpandableRows()[0]); |
| 293 | + toggleCell.focus(); |
| 294 | + await sendKeys({ press: 'Space' }); |
| 295 | + expect(toggleCell.getAttribute('aria-expanded')).to.equal('true'); |
| 296 | + await sendKeys({ press: 'Space' }); |
| 297 | + expect(toggleCell.getAttribute('aria-expanded')).to.equal('false'); |
| 298 | + }); |
| 299 | + }); |
| 300 | + |
| 301 | + describe('with selection column', () => { |
| 302 | + beforeEach(() => { |
| 303 | + setupTreeGrid(['<vaadin-grid-selection-column></vaadin-grid-selection-column>']); |
| 304 | + }); |
| 305 | + |
| 306 | + it('should update correct cell when selection column is present', () => { |
| 307 | + const expandableRows = getExpandableRows(); |
| 308 | + expect(expandableRows.length).to.be.above(0); |
| 309 | + expandableRows.forEach((row) => { |
| 310 | + const toggleCell = getToggleCell(row); |
| 311 | + expect(toggleCell).to.exist; |
| 312 | + expect(toggleCell).to.not.equal(row.querySelector('td')); |
| 313 | + expect(toggleCell._content.querySelector('vaadin-grid-tree-toggle')).to.exist; |
| 314 | + expect(toggleCell.getAttribute('aria-expanded')).to.equal(row.getAttribute('aria-expanded')); |
| 315 | + }); |
| 316 | + }); |
| 317 | + }); |
| 318 | + }); |
221 | 319 | });
|
222 | 320 |
|
223 | 321 | describe('details', () => {
|
|
0 commit comments