Skip to content

Commit 4ecf17d

Browse files
committed
fix(fixture): support function TextMatch argument in queries
1 parent 4b7eb6e commit 4ecf17d

File tree

2 files changed

+28
-0
lines changed

2 files changed

+28
-0
lines changed

lib/fixture/helpers.ts

+6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
const replacer = (_: string, value: unknown) => {
22
if (value instanceof RegExp) return `__REGEXP ${value.toString()}`
3+
if (typeof value === 'function') return `__FUNCTION ${value.toString()}`
34

45
return value
56
}
@@ -11,6 +12,11 @@ const reviver = (_: string, value: string) => {
1112
return new RegExp(match![1], match![2] || '')
1213
}
1314

15+
if (value.toString().includes('__FUNCTION ')) {
16+
// eslint-disable-next-line @typescript-eslint/no-implied-eval
17+
return new Function(`return (${value.split('__FUNCTION ')[1]}).apply(this, arguments)`)
18+
}
19+
1420
return value
1521
}
1622

test/fixture/locators.test.ts

+22
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,28 @@ test.describe('lib/fixture.ts (locators)', () => {
4040
expect(await locator.textContent()).toEqual('Hello h1')
4141
})
4242

43+
test('supports function style `TextMatch`', async ({screen}) => {
44+
const locator = screen.getByText(
45+
// eslint-disable-next-line prefer-arrow-callback, func-names
46+
function (content, element) {
47+
return content.startsWith('Hello') && element?.tagName.toLowerCase() === 'h3'
48+
},
49+
)
50+
51+
expect(locator).toBeTruthy()
52+
expect(await locator.textContent()).toEqual('Hello h3')
53+
})
54+
55+
test('supports arrow function style `TextMatch`', async ({screen}) => {
56+
const locator = screen.getByText(
57+
(content, element) =>
58+
content.startsWith('Hello') && element?.tagName.toLowerCase() === 'h3',
59+
)
60+
61+
expect(locator).toBeTruthy()
62+
expect(await locator.textContent()).toEqual('Hello h3')
63+
})
64+
4365
test('should handle the get* methods', async ({queries: {getByTestId}}) => {
4466
const locator = getByTestId('testid-text-input')
4567

0 commit comments

Comments
 (0)