Skip to content

Commit

Permalink
fix: implement missing getNodeText in fixture queries
Browse files Browse the repository at this point in the history
  • Loading branch information
jrolfs committed Feb 19, 2022
1 parent 25ec7b3 commit 3c562f3
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 6 deletions.
5 changes: 4 additions & 1 deletion lib/fixture.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type {PlaywrightTestArgs, TestFixture} from '@playwright/test'

import {getDocument, queries as unscopedQueries} from '.'
import {queryNames} from './common'
import type {ScopedQueries as Queries} from './typedefs'
import type {FixtureQueries as Queries} from './typedefs'

interface TestingLibraryFixtures {
queries: Queries
Expand All @@ -21,10 +21,13 @@ const fixture: TestFixture<Queries, PlaywrightTestArgs> = async ({page}, use) =>
}
})

queries.getNodeText = async e => unscopedQueries.getNodeText(e)

await use(queries)
}

const fixtures = {queries: fixture}

export {configure} from '.'
export {fixture, fixtures}
export type {Queries, TestingLibraryFixtures}
7 changes: 7 additions & 0 deletions lib/typedefs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,14 @@ export type BoundFunction<T> = T extends (
: T extends (a1: any, text: infer P, options: infer Q) => infer R
? (text: P, options?: Q) => R
: never

export type BoundFunctions<T> = {[P in keyof T]: BoundFunction<T[P]>}
export type BoundQueryMethods = BoundFunctions<QueryMethods>

export interface FixtureQueries extends BoundFunctions<QueryMethods> {
getQueriesForElement(): ScopedQueries
getNodeText(el: Element): Promise<string>
}

export interface ScopedQueries extends BoundFunctions<QueryMethods> {
getQueriesForElement(): ScopedQueries
Expand Down
14 changes: 9 additions & 5 deletions test/fixture/fixture.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,18 @@ test.describe('lib/fixture.ts', () => {
)
})

test('handles page navigations', async ({queries: {getByTestId}, page}) => {
test('attaches `getNodeText`', async ({queries}) => {
const element = await queries.getByText('Hello h1')

expect(await queries.getNodeText(element)).toEqual('Hello h1')
})

test('handles page navigations', async ({queries: {getByText}, page}) => {
await page.goto(`file://${path.join(__dirname, '../fixtures/page.html')}`)

const element = await getByTestId('testid-text-input')
const element = await getByText('Hello h1')

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

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

0 comments on commit 3c562f3

Please sign in to comment.