Skip to content

Commit 4e077c8

Browse files
authored
feat: export configure function with data-testid override (#39)
1 parent 950c30f commit 4e077c8

File tree

4 files changed

+55
-6
lines changed

4 files changed

+55
-6
lines changed

lib/index.ts

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

6-
import {IQueryUtils, IScopedQueryUtils} from './typedefs'
6+
import {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
@@ -130,6 +132,21 @@ export function wait(
130132
return waitForExpect(callback, timeout, interval)
131133
}
132134

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

lib/typedefs.ts

+4
Original file line numberDiff line numberDiff line change
@@ -58,3 +58,7 @@ export interface IQueryUtils extends IQueryMethods {
5858
getQueriesForElement(): IQueryUtils & IScopedQueryUtils
5959
getNodeText(el: Element): Promise<string>
6060
}
61+
62+
export interface IConfigureOptions {
63+
testIdAttribute: string
64+
}

test/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">

test/index.test.ts

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

55
describe('lib/index.ts', () => {
66
let browser: puppeteer.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')
@@ -41,6 +65,10 @@ describe('lib/index.ts', () => {
4165
expect(await getByText('Loaded!')).toBeTruthy()
4266
}, 9000)
4367

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

0 commit comments

Comments
 (0)