Skip to content
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

feat(configureTestIdAttribute): add ability to override data-testid [generic config] #164

Merged

Conversation

RoystonS
Copy link
Contributor

N.B. This is one of two alternative PRs with slightly differing configuration APIs. This one contains a generic configuration API exposing one configuration function taking an object which could be extended for future use. That may desirable or not, hence the two PRs.

What:

Introduce an API to allow data-testid to be overridden without consumers needing
to construct and maintain a set of higher level decorator functions to override the various 'xxxByTestId' functions as exposed in various places.

Why:

The existing API, where data-testid cannot be changed at the low level, requires a significant amount of effort to customise the higher-level APIs. See issue #162 for more discussion.

How:

Ultimately we needed to parameterise what was previously some hardcoded strings in queries.js. I would have preferred to keep the changes local to queries.js, but we assume elsewhere that all exports from queries.js are queries, and I didn't want to alter that. So I added a simple config.js module to store the actual configuration, and queries.js references that and index.js exposes the tiny configuration API.

Checklist:

  • Documentation
  • Tests
  • Ready to be merged
  • Added myself to contributors table

Adds the configureTestIdAttribute function that enables the overriding of
data-testid without having to write and maintain higher-level decoration
to replace all the ByTestId queries in various places.
This was a typo whilst producing the two PRs.
kentcdodds
kentcdodds previously approved these changes Nov 28, 2018
Copy link
Member

@kentcdodds kentcdodds left a comment

Choose a reason for hiding this comment

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

This is actually really good. I'm in favor but would love to hear what other people have to say.

src/config.js Outdated
}
}
export function getTestIdAttribute() {
return config.testIdAttribute
Copy link
Collaborator

Choose a reason for hiding this comment

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

This looks good to me. My only nit is that I don't think there needs to be a special method to grab just this attribute; export the whole config object or a getConfig function instead.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Configure could also take an update function:

configure(currentConfig => { return {...newConfig} })

This would be nice if you want to read and extend subobjects or arrays (like the current set of queries) without importing the default value into the host file.

Copy link
Contributor Author

@RoystonS RoystonS Nov 29, 2018

Choose a reason for hiding this comment

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

This looks good to me. My only nit is that I don't think there needs to be a special method to grab just this attribute; export the whole config object or a getConfig function instead.

This comes from of some of my type-safety background. It's easy to get a silent failure from getConfig().mistypedConfigKeyName but getMistypedConfigKeyFunction() will always blow. But in this case the code will be protected by a unit test so that shouldn't be an issue. I'll tweak it to a getConfig function as that's more flexible than merely exposing the whole config.

@davidgilbertson
Copy link

This is great, nice one @RoystonS .

Just a note (to self, you or @kentcdodds), the "What if my project already uses data-test-id?" section in react-testing-library can be replaced with a link to this, once merged.

@RoystonS
Copy link
Contributor Author

This is great, nice one @RoystonS .

Just a note (to self, you or @kentcdodds), the "What if my project already uses data-test-id?" section in react-testing-library can be replaced with a link to this, once merged.

Absolutely. Very much looking forward to being able to do that!

This commit prepares the way for configure() to be used other than
just for `testIdAttribute`: it exposes the whole config internally
and upgrades the configure() function so that it can take a function
that.

I've introduced explicit tests for this more complex configuration API
and a new documentation section for it. The documentation for the
function-based configure() API could be improved with a real example
once there is a real key for which it would be useful.
@RoystonS
Copy link
Contributor Author

I've added another commit for @alexkrolick's extended configuration use case. That required a new set of tests and a new documentation section.

@RoystonS
Copy link
Contributor Author

I've made the usual sacrifices to eslint to make it shut up; this is now ready for review.

kentcdodds
kentcdodds previously approved these changes Nov 29, 2018
Copy link
Member

@kentcdodds kentcdodds left a comment

Choose a reason for hiding this comment

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

Looks super! Just a few things :)

README.md Outdated
Configuration options:

`testIdAttribute`
: The attribute used by `getByTestId` and related queries. Defaults to `data-testid`. See [`getByTestId`](#getbytestid).
Copy link
Member

Choose a reason for hiding this comment

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

Can this be placed on the same line so there's no space in the rendered output?

screen shot 2018-11-29 at 8 40 37 am

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hmm. Might be another prettier funsie. I'm sure I put them on the same line originally. I'll have a go...

})

describe('configure', () => {
test('merges a delta rather than replacing the whole config', () => {
Copy link
Member

Choose a reason for hiding this comment

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

Hmmm... I think the fact that you can add random properties to the configuration is an implementation detail and not necessarily a feature we care to support. By that I mean, if we change the implementation in the future to only allow you to configure keys of things we support then I don't want to have to bother with fixing these tests.

So if we could make only assertions for the features we care to support that would be great.

More specifically, this test is fine, so long as we change the assertion to:

expect(conf).toMatchObject({testIdAttribute: 'data-testid'})

The fact that newKey exists as well is a coincidence and not necessarily something we need to assert in our tests.

test('passes existing config out to config function', () => {
// Create a new config key based on the value of an existing one
configure(existingConfig => ({
newKey: `${existingConfig.testIdAttribute}-derived`,
Copy link
Member

Choose a reason for hiding this comment

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

Rather than newKey in this test, we could set this to the testIdAttribute and get the same value out of the test without asserting on a non-feature.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yup, will update tests so they don't create random properties. Would have been easier to test had there been two supported properties from the start.

Copy link
Member

@kentcdodds kentcdodds left a comment

Choose a reason for hiding this comment

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

Perfect 💯

@kentcdodds kentcdodds merged commit 879bf94 into testing-library:master Nov 29, 2018
@RoystonS RoystonS deleted the pr/configure-testid-generic-config branch November 29, 2018 17:08
@kentcdodds
Copy link
Member

🎉 This PR is included in version 3.13.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

@RoystonS
Copy link
Contributor Author

Very much appreciated!

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

Successfully merging this pull request may close these issues.

5 participants