Skip to content

Commit

Permalink
fix(fixture): support function TextMatch argument in queries
Browse files Browse the repository at this point in the history
  • Loading branch information
jrolfs committed Sep 22, 2022
1 parent 4b7eb6e commit 4ecf17d
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
6 changes: 6 additions & 0 deletions lib/fixture/helpers.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const replacer = (_: string, value: unknown) => {
if (value instanceof RegExp) return `__REGEXP ${value.toString()}`
if (typeof value === 'function') return `__FUNCTION ${value.toString()}`

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

if (value.toString().includes('__FUNCTION ')) {
// eslint-disable-next-line @typescript-eslint/no-implied-eval
return new Function(`return (${value.split('__FUNCTION ')[1]}).apply(this, arguments)`)
}

return value
}

Expand Down
22 changes: 22 additions & 0 deletions test/fixture/locators.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,28 @@ test.describe('lib/fixture.ts (locators)', () => {
expect(await locator.textContent()).toEqual('Hello h1')
})

test('supports function style `TextMatch`', async ({screen}) => {
const locator = screen.getByText(
// eslint-disable-next-line prefer-arrow-callback, func-names
function (content, element) {
return content.startsWith('Hello') && element?.tagName.toLowerCase() === 'h3'
},
)

expect(locator).toBeTruthy()
expect(await locator.textContent()).toEqual('Hello h3')
})

test('supports arrow function style `TextMatch`', async ({screen}) => {
const locator = screen.getByText(
(content, element) =>
content.startsWith('Hello') && element?.tagName.toLowerCase() === 'h3',
)

expect(locator).toBeTruthy()
expect(await locator.textContent()).toEqual('Hello h3')
})

test('should handle the get* methods', async ({queries: {getByTestId}}) => {
const locator = getByTestId('testid-text-input')

Expand Down

0 comments on commit 4ecf17d

Please sign in to comment.