Skip to content

Commit 1edcd57

Browse files
committed
fix: ensure queries provided by fixture work after navigation
1 parent 20fd768 commit 1edcd57

File tree

4 files changed

+84
-61
lines changed

4 files changed

+84
-61
lines changed

lib/common.ts

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
import {IQueryUtils} from './typedefs'
2+
3+
export const queryNames: Array<keyof IQueryUtils> = [
4+
'queryByPlaceholderText',
5+
'queryAllByPlaceholderText',
6+
'getByPlaceholderText',
7+
'getAllByPlaceholderText',
8+
'findByPlaceholderText',
9+
'findAllByPlaceholderText',
10+
11+
'queryByText',
12+
'queryAllByText',
13+
'getByText',
14+
'getAllByText',
15+
'findByText',
16+
'findAllByText',
17+
18+
'queryByLabelText',
19+
'queryAllByLabelText',
20+
'getByLabelText',
21+
'getAllByLabelText',
22+
'findByLabelText',
23+
'findAllByLabelText',
24+
25+
'queryByAltText',
26+
'queryAllByAltText',
27+
'getByAltText',
28+
'getAllByAltText',
29+
'findByAltText',
30+
'findAllByAltText',
31+
32+
'queryByTestId',
33+
'queryAllByTestId',
34+
'getByTestId',
35+
'getAllByTestId',
36+
'findByTestId',
37+
'findAllByTestId',
38+
39+
'queryByTitle',
40+
'queryAllByTitle',
41+
'getByTitle',
42+
'getAllByTitle',
43+
'findByTitle',
44+
'findAllByTitle',
45+
46+
'queryByRole',
47+
'queryAllByRole',
48+
'getByRole',
49+
'getAllByRole',
50+
'findByRole',
51+
'findAllByRole',
52+
53+
'queryByDisplayValue',
54+
'queryAllByDisplayValue',
55+
'getByDisplayValue',
56+
'getAllByDisplayValue',
57+
'findByDisplayValue',
58+
'findAllByDisplayValue',
59+
]

lib/fixture.ts

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,25 @@
11
import type {PlaywrightTestArgs, TestFixture} from '@playwright/test'
2+
3+
import {getDocument, queries as unscopedQueries} from '.'
4+
import {queryNames} from './common'
25
import type {IScopedQueryUtils as Queries} from './typedefs'
3-
import {getDocument, getQueriesForElement} from '.'
46

57
interface TestingLibraryFixtures {
68
queries: Queries
79
}
810

911
const fixture: TestFixture<Queries, PlaywrightTestArgs> = async ({page}, use) => {
10-
const document = await getDocument(page)
12+
const queries = {} as Queries
13+
14+
queryNames.forEach(name => {
15+
// @ts-expect-error
16+
queries[name] = async (...args) => {
17+
const document = await getDocument(page)
1118

12-
const queries = getQueriesForElement(document)
19+
// @ts-expect-error
20+
return unscopedQueries[name](document, ...args)
21+
}
22+
})
1323

1424
await use(queries)
1525
}

lib/index.ts

Lines changed: 2 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import {JSHandle, Page} from 'playwright'
44
import waitForExpect from 'wait-for-expect'
55

66
import {ElementHandle, IConfigureOptions, IQueryUtils, IScopedQueryUtils} from './typedefs'
7+
import {queryNames} from './common'
78

89
const domLibraryAsString = readFileSync(
910
path.join(__dirname, '../dom-testing-library.js'),
@@ -193,64 +194,7 @@ export function getQueriesForElement<T>(
193194
// eslint-disable-next-line no-param-reassign
194195
if (!contextFn) contextFn = () => o
195196

196-
const functionNames: Array<keyof IQueryUtils> = [
197-
'queryByPlaceholderText',
198-
'queryAllByPlaceholderText',
199-
'getByPlaceholderText',
200-
'getAllByPlaceholderText',
201-
'findByPlaceholderText',
202-
'findAllByPlaceholderText',
203-
204-
'queryByText',
205-
'queryAllByText',
206-
'getByText',
207-
'getAllByText',
208-
'findByText',
209-
'findAllByText',
210-
211-
'queryByLabelText',
212-
'queryAllByLabelText',
213-
'getByLabelText',
214-
'getAllByLabelText',
215-
'findByLabelText',
216-
'findAllByLabelText',
217-
218-
'queryByAltText',
219-
'queryAllByAltText',
220-
'getByAltText',
221-
'getAllByAltText',
222-
'findByAltText',
223-
'findAllByAltText',
224-
225-
'queryByTestId',
226-
'queryAllByTestId',
227-
'getByTestId',
228-
'getAllByTestId',
229-
'findByTestId',
230-
'findAllByTestId',
231-
232-
'queryByTitle',
233-
'queryAllByTitle',
234-
'getByTitle',
235-
'getAllByTitle',
236-
'findByTitle',
237-
'findAllByTitle',
238-
239-
'queryByRole',
240-
'queryAllByRole',
241-
'getByRole',
242-
'getAllByRole',
243-
'findByRole',
244-
'findAllByRole',
245-
246-
'queryByDisplayValue',
247-
'queryAllByDisplayValue',
248-
'getByDisplayValue',
249-
'getAllByDisplayValue',
250-
'findByDisplayValue',
251-
'findAllByDisplayValue',
252-
]
253-
functionNames.forEach(functionName => {
197+
queryNames.forEach(functionName => {
254198
o[functionName] = createDelegateFor(functionName, contextFn)
255199
})
256200

test/fixture/fixture.test.ts

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

47+
test('handles page navigations', async ({queries: {getByTestId}, page}) => {
48+
await page.goto(`file://${path.join(__dirname, '../fixtures/page.html')}`)
49+
50+
const element = await getByTestId('testid-text-input')
51+
52+
expect(await page.evaluate(el => el.outerHTML, element)).toMatch(
53+
`<input type="text" data-testid="testid-text-input">`,
54+
)
55+
})
56+
4757
test('should handle the get* method failures', async ({queries}) => {
4858
const {getByTitle} = queries
4959
// Use the scoped element so the pretty HTML snapshot is smaller

0 commit comments

Comments
 (0)