Skip to content

Commit 2cf56dd

Browse files
silviuaavramjrolfs
authored andcommitted
feat: export configure function with data-testid override (#39)
1 parent 8359c7e commit 2cf56dd

File tree

4 files changed

+55
-6
lines changed

4 files changed

+55
-6
lines changed

lib/__tests__/fixtures/page.html

+3-3
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22
<html>
33

44
<body>
5-
<h1>Hello h1</h1>
6-
<h2>Hello h2</h2>
5+
<h1 data-new-id="first-level-header">Hello h1</h1>
6+
<h2 data-id="second-level-header">Hello h2</h2>
77
<img alt="Image A" src="">
88
<input type="text" data-testid="testid-text-input">
9-
<label for="label-text-input">Label A</label>
9+
<label for="label-text-input" data-testid="testid-label">Label A</label>
1010
<input id="label-text-input" type="text">
1111

1212
<div id="scoped">

lib/__tests__/index.ts

+29-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import * as path from 'path'
22
import * as playwright from 'playwright'
3-
import {getDocument, queries, getQueriesForElement, wait} from '..'
3+
import {getDocument, queries, getQueriesForElement, wait, configure} from '..'
44

55
describe('lib/index.ts', () => {
66
let browser: playwright.Browser
@@ -18,6 +18,30 @@ describe('lib/index.ts', () => {
1818
expect(await queries.getNodeText(element)).toEqual('Hello h1')
1919
})
2020

21+
it('should support custom data-testid attribute name', async () => {
22+
configure({testIdAttribute: 'data-id'})
23+
const document = await getDocument(page)
24+
const element = await queries.getByTestId(document, 'second-level-header')
25+
expect(await queries.getNodeText(element)).toEqual('Hello h2')
26+
})
27+
28+
it('should support subsequent changing the data-testid attribute names', async () => {
29+
configure({testIdAttribute: 'data-id'})
30+
configure({testIdAttribute: 'data-new-id'})
31+
const document = await getDocument(page)
32+
const element = await queries.getByTestId(document, 'first-level-header')
33+
expect(await queries.getNodeText(element)).toEqual('Hello h1')
34+
})
35+
36+
it('should keep the default data-testid when input passed is invalid', async () => {
37+
;[{}, undefined, null, {testIdAttribute: ''}].forEach(async options => {
38+
const document = await getDocument(page)
39+
configure(options as any)
40+
const element = await queries.getByTestId(document, 'testid-label')
41+
expect(await queries.getNodeText(element)).toEqual('Label A')
42+
})
43+
})
44+
2145
it('should support regex on raw queries object', async () => {
2246
const scope = await page.$('#scoped')
2347
if (!scope) throw new Error('Should have scope')
@@ -42,6 +66,10 @@ describe('lib/index.ts', () => {
4266
expect(await getByText('Loaded!')).toBeTruthy()
4367
}, 9000)
4468

69+
afterEach(() => {
70+
configure({testIdAttribute: 'data-testid'}) //cleanup
71+
})
72+
4573
afterAll(async () => {
4674
await browser.close()
4775
})

lib/index.ts

+19-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import * as path from 'path'
33
import {JSHandle, Page} from 'playwright'
44
import waitForExpect from 'wait-for-expect'
55

6-
import {ElementHandle, IQueryUtils, IScopedQueryUtils} from './typedefs'
6+
import {ElementHandle, IConfigureOptions, IQueryUtils, IScopedQueryUtils} from './typedefs'
77

88
const domLibraryAsString = readFileSync(
99
path.join(__dirname, '../dom-testing-library.js'),
@@ -17,7 +17,7 @@ function mapArgument(argument: any, index: number): any {
1717
: argument
1818
}
1919

20-
const delegateFnBodyToExecuteInPage = `
20+
const delegateFnBodyToExecuteInPageInitial = `
2121
${domLibraryAsString};
2222
2323
const mappedArgs = args.map(${mapArgument.toString()});
@@ -27,6 +27,8 @@ const delegateFnBodyToExecuteInPage = `
2727
return moduleWithFns[fnName](container, ...mappedArgs);
2828
`
2929

30+
let delegateFnBodyToExecuteInPage = delegateFnBodyToExecuteInPageInitial
31+
3032
type DOMReturnType = ElementHandle | ElementHandle[] | null
3133

3234
type ContextFn = (...args: any[]) => ElementHandle
@@ -132,6 +134,21 @@ export function wait(
132134
return waitForExpect(callback, timeout, interval)
133135
}
134136

137+
export function configure(options: Partial<IConfigureOptions>): void {
138+
if (!options) {
139+
return
140+
}
141+
142+
const { testIdAttribute } = options
143+
144+
if (testIdAttribute) {
145+
delegateFnBodyToExecuteInPage = delegateFnBodyToExecuteInPageInitial.replace(
146+
/testIdAttribute: (['|"])data-testid(['|"])/g,
147+
`testIdAttribute: $1${testIdAttribute}$2`,
148+
)
149+
}
150+
}
151+
135152
export function getQueriesForElement<T>(
136153
object: T,
137154
contextFn?: ContextFn,

lib/typedefs.ts

+4
Original file line numberDiff line numberDiff line change
@@ -63,3 +63,7 @@ export interface IQueryUtils extends IQueryMethods {
6363
getQueriesForElement(): IQueryUtils & IScopedQueryUtils
6464
getNodeText(el: Element): Promise<string>
6565
}
66+
67+
export interface IConfigureOptions {
68+
testIdAttribute: string
69+
}

0 commit comments

Comments
 (0)