Skip to content

Commit 016ebf2

Browse files
committed
fix: update ByRole options type definitions to include hidden etc
1 parent ee5fc9c commit 016ebf2

File tree

4 files changed

+49
-25
lines changed

4 files changed

+49
-25
lines changed

lib/typedefs.ts

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import {
22
Matcher,
3-
MatcherOptions as MatcherOptions_,
4-
SelectorMatcherOptions as SelectorMatcherOptions_,
3+
MatcherOptions as TestingLibraryMatcherOptions,
4+
SelectorMatcherOptions as TestingLibrarySelectorMatcherOptions,
5+
ByRoleOptions as TestingLibraryByRoleOptions,
56
waitForOptions,
67
} from '@testing-library/dom'
78
import {ElementHandle as PlaywrightElementHandle} from 'playwright'
@@ -10,16 +11,14 @@ export type ElementHandle = PlaywrightElementHandle<SVGElement | HTMLElement>
1011

1112
type Element = ElementHandle
1213

13-
type MatcherOptions = Omit<MatcherOptions_, 'normalizer'>
14-
type SelectorMatcherOptions = Omit<SelectorMatcherOptions_, 'normalizer'>
14+
type MatcherOptions = Omit<TestingLibraryMatcherOptions, 'normalizer'>
15+
type SelectorMatcherOptions = Omit<TestingLibrarySelectorMatcherOptions, 'normalizer'>
1516

16-
interface RoleMatcherOptions extends MatcherOptions {
17+
interface RoleMatcherOptions extends Omit<TestingLibraryByRoleOptions, 'name'> {
1718
name?: string | RegExp
1819
}
1920

20-
interface SelectorRoleMatcherOptions extends SelectorMatcherOptions {
21-
name?: string | RegExp
22-
}
21+
interface SelectorRoleMatcherOptions extends SelectorMatcherOptions, RoleMatcherOptions {}
2322

2423
interface QueryMethods {
2524
queryByPlaceholderText(el: Element, m: Matcher, opts?: MatcherOptions): Promise<Element | null>

test/fixture/fixture.test.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,14 @@ test.describe('lib/fixture.ts', () => {
116116
expect(text).toEqual('getByRole Test')
117117
})
118118

119+
test('supports `hidden` option when querying by role', async ({queries: {queryAllByRole}}) => {
120+
const elements = await queryAllByRole('img')
121+
const hiddenElements = await queryAllByRole('img', {hidden: true})
122+
123+
expect(elements).toHaveLength(1)
124+
expect(hiddenElements).toHaveLength(2)
125+
})
126+
119127
test('should get text content', async ({page}) => {
120128
const document = await getDocument(page)
121129
const $h3 = await document.$('#scoped h3')

test/fixtures/page.html

Lines changed: 25 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,30 @@
1-
<!doctype html>
1+
<!DOCTYPE html>
22
<html>
3+
<body>
4+
<h1 data-new-id="first-level-header">Hello h1</h1>
5+
<h2 data-id="second-level-header">Hello h2</h2>
6+
<img alt="Image A" src="" />
7+
<input type="text" data-testid="testid-text-input" />
8+
<label for="label-text-input" data-testid="testid-label">Label A</label>
9+
<input id="label-text-input" type="text" />
310

4-
<body>
5-
<h1 data-new-id="first-level-header">Hello h1</h1>
6-
<h2 data-id="second-level-header">Hello h2</h2>
7-
<img alt="Image A" src="">
8-
<input type="text" data-testid="testid-text-input">
9-
<label for="label-text-input" data-testid="testid-label">Label A</label>
10-
<input id="label-text-input" type="text">
11+
<button role="button">getByRole Test</button>
12+
<div id="scoped">
13+
<h3>Hello h3</h3>
14+
</div>
1115

12-
<button role="button">getByRole Test</button>
13-
<div id="scoped">
14-
<h3>Hello h3</h3>
15-
</div>
16-
17-
<table role="presentation">
18-
<td>Layout table</td>
19-
</table>
20-
</body>
16+
<table role="presentation">
17+
<td>Layout table</td>
18+
</table>
2119

20+
<svg
21+
aria-hidden="true"
22+
role="img"
23+
xmlns="http://www.w3.org/2000/svg"
24+
viewBox="0 0 512 512"
25+
>
26+
<path d="M502.3 190.8c3.9-3.1 9.7-.2 9.7 4.7V400c0 26.5-21.5 48-48 48H48c-26.5 0-48-21.5-48-48V195.6c0-5 5.7-7.8 9.7-4.7 22.4 17.4 52.1 39.5 154.1 113.6 21.1 15.4 56.7 47.8 92.2 47.6 35.7.3 72-32.8 92.3-47.6 102-74.1 131.6-96.3 154-113.7zM256 320c23.2.4 56.6-29.2 73.4-41.4 132.7-96.3 142.8-104.7 173.4-128.7 5.8-4.5 9.2-11.5 9.2-18.9v-19c0-26.5-21.5-48-48-48H48C21.5 64 0 85.5 0 112v19c0 7.4 3.4 14.3 9.2 18.9 30.6 23.9 40.7 32.4 173.4 128.7 16.8 12.2 50.2 41.8 73.4 41.4z" />
27+
</svg>
28+
</body>
29+
</body>
2230
</html>

test/standalone/index.test.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,15 @@ describe('lib/index.ts', () => {
8787
expect(text).toEqual('getByRole Test')
8888
})
8989

90+
test('supports `hidden` option when querying by role', async () => {
91+
const document = await getDocument(page)
92+
const elements = await queries.queryAllByRole(document, 'img')
93+
const hiddenElements = await queries.queryAllByRole(document, 'img', {hidden: true})
94+
95+
expect(elements).toHaveLength(1)
96+
expect(hiddenElements).toHaveLength(2)
97+
})
98+
9099
it('attaches `getNodeText`', async () => {
91100
const document = await getDocument(page)
92101
const element = await queries.getByText(document, 'Hello h1')

0 commit comments

Comments
 (0)