-
Notifications
You must be signed in to change notification settings - Fork 352
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
#336/Fix broken unit tests caused by react-children-utilities dependency. #352
#336/Fix broken unit tests caused by react-children-utilities dependency. #352
Conversation
This removes the react-children-utilities dependency, which fixes the tests without requiring module transpilation. The FormHelpButton uses the label field to label the help box, so if the label is a React Node then the text is difficult to access programmatically. The previous implementation used a 3rd party dependency, react-children-utilities, to extract the text. But the dependency breaks the unit tests because the module format is not supported by Jest. This commit creates a HelpProps interface to communicate help label and text to the FormHelpButton. This is more explicit, and avoids the need for another dependency.
This pull request is being automatically deployed with ZEIT Now (learn more). 🔍 Inspect: https://zeit.co/covid19-scenarios/covid19-scenarios/6geanctzf |
@@ -82,7 +82,6 @@ | |||
"prop-types": "15.7.2", | |||
"raf": "3.4.1", | |||
"react": "16.13.0", | |||
"react-children-utilities": "2.0.12", |
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.
Remove dependency.
export interface HelpProps { | ||
label: string | ||
text: string | ||
} |
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 interface is passed into the 'help' attribute for elements that need a help button. Having label
in this interface removes the need to try and extract label text from the the 'label' attribute on the element.
export function help(label:string = "", text:string = "") : HelpProps { | ||
return { label, text } | ||
} | ||
|
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 a helper function to create the HelpProps object.
{label && <h4>{onlyText(label)}</h4>} | ||
<p>{help}</p> | ||
<h4>{help.label}</h4> |
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.
Now we no longer need to invoke onlyText
. We just insert the contents of the 'label' field from the HelpProps object.
@@ -106,7 +107,7 @@ function ResultsCardFunction({ | |||
{t('Results')} | |||
</h2> | |||
} | |||
help={t('This section contains simulation results')} | |||
help={help(t('Results'), t('This section contains simulation results'))} |
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 pattern is repeated throughout all elements that have a 'help' attribute. The attribute receives a HelpParams object, created by the help(title, text)
helper.
label={<h2 className="p-0 m-0 d-inline text-truncate">{t('Scenario')}</h2>} | ||
help={t('Combination of population, epidemiology, and mitigation scenarios')} | ||
help={help(t('Scenario'), t('Combination of population, epidemiology, and mitigation scenarios'))} |
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.
Here is an example of a label that renders a React node. Note that we pass both a label and help text to the help() method, which generates a HelpParams object. The label is duplicated in both the label= attribute and the help() function.
@@ -12,13 +10,21 @@ function safeId(id: string) { | |||
return id.replace('.', '-') | |||
} | |||
|
|||
export interface HelpProps { | |||
label: string | |||
text: string |
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.
Hi @abrie could text
be changed to content
and be string | React.ReactNode
? When I changed this last, @ivan-aksamentov mentioned that he wanted to allow the content of help possibly be more than plain text.
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.
@gj262 Yeah, sounds reasonable. PR Updated.
it('it displays a ReactNode', async () => { | ||
const { getByLabelText, findByText, queryByText } = render( | ||
<FormHelpButton identifier="abc" help={help('def', <span>Hello, ReactNode</span>)} />, | ||
) | ||
|
||
fireEvent.click(getByLabelText('help')) | ||
|
||
await findByText('Hello, ReactNode') | ||
expect(queryByText('Hello, ReactNode')).toBeTruthy() | ||
}) | ||
|
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.
@gj262 Here's a unit test for setting HelpParams content to a ReactNode
content: string | React.ReactNode | ||
} | ||
|
||
export function help(label = '', text = ''): HelpProps { | ||
return { label, text } | ||
export function help(label: string, content: string | React.ReactNode): HelpProps { | ||
return { label, content } |
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.
@gj262 text
renamed to content
, and it allows React.ReactNode.
Hi @abrie LGTM and thanks for tidying this up and fixing the test. However it looks like you missed one, the help on the Severity Card is not rendering with the preview. |
@abrie I would prefer to just transpile the package. In fact I already did it on another branch. Will backport right now. |
Ok |
@abrie @gj262 Now, let's talk about the refactoring I see in the diff. Adding a @rneher Richard has never actually used any of this functionality, but I hope we can do it later still. We have so many questions because people simply don't understand what parameters do, or whether one include another or whether they are cumulative etc. A little link to FAQ + an SVG image would do magic there. What do you think folks? Tremendous push would be to scaffold a bunch of components somewhere, preferably in The opposite move would be to remove this functionality, make it a string and don't bother with the package. @abrie I only glanced over your PR, so let me know if I am absolutely wrong. It can easily be the case. In which case I trust Gavin @gj262 to merge this or something similar. Feel free to create a coherent issue(s) to make it more transparent for the community. |
@ivan-aksamentov sounds good, your fix is similar to the one I tried in the window.open() fix. #334. And I like @abrie's approach of making the help popover label explicit and separate from the form label as it should be IMHO (separate concerns, etc.). So if the work in this PR can be resurrected I'll happily review and merge :) |
This was a poorly titled PR. As @ivan-aksamentov noted, it is primarily a refactoring. It fixed the unit tests as a side effect. If the plan is a more elaborate help system, then these changes are probably worth exploring. The existing implementation is magical and could use a more explicit approach, i.e. separate concerns, etc (thanks @gj262). For future enhancements, this PR provides an interface, |
Related issues and PRs
#336
Description
Fixes broken unit tests caused by
react-children-utilities
dependency.FormHelpButton
uses the label attribute to populate the help box. If the label is a React node then the text is difficult to access programmatically. #336 introduces a dependency,react-children-utilities
for the 'onlyText' method to extract the text from React nodes. But the dependency breaks the unit tests because the ESM module format is not supported by Jest (see open issue jestjs/jest#4842).This PR creates a
HelpProps
interface to communicate the help label and text to theFormHelpButton
. This solution is slightly more verbose than extracting text from the 'label' attribute. But it is more explicit and breaks no existing behavior.Impacted Areas in the application
This changes a lot of files, but in a predictable and uniform way.
Both the UI and unit tests are affected by these changes.
Testing
yarn test