Skip to content

feat: export configure function with data-testid override #39

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 17 additions & 2 deletions lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import * as path from 'path'
import {ElementHandle, EvaluateFn, JSHandle, Page} from 'puppeteer'
import waitForExpect from 'wait-for-expect'

import {IQueryUtils, IScopedQueryUtils} from './typedefs'
import {IQueryUtils, IScopedQueryUtils, Config} from './typedefs'
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
import {IQueryUtils, IScopedQueryUtils, Config} from './typedefs'
import {IConfigureOptions, IQueryUtils, IScopedQueryUtils} from './typedefs'


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

const delegateFnBodyToExecuteInPage = `
let delegateFnBodyToExecuteInPage = `
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let's save the original as const delegateFnBody so we can configure more than once :)

${domLibraryAsString};

const mappedArgs = args.map(${mapArgument.toString()});
Expand Down Expand Up @@ -130,6 +130,21 @@ export function wait(
return waitForExpect(callback, timeout, interval)
}

export function configure(newConfig: Partial<Config>) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
export function configure(newConfig: Partial<Config>) {
export function configure(options: Partial<IConfigureOptions>): void {

const { testIdAttribute } = newConfig;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
const { testIdAttribute } = newConfig;
const {testIdAttribute} = options


if (
testIdAttribute &&
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
testIdAttribute &&

this one is redundant with the other checks :)

typeof testIdAttribute === 'string' &&
testIdAttribute !== ''
) {
delegateFnBodyToExecuteInPage = delegateFnBodyToExecuteInPage.replace(
`testIdAttribute: 'data-testid'`,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

does this pattern only ever occur once or do we need a global replace?

`testIdAttribute: '${newConfig.testIdAttribute}'`
)
}
}

export function getQueriesForElement<T>(
object: T,
contextFn?: ContextFn,
Expand Down
4 changes: 4 additions & 0 deletions lib/typedefs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,7 @@ export interface IQueryUtils extends IQueryMethods {
getQueriesForElement(): IQueryUtils & IScopedQueryUtils
getNodeText(el: Element): Promise<string>
}

export interface Config {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
export interface Config {
export interface IConfigureOptions {

testIdAttribute: string;
}
2 changes: 1 addition & 1 deletion test/fixtures/page.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

<body>
<h1>Hello h1</h1>
<h2>Hello h2</h2>
<h2 data-id="my-header">Hello h2</h2>
<img alt="Image A" src="">
<input type="text" data-testid="testid-text-input">
<label for="label-text-input">Label A</label>
Expand Down
9 changes: 8 additions & 1 deletion test/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as path from 'path'
import * as puppeteer from 'puppeteer'
import {getDocument, queries, getQueriesForElement, wait} from '../lib'
import {getDocument, queries, getQueriesForElement, wait, configure} from '../lib'

describe('lib/index.ts', () => {
let browser: puppeteer.Browser
Expand All @@ -17,6 +17,13 @@ describe('lib/index.ts', () => {
const element = await queries.getByText(document, 'Hello h1')
expect(await queries.getNodeText(element)).toEqual('Hello h1')
})

it('should support custom data-testid names', async () => {
configure({testIdAttribute: 'data-id'})
const document = await getDocument(page)
const element = await queries.getByTestId(document, 'my-header')
expect(await queries.getNodeText(element)).toEqual('Hello h2')
})

it('should support regex on raw queries object', async () => {
const scope = await page.$('#scoped')
Expand Down