Skip to content

Commit 3c562f3

Browse files
committed
fix: implement missing getNodeText in fixture queries
1 parent 25ec7b3 commit 3c562f3

File tree

3 files changed

+20
-6
lines changed

3 files changed

+20
-6
lines changed

lib/fixture.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import type {PlaywrightTestArgs, TestFixture} from '@playwright/test'
22

33
import {getDocument, queries as unscopedQueries} from '.'
44
import {queryNames} from './common'
5-
import type {ScopedQueries as Queries} from './typedefs'
5+
import type {FixtureQueries as Queries} from './typedefs'
66

77
interface TestingLibraryFixtures {
88
queries: Queries
@@ -21,10 +21,13 @@ const fixture: TestFixture<Queries, PlaywrightTestArgs> = async ({page}, use) =>
2121
}
2222
})
2323

24+
queries.getNodeText = async e => unscopedQueries.getNodeText(e)
25+
2426
await use(queries)
2527
}
2628

2729
const fixtures = {queries: fixture}
2830

31+
export {configure} from '.'
2932
export {fixture, fixtures}
3033
export type {Queries, TestingLibraryFixtures}

lib/typedefs.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,14 @@ export type BoundFunction<T> = T extends (
171171
: T extends (a1: any, text: infer P, options: infer Q) => infer R
172172
? (text: P, options?: Q) => R
173173
: never
174+
174175
export type BoundFunctions<T> = {[P in keyof T]: BoundFunction<T[P]>}
176+
export type BoundQueryMethods = BoundFunctions<QueryMethods>
177+
178+
export interface FixtureQueries extends BoundFunctions<QueryMethods> {
179+
getQueriesForElement(): ScopedQueries
180+
getNodeText(el: Element): Promise<string>
181+
}
175182

176183
export interface ScopedQueries extends BoundFunctions<QueryMethods> {
177184
getQueriesForElement(): ScopedQueries

test/fixture/fixture.test.ts

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,14 +44,18 @@ test.describe('lib/fixture.ts', () => {
4444
)
4545
})
4646

47-
test('handles page navigations', async ({queries: {getByTestId}, page}) => {
47+
test('attaches `getNodeText`', async ({queries}) => {
48+
const element = await queries.getByText('Hello h1')
49+
50+
expect(await queries.getNodeText(element)).toEqual('Hello h1')
51+
})
52+
53+
test('handles page navigations', async ({queries: {getByText}, page}) => {
4854
await page.goto(`file://${path.join(__dirname, '../fixtures/page.html')}`)
4955

50-
const element = await getByTestId('testid-text-input')
56+
const element = await getByText('Hello h1')
5157

52-
expect(await page.evaluate(el => el.outerHTML, element)).toMatch(
53-
`<input type="text" data-testid="testid-text-input">`,
54-
)
58+
expect(await element.textContent()).toEqual('Hello h1')
5559
})
5660

5761
test('should handle the get* method failures', async ({queries}) => {

0 commit comments

Comments
 (0)