-
Notifications
You must be signed in to change notification settings - Fork 17
chore(E2E): add toBeExpanded() aria-expanded matcher #3437
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
Draft
Dave Shoup (shouples)
wants to merge
2
commits into
main
Choose a base branch
from
djs/issue-2566-aria-expanded-matcher
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
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,32 @@ | ||
| // Coverage for the shared `toBeExpanded()` matcher defined in tests/e2e/matchers.ts. It lives here, | ||
| // not in Electron E2E, because the rollwright functional harness is the only non-Electron way to run | ||
| // real-DOM Playwright assertions, keeping the `.not` and absent-attribute edge cases fast in CI. | ||
| import { expect } from "@playwright/test"; | ||
| // registers the `toBeExpanded` matcher on Playwright's shared `expect` | ||
| import "../../tests/e2e/matchers"; | ||
| import { test } from "./baseTest"; | ||
|
|
||
| // minimal tree items covering the three aria-expanded states the matcher distinguishes | ||
| const TREE_ITEMS = ` | ||
| <div role="treeitem" data-testid="expanded" aria-expanded="true">expanded</div> | ||
| <div role="treeitem" data-testid="collapsed" aria-expanded="false">collapsed</div> | ||
| <div role="treeitem" data-testid="leaf">leaf</div> | ||
| `; | ||
|
|
||
| test.use({ coverage: false }); | ||
|
|
||
| test.beforeEach(async ({ page }) => { | ||
| await page.setContent(TREE_ITEMS); | ||
| }); | ||
|
|
||
| test('toBeExpanded() passes for aria-expanded="true"', async ({ page }) => { | ||
| await expect(page.getByTestId("expanded")).toBeExpanded(); | ||
| }); | ||
|
|
||
| test('not.toBeExpanded() passes for aria-expanded="false"', async ({ page }) => { | ||
| await expect(page.getByTestId("collapsed")).not.toBeExpanded(); | ||
| }); | ||
|
|
||
| test("not.toBeExpanded() passes when aria-expanded is absent", async ({ page }) => { | ||
| await expect(page.getByTestId("leaf")).not.toBeExpanded(); | ||
| }); |
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
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,58 @@ | ||
| import type { ExpectMatcherState, Locator, MatcherReturnType } from "@playwright/test"; | ||
| import { expect } from "@playwright/test"; | ||
|
|
||
| /** Options accepted by {@linkcode toBeExpanded}, mirroring built-in locator assertions. */ | ||
| interface ToBeExpandedOptions { | ||
| timeout?: number; | ||
| } | ||
|
|
||
| /** | ||
| * Assert that a tree item or view header is expanded, i.e. carries `aria-expanded="true"`. | ||
| * | ||
| * Delegates to the auto-retrying `toHaveAttribute` assertion, so it preserves web-first | ||
| * auto-waiting and works with `.not`. | ||
| */ | ||
| async function toBeExpanded( | ||
| this: ExpectMatcherState, | ||
| locator: Locator, | ||
| options?: ToBeExpandedOptions, | ||
| ): Promise<MatcherReturnType> { | ||
| const assertionName = "toBeExpanded"; | ||
| let pass: boolean; | ||
| let matcherResult: { actual?: unknown } | undefined; | ||
| try { | ||
| // delegate through `.not` when negated so auto-waiting polls in the expected direction | ||
| const assertion = this.isNot ? expect(locator).not : expect(locator); | ||
| await assertion.toHaveAttribute("aria-expanded", "true", options); | ||
| pass = true; | ||
| } catch (error: unknown) { | ||
| matcherResult = (error as { matcherResult?: { actual?: unknown } }).matcherResult; | ||
| pass = false; | ||
| } | ||
| // `.not` was already applied above; flip back so the returned `pass` reflects the base assertion. | ||
| if (this.isNot) { | ||
| pass = !pass; | ||
| } | ||
|
|
||
| const message = () => { | ||
| const hint = this.utils.matcherHint(assertionName, undefined, "", { isNot: this.isNot }); | ||
| return ( | ||
| `${hint}\n\n` + | ||
| `Expected: ${this.isNot ? "not " : ""}aria-expanded="true"\n` + | ||
| `Received: aria-expanded=${this.utils.printReceived(matcherResult?.actual)}` | ||
| ); | ||
| }; | ||
|
|
||
| return { message, pass, name: assertionName, expected: "true", actual: matcherResult?.actual }; | ||
| } | ||
|
|
||
| expect.extend({ toBeExpanded }); | ||
|
|
||
| declare global { | ||
| namespace PlaywrightTest { | ||
| interface Matchers<R, T = unknown> { | ||
| /** Assert the locator carries `aria-expanded="true"` (i.e. the tree item/view is expanded). */ | ||
| toBeExpanded(options?: ToBeExpandedOptions): R; | ||
| } | ||
| } | ||
| } | ||
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
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
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
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
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
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
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
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.