|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright Google LLC All Rights Reserved. |
| 4 | + * |
| 5 | + * Use of this source code is governed by an MIT-style license that can be |
| 6 | + * found in the LICENSE file at https://angular.dev/license |
| 7 | + */ |
| 8 | + |
| 9 | +import {Component} from '@angular/core'; |
| 10 | +import {ComponentFixture, TestBed} from '@angular/core/testing'; |
| 11 | +import {HarnessLoader} from '@angular/cdk/testing'; |
| 12 | +import {TestbedHarnessEnvironment} from '@angular/cdk/testing/testbed'; |
| 13 | + |
| 14 | +import {GridHarness, GridRowHarness, GridCellHarness} from './grid-harness'; |
| 15 | +import {Grid, GridRow, GridCell} from '../index'; |
| 16 | + |
| 17 | +describe('Grid Harness', () => { |
| 18 | + let fixture: ComponentFixture<GridHarnessTestComponent>; |
| 19 | + let loader: HarnessLoader; |
| 20 | + |
| 21 | + beforeEach(() => { |
| 22 | + TestBed.configureTestingModule({ |
| 23 | + imports: [GridHarnessTestComponent], |
| 24 | + }); |
| 25 | + fixture = TestBed.createComponent(GridHarnessTestComponent); |
| 26 | + fixture.detectChanges(); |
| 27 | + loader = TestbedHarnessEnvironment.loader(fixture); |
| 28 | + }); |
| 29 | + |
| 30 | + it('finds the grid harness', async () => { |
| 31 | + await expectAsync(loader.getHarness(GridHarness)).toBeResolved(); |
| 32 | + }); |
| 33 | + |
| 34 | + it('returns all rows scoped within the grid', async () => { |
| 35 | + const grid = await loader.getHarness(GridHarness); |
| 36 | + const rows = await grid.getRows(); |
| 37 | + expect(rows.length).toBe(2); |
| 38 | + }); |
| 39 | + |
| 40 | + it('returns all cells scoped within the grid', async () => { |
| 41 | + const grid = await loader.getHarness(GridHarness); |
| 42 | + const cells = await grid.getCells(); |
| 43 | + expect(cells.length).toBe(4); |
| 44 | + }); |
| 45 | + |
| 46 | + it('returns cells scoped within a row', async () => { |
| 47 | + const rows = await loader.getAllHarnesses(GridRowHarness); |
| 48 | + expect(rows.length).toBe(2); |
| 49 | + |
| 50 | + const cellsInRow0 = await rows[0].getCells(); |
| 51 | + expect(cellsInRow0.length).toBe(2); |
| 52 | + expect(await cellsInRow0[0].getText()).toBe('Cell 1.1'); |
| 53 | + |
| 54 | + const cellsInRow1 = await rows[1].getCells(); |
| 55 | + expect(cellsInRow1.length).toBe(2); |
| 56 | + expect(await cellsInRow1[0].getText()).toBe('Cell 2.1'); |
| 57 | + }); |
| 58 | + |
| 59 | + it('filters cells by exact text content', async () => { |
| 60 | + const grid = await loader.getHarness(GridHarness); |
| 61 | + const cells = await grid.getCells({text: 'Cell 1.1'}); |
| 62 | + expect(cells.length).toBe(1); |
| 63 | + }); |
| 64 | + |
| 65 | + it('reports the disabled state of the grid', async () => { |
| 66 | + const grid = await loader.getHarness(GridHarness); |
| 67 | + const isDisabled = await grid.isDisabled(); |
| 68 | + expect(isDisabled).toBeFalse(); |
| 69 | + }); |
| 70 | + |
| 71 | + it('reports the multi-selectable state of the grid', async () => { |
| 72 | + const grid = await loader.getHarness(GridHarness); |
| 73 | + const isMulti = await grid.isMultiSelectable(); |
| 74 | + expect(isMulti).toBeTrue(); |
| 75 | + }); |
| 76 | + |
| 77 | + it('reports the selected state of a cell', async () => { |
| 78 | + const cell = await loader.getHarness(GridCellHarness.with({text: 'Cell 1.1'})); |
| 79 | + expect(await cell.isSelected()).toBeTrue(); |
| 80 | + }); |
| 81 | + |
| 82 | + it('reports the disabled state of a cell', async () => { |
| 83 | + const cell = await loader.getHarness(GridCellHarness.with({text: 'Cell 2.2'})); |
| 84 | + expect(await cell.isDisabled()).toBeTrue(); |
| 85 | + }); |
| 86 | + |
| 87 | + it('gets the text of cells organized by rows', async () => { |
| 88 | + const grid = await loader.getHarness(GridHarness); |
| 89 | + const text = await grid.getCellTextByIndex(); |
| 90 | + expect(text).toEqual([ |
| 91 | + ['Cell 1.1', 'Cell 1.2'], |
| 92 | + ['Cell 2.1', 'Cell 2.2'], |
| 93 | + ]); |
| 94 | + }); |
| 95 | + |
| 96 | + it('gets the text of cells in a row', async () => { |
| 97 | + const rows = await loader.getAllHarnesses(GridRowHarness); |
| 98 | + expect(await rows[0].getCellTextByIndex()).toEqual(['Cell 1.1', 'Cell 1.2']); |
| 99 | + }); |
| 100 | +}); |
| 101 | + |
| 102 | +@Component({ |
| 103 | + imports: [Grid, GridRow, GridCell], |
| 104 | + template: ` |
| 105 | + <table ngGrid [multi]="true" [disabled]="false" [enableSelection]="true"> |
| 106 | + <tr ngGridRow> |
| 107 | + <td ngGridCell [selected]="true">Cell 1.1</td> |
| 108 | + <td ngGridCell>Cell 1.2</td> |
| 109 | + </tr> |
| 110 | + <tr ngGridRow> |
| 111 | + <td ngGridCell>Cell 2.1</td> |
| 112 | + <td ngGridCell [disabled]="true">Cell 2.2</td> |
| 113 | + </tr> |
| 114 | + </table> |
| 115 | + `, |
| 116 | +}) |
| 117 | +class GridHarnessTestComponent {} |
0 commit comments