Skip to content

Commit 6ec07bc

Browse files
authored
feat(aria/grid): add test harnesses (#33081)
1 parent a7b2c51 commit 6ec07bc

8 files changed

Lines changed: 426 additions & 0 deletions

File tree

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
## API Report File for "@angular/aria_grid_testing"
2+
3+
> Do not edit this file. It is a report generated by [API Extractor](https://api-extractor.com/).
4+
5+
```ts
6+
7+
import { BaseHarnessFilters } from '@angular/cdk/testing';
8+
import { ComponentHarness } from '@angular/cdk/testing';
9+
import { ContentContainerComponentHarness } from '@angular/cdk/testing';
10+
import { HarnessPredicate } from '@angular/cdk/testing';
11+
12+
// @public
13+
export class GridCellHarness extends ContentContainerComponentHarness {
14+
blur(): Promise<void>;
15+
click(): Promise<void>;
16+
focus(): Promise<void>;
17+
getText(): Promise<string>;
18+
// (undocumented)
19+
static hostSelector: string;
20+
isDisabled(): Promise<boolean>;
21+
isSelected(): Promise<boolean>;
22+
static with(options?: GridCellHarnessFilters): HarnessPredicate<GridCellHarness>;
23+
}
24+
25+
// @public
26+
export interface GridCellHarnessFilters extends BaseHarnessFilters {
27+
disabled?: boolean;
28+
selected?: boolean;
29+
text?: string | RegExp;
30+
}
31+
32+
// @public
33+
export class GridHarness extends ComponentHarness {
34+
getCells(filters?: GridCellHarnessFilters): Promise<GridCellHarness[]>;
35+
getCellTextByIndex(): Promise<string[][]>;
36+
getRows(filters?: GridRowHarnessFilters): Promise<GridRowHarness[]>;
37+
// (undocumented)
38+
static hostSelector: string;
39+
isDisabled(): Promise<boolean>;
40+
isMultiSelectable(): Promise<boolean>;
41+
static with(options?: GridHarnessFilters): HarnessPredicate<GridHarness>;
42+
}
43+
44+
// @public
45+
export interface GridHarnessFilters extends BaseHarnessFilters {
46+
disabled?: boolean;
47+
}
48+
49+
// @public
50+
export class GridRowHarness extends ComponentHarness {
51+
getCells(filters?: GridCellHarnessFilters): Promise<GridCellHarness[]>;
52+
getCellTextByIndex(filters?: GridCellHarnessFilters): Promise<string[]>;
53+
// (undocumented)
54+
static hostSelector: string;
55+
static with(options?: GridRowHarnessFilters): HarnessPredicate<GridRowHarness>;
56+
}
57+
58+
// @public
59+
export interface GridRowHarnessFilters extends BaseHarnessFilters {
60+
}
61+
62+
// (No @packageDocumentation comment for this package)
63+
64+
```

src/aria/config.bzl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ ARIA_ENTRYPOINTS = [
44
"accordion/testing",
55
"combobox",
66
"grid",
7+
"grid/testing",
78
"listbox",
89
"listbox/testing",
910
"menu",

src/aria/grid/testing/BUILD.bazel

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
load("//tools:defaults.bzl", "ng_project", "ng_web_test_suite", "ts_project")
2+
3+
package(default_visibility = ["//visibility:public"])
4+
5+
ts_project(
6+
name = "testing",
7+
srcs = glob(
8+
["**/*.ts"],
9+
exclude = ["**/*.spec.ts"],
10+
),
11+
deps = [
12+
"//:node_modules/@angular/core",
13+
"//src/cdk/testing",
14+
],
15+
)
16+
17+
filegroup(
18+
name = "source-files",
19+
srcs = glob(["**/*.ts"]),
20+
)
21+
22+
ng_project(
23+
name = "unit_tests_lib",
24+
testonly = True,
25+
srcs = glob(["**/*.spec.ts"]),
26+
deps = [
27+
":testing",
28+
"//:node_modules/@angular/core",
29+
"//:node_modules/@angular/platform-browser",
30+
"//src/aria/grid",
31+
"//src/cdk/testing",
32+
"//src/cdk/testing/private",
33+
"//src/cdk/testing/testbed",
34+
],
35+
)
36+
37+
ng_web_test_suite(
38+
name = "unit_tests",
39+
deps = [
40+
":unit_tests_lib",
41+
],
42+
)
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
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 {BaseHarnessFilters} from '@angular/cdk/testing';
10+
11+
/** Filters for locating a `GridCellHarness`. */
12+
export interface GridCellHarnessFilters extends BaseHarnessFilters {
13+
/** Only find instances whose text matches the given value. */
14+
text?: string | RegExp;
15+
/** Only find instances whose selected state matches the given value. */
16+
selected?: boolean;
17+
/** Only find instances whose disabled state matches the given value. */
18+
disabled?: boolean;
19+
}
20+
21+
/** Filters for locating a `GridRowHarness`. */
22+
export interface GridRowHarnessFilters extends BaseHarnessFilters {
23+
// Add filters if needed, e.g., rowIndex
24+
}
25+
26+
/** Filters for locating a `GridHarness`. */
27+
export interface GridHarnessFilters extends BaseHarnessFilters {
28+
/** Only find instances whose disabled state matches the given value. */
29+
disabled?: boolean;
30+
}
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
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

Comments
 (0)