-
Notifications
You must be signed in to change notification settings - Fork 6.8k
feat(aria/listbox): introduce listbox harness #33064
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| ## API Report File for "@angular/aria_listbox_testing" | ||
|
|
||
| > Do not edit this file. It is a report generated by [API Extractor](https://api-extractor.com/). | ||
|
|
||
| ```ts | ||
|
|
||
| import { BaseHarnessFilters } from '@angular/cdk/testing'; | ||
| import { ComponentHarness } from '@angular/cdk/testing'; | ||
| import { HarnessPredicate } from '@angular/cdk/testing'; | ||
|
|
||
| // @public (undocumented) | ||
| export class ListboxHarness extends ComponentHarness { | ||
| blur(): Promise<void>; | ||
| // (undocumented) | ||
| focus(): Promise<void>; | ||
| // (undocumented) | ||
| getOptions(filters?: ListboxOptionHarnessFilters): Promise<ListboxOptionHarness[]>; | ||
| // (undocumented) | ||
| getOrientation(): Promise<'vertical' | 'horizontal'>; | ||
| // (undocumented) | ||
| static hostSelector: string; | ||
| // (undocumented) | ||
| isDisabled(): Promise<boolean>; | ||
| // (undocumented) | ||
| isMulti(): Promise<boolean>; | ||
| // (undocumented) | ||
| static with(options?: ListboxHarnessFilters): HarnessPredicate<ListboxHarness>; | ||
| } | ||
|
|
||
| // @public (undocumented) | ||
| export interface ListboxHarnessFilters extends BaseHarnessFilters { | ||
| // (undocumented) | ||
| disabled?: boolean; | ||
| } | ||
|
|
||
| // @public (undocumented) | ||
| export class ListboxOptionHarness extends ComponentHarness { | ||
| // (undocumented) | ||
| click(): Promise<void>; | ||
| // (undocumented) | ||
| getText(): Promise<string>; | ||
| // (undocumented) | ||
| static hostSelector: string; | ||
| // (undocumented) | ||
| isDisabled(): Promise<boolean>; | ||
| // (undocumented) | ||
| isSelected(): Promise<boolean>; | ||
| // (undocumented) | ||
| static with(options?: ListboxOptionHarnessFilters): HarnessPredicate<ListboxOptionHarness>; | ||
| } | ||
|
|
||
| // @public | ||
| export interface ListboxOptionHarnessFilters extends BaseHarnessFilters { | ||
| disabled?: boolean; | ||
| selected?: boolean; | ||
| text?: string | RegExp; | ||
| } | ||
|
|
||
| // @public | ||
| export enum ListboxSection { | ||
| // (undocumented) | ||
| OPTION = "[ngOption]" | ||
| } | ||
|
|
||
| // (No @packageDocumentation comment for this package) | ||
|
|
||
| ``` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,6 +4,7 @@ ARIA_ENTRYPOINTS = [ | |
| "combobox", | ||
| "grid", | ||
| "listbox", | ||
| "listbox/testing", | ||
| "menu", | ||
| "tabs", | ||
| "toolbar", | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| load("//tools:defaults.bzl", "ng_project", "ng_web_test_suite", "ts_project") | ||
|
|
||
| package(default_visibility = ["//visibility:public"]) | ||
|
|
||
| ts_project( | ||
| name = "testing", | ||
| srcs = glob( | ||
| ["**/*.ts"], | ||
| exclude = ["**/*.spec.ts"], | ||
| ), | ||
| deps = [ | ||
| "//:node_modules/@angular/core", | ||
| "//src/cdk/testing", | ||
| ], | ||
| ) | ||
|
|
||
| filegroup( | ||
| name = "source-files", | ||
| srcs = glob(["**/*.ts"]), | ||
| ) | ||
|
|
||
| ng_project( | ||
| name = "unit_tests_lib", | ||
| testonly = True, | ||
| srcs = glob(["**/*.spec.ts"]), | ||
| deps = [ | ||
| ":testing", | ||
| "//:node_modules/@angular/core", | ||
| "//:node_modules/@angular/platform-browser", | ||
| "//src/aria/listbox", | ||
| "//src/cdk/testing", | ||
| "//src/cdk/testing/private", | ||
| "//src/cdk/testing/testbed", | ||
| ], | ||
| ) | ||
|
|
||
| ng_web_test_suite( | ||
| name = "unit_tests", | ||
| deps = [ | ||
| ":unit_tests_lib", | ||
| ], | ||
| ) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| /** | ||
| * @license | ||
| * Copyright Google LLC All Rights Reserved. | ||
| * | ||
| * Use of this source code is governed by an MIT-style license that can be | ||
| * found in the LICENSE file at https://angular.dev/license | ||
| */ | ||
|
|
||
| export * from './public-api'; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,93 @@ | ||
| /** | ||
| * @license | ||
| * Copyright Google LLC All Rights Reserved. | ||
| * | ||
| * Use of this source code is governed by an MIT-style license that can be | ||
| * found in the LICENSE file at https://angular.dev/license | ||
| */ | ||
|
|
||
| import {Component} from '@angular/core'; | ||
| import {TestBed} from '@angular/core/testing'; | ||
| import {TestbedHarnessEnvironment} from '@angular/cdk/testing/testbed'; | ||
|
|
||
| import {ListboxHarness, ListboxOptionHarness} from './listbox-harness'; | ||
| import {Listbox, Option} from '../index'; | ||
|
|
||
| describe('Listbox Harness', () => { | ||
| let fixture: any; | ||
| let loader: any; | ||
|
|
||
| @Component({ | ||
| imports: [Listbox, Option], | ||
| template: ` | ||
| <ul ngListbox [disabled]="false" [multi]="true" orientation="horizontal"> | ||
| <li ngOption [value]="1" label="Apple" aria-selected="true">Apple</li> | ||
| <li ngOption [value]="2" label="Banana">Banana</li> | ||
| <div class="test-item">Inside Listbox</div> | ||
| </ul> | ||
| `, | ||
| }) | ||
| class ListboxHarnessTestComponent {} | ||
|
|
||
| beforeEach(() => { | ||
| TestBed.configureTestingModule({ | ||
| imports: [ListboxHarnessTestComponent], | ||
| }); | ||
| fixture = TestBed.createComponent(ListboxHarnessTestComponent); | ||
| fixture.detectChanges(); | ||
| loader = TestbedHarnessEnvironment.loader(fixture); | ||
| }); | ||
|
|
||
| it('finds the listbox container harness', async () => { | ||
| const listbox = await loader.getHarness(ListboxHarness); | ||
| expect(listbox).toBeTruthy(); | ||
| }); | ||
|
|
||
| it('returns all options scoped within the listbox', async () => { | ||
| const listbox = await loader.getHarness(ListboxHarness); | ||
|
|
||
| const options = await listbox.getOptions(); | ||
|
|
||
| expect(options.length).toBe(2); | ||
| }); | ||
|
|
||
| it('filters options by exact text content', async () => { | ||
| const listbox = await loader.getHarness(ListboxHarness); | ||
|
|
||
| const options = await listbox.getOptions({text: 'Apple'}); | ||
|
|
||
| expect(options.length).toBe(1); | ||
| }); | ||
|
|
||
| it('reports the disabled state of the listbox', async () => { | ||
| const listbox = await loader.getHarness(ListboxHarness); | ||
|
|
||
| const isDisabled = await listbox.isDisabled(); | ||
|
|
||
| expect(isDisabled).toBeFalse(); | ||
| }); | ||
|
|
||
| it('reports the multi-selectable state of the listbox', async () => { | ||
| const listbox = await loader.getHarness(ListboxHarness); | ||
|
|
||
| const isMulti = await listbox.isMulti(); | ||
|
|
||
| expect(isMulti).toBeTrue(); | ||
| }); | ||
|
|
||
| it('reports the orientation of the listbox', async () => { | ||
| const listbox = await loader.getHarness(ListboxHarness); | ||
|
|
||
| const orientation = await listbox.getOrientation(); | ||
|
|
||
| expect(orientation).toBe('horizontal'); | ||
| }); | ||
|
|
||
| it('clicks an option inside the listbox', async () => { | ||
| const option = await loader.getHarness(ListboxOptionHarness.with({text: 'Apple'})); | ||
|
|
||
| await option.click(); | ||
|
|
||
| expect(await option.isSelected()).toBeTrue(); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,113 @@ | ||
| /** | ||
| * @license | ||
| * Copyright Google LLC All Rights Reserved. | ||
| * | ||
| * Use of this source code is governed by an MIT-style license that can be | ||
| * found in the LICENSE file at https://angular.dev/license | ||
| */ | ||
|
|
||
| import {ComponentHarness, HarnessPredicate, BaseHarnessFilters} from '@angular/cdk/testing'; | ||
|
|
||
| /** Selectors for the listbox sections. */ | ||
| export enum ListboxSection { | ||
| OPTION = '[ngOption]', | ||
| } | ||
|
|
||
| /** Filters for locating a `ListboxOptionHarness`. */ | ||
| export interface ListboxOptionHarnessFilters extends BaseHarnessFilters { | ||
| /** Only find instances whose text matches the given value. */ | ||
| text?: string | RegExp; | ||
| /** Only find instances whose selected state matches the given value. */ | ||
| selected?: boolean; | ||
| /** Only find instances whose disabled state matches the given value. */ | ||
| disabled?: boolean; | ||
| } | ||
|
|
||
| export class ListboxOptionHarness extends ComponentHarness { | ||
| static hostSelector = '[ngOption]'; | ||
|
|
||
| static with(options: ListboxOptionHarnessFilters = {}): HarnessPredicate<ListboxOptionHarness> { | ||
| return new HarnessPredicate(ListboxOptionHarness, options) | ||
| .addOption('text', options.text, (harness, text) => | ||
| HarnessPredicate.stringMatches(harness.getText(), text), | ||
| ) | ||
| .addOption( | ||
| 'selected', | ||
| options.selected, | ||
| async (harness, selected) => (await harness.isSelected()) === selected, | ||
| ) | ||
| .addOption( | ||
| 'disabled', | ||
| options.disabled, | ||
| async (harness, disabled) => (await harness.isDisabled()) === disabled, | ||
| ); | ||
| } | ||
|
|
||
| async isSelected(): Promise<boolean> { | ||
| const host = await this.host(); | ||
| return (await host.getAttribute('aria-selected')) === 'true'; | ||
| } | ||
|
|
||
| async isDisabled(): Promise<boolean> { | ||
| const host = await this.host(); | ||
| return ( | ||
| (await host.getAttribute('aria-disabled')) === 'true' || | ||
| (await host.getProperty('disabled')) === true | ||
| ); | ||
| } | ||
|
|
||
| async getText(): Promise<string> { | ||
| const host = await this.host(); | ||
| return host.text(); | ||
| } | ||
|
|
||
| async click(): Promise<void> { | ||
| const host = await this.host(); | ||
| return host.click(); | ||
| } | ||
| } | ||
|
|
||
| export interface ListboxHarnessFilters extends BaseHarnessFilters { | ||
| disabled?: boolean; | ||
| } | ||
|
|
||
| export class ListboxHarness extends ComponentHarness { | ||
| static hostSelector = '[ngListbox]'; | ||
|
|
||
| static with(options: ListboxHarnessFilters = {}): HarnessPredicate<ListboxHarness> { | ||
| return new HarnessPredicate(ListboxHarness, options).addOption( | ||
| 'disabled', | ||
| options.disabled, | ||
| async (harness, disabled) => (await harness.isDisabled()) === disabled, | ||
| ); | ||
| } | ||
|
|
||
| async getOrientation(): Promise<'vertical' | 'horizontal'> { | ||
| const host = await this.host(); | ||
| const orientation = await host.getAttribute('aria-orientation'); | ||
| return orientation === 'horizontal' ? 'horizontal' : 'vertical'; | ||
| } | ||
|
|
||
| async isMulti(): Promise<boolean> { | ||
| const host = await this.host(); | ||
| return (await host.getAttribute('aria-multiselectable')) === 'true'; | ||
| } | ||
|
|
||
| async isDisabled(): Promise<boolean> { | ||
| const host = await this.host(); | ||
| return (await host.getAttribute('aria-disabled')) === 'true'; | ||
| } | ||
|
|
||
| async getOptions(filters: ListboxOptionHarnessFilters = {}): Promise<ListboxOptionHarness[]> { | ||
| return this.locatorForAll(ListboxOptionHarness.with(filters))(); | ||
| } | ||
|
|
||
| async focus(): Promise<void> { | ||
| await (await this.host()).focus(); | ||
| } | ||
|
|
||
| /** Blurs the listbox container. */ | ||
| async blur(): Promise<void> { | ||
| await (await this.host()).blur(); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| /** | ||
| * @license | ||
| * Copyright Google LLC All Rights Reserved. | ||
| * | ||
| * Use of this source code is governed by an MIT-style license that can be | ||
| * found in the LICENSE file at https://angular.dev/license | ||
| */ | ||
|
|
||
| export * from './listbox-harness'; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.