-
Notifications
You must be signed in to change notification settings - Fork 472
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
feat(configureTestIdAttribute): add ability to override data-testid [generic config] #164
Conversation
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.
There was a problem hiding this 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 |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
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 |
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.
I've added another commit for @alexkrolick's extended configuration use case. That required a new set of tests and a new documentation section. |
I've made the usual sacrifices to eslint to make it shut up; this is now ready for review. |
There was a problem hiding this 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). |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
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', () => { |
There was a problem hiding this comment.
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.
src/__tests__/config.js
Outdated
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`, |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Perfect 💯
🎉 This PR is included in version 3.13.0 🎉 The release is available on:
Your semantic-release bot 📦🚀 |
Very much appreciated! |
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 needingto 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 toqueries.js
, but we assume elsewhere that all exports fromqueries.js
are queries, and I didn't want to alter that. So I added a simpleconfig.js
module to store the actual configuration, andqueries.js
references that andindex.js
exposes the tiny configuration API.Checklist: