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

Conversation

silviuaavram
Copy link
Contributor

@silviuaavram silviuaavram commented Aug 5, 2020

Closes #32.

Replaces all occurrences of testIdAttribute: 'data-testid' with testIdAttribute: '<custom-attribute-name>' in the imported stringified dom-testing-library. Also keeps the original string just in case we need to perform subsequent changes.

Adds 2 unit tests to cover the changes.

Adds a new type that accepts only the supported parameter from configure.

export interface IConfigureOptions {
  testIdAttribute: string;
}

Copy link
Collaborator

@patrickhulce patrickhulce left a comment

Choose a reason for hiding this comment

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

clever hack for this @silviuaavram thanks for the contribution! the suggestions should help with the lint errors you're seeing, but the overall impl works for me :)

lib/index.ts Outdated
@@ -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 :)

lib/index.ts Outdated
@@ -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 {

lib/typedefs.ts Outdated
@@ -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 {

lib/index.ts Outdated
@@ -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'

lib/index.ts Outdated
@@ -130,6 +130,21 @@ export function wait(
return waitForExpect(callback, timeout, interval)
}

export function configure(newConfig: Partial<Config>) {
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

lib/index.ts Outdated
const { testIdAttribute } = newConfig;

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 :)

lib/index.ts Outdated
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?

@silviuaavram
Copy link
Contributor Author

Thank you for the review @patrickhulce and for getting back so soon, I will address the comments! I was not sure about the solutions so I wanted to create a PoC for it. All the suggestion make sense, thanks! Will ping you once I finish the improvements.

@silviuaavram
Copy link
Contributor Author

@patrickhulce it's done, let me know what you think. It's not ideal, as string replacing in this case is fragile, but it's how we can get it done with minimal changes + we are covered by unit tests, just in case we update dom testing library and there are changes to the strings we replace.

@patrickhulce
Copy link
Collaborator

SG @silviuaavram this looks great to me if you could just cleanup the lint errors! yarn test:lint --fix should cleanup some of them and look to my previous suggestions for how to fix the rest

Copy link
Collaborator

@patrickhulce patrickhulce left a comment

Choose a reason for hiding this comment

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

thanks @silviuaavram! 🎉

@patrickhulce patrickhulce merged commit 4e077c8 into testing-library:master Aug 7, 2020
@silviuaavram
Copy link
Contributor Author

This is cool, thank you so much for the feedback! Looking forward to seeing it released and used!

@patrickhulce
Copy link
Collaborator

published in 0.6.2 :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

How to override data-testid attribute?
2 participants