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

test(text-area): Change tests to react-testing-library and improve coverage #394

Merged
merged 19 commits into from
Feb 1, 2020
Merged

test(text-area): Change tests to react-testing-library and improve coverage #394

merged 19 commits into from
Feb 1, 2020

Conversation

stephanerangaya
Copy link
Contributor

@stephanerangaya stephanerangaya commented Jan 6, 2020

Summary

Related to #286

Converts test to RTL, improves coverage, adds a static states story for visual regression testing, and uses a more BDD (behavioral-driven) approach for unit tests.

Checklist

  • branch has been rebased on the latest master commit
  • tests are changed or added
  • yarn test passes
  • all (dev)dependencies that the module needs is added to its package.json
  • code adheres to the API & Pattern guidelines

@cypress
Copy link

cypress bot commented Jan 6, 2020



Test summary

134 0 0 0


Run details

Project canvas-kit
Status Passed
Commit 7e5167f
Started Jan 31, 2020 11:53 PM
Ended Jan 31, 2020 11:56 PM
Duration 02:35 💡
OS Linux Ubuntu Linux - 16.04
Browser Electron 61

View run in Cypress Dashboard ➡️


This comment has been generated by cypress-bot as a result of this project's GitHub integration settings. You can manage this integration in this project's settings in the Cypress Dashboard

@lychyi lychyi mentioned this pull request Jan 6, 2020
31 tasks
@stephanerangaya stephanerangaya changed the title test(text-area): Switch tests to RTL test(text-area): Change tests to react-testing-library and improve coverage Jan 13, 2020
Copy link
Contributor

@mannycarrera4 mannycarrera4 left a comment

Choose a reason for hiding this comment

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

I think for StaticStates to work, we need to import the styled function from labs core, i forgot about this!

@stephanerangaya
Copy link
Contributor Author

I think for StaticStates to work, we need to import the styled function from labs core, i forgot about this!

They work I think, aren't they?

const ref: React.RefObject<HTMLInputElement> = React.createRef();
render(<TextArea inputRef={ref} id={id} />);
expect(ref.current).not.toBeNull();
expect(ref.current).toHaveAttribute('id', id);
Copy link
Member

Choose a reason for hiding this comment

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

I think the following should work:

expect(await getByRole('textbox')).toEqual(ref.current)

Also, as a side-note, I think it is safer for Testing Library APIs to be used with the promise API since rendering can be asynchronous. The synchronous API could break if we slightly change the implementation of components or React slightly changes timing (e.g. useEffect vs componentDidMount or concurrent mode)

Copy link
Contributor

Choose a reason for hiding this comment

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

@NicholasBoll Are you saying that we should aways be using expect(await getBy*()) instead of expect(getBy*())?

Copy link
Member

Choose a reason for hiding this comment

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

I know a bit more about the React Testing Library APIs. There are get*, query*, and find* queries.

find* is most likely what we want for React because of timing. find* returns a promise that resolves when the element is found (retrying until it is found). If there is a delay in rendering something (hooks, concurrent mode, etc), find* will eventually find the element and return it for testing.

So I should update my previous code snippet:

expect(await findByRole('textbox')).toEqual(ref.current)

Copy link
Member

Choose a reason for hiding this comment

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

I'm not sure if await find* vs get* is strictly necessary. React Testing Library hooks into some testing internals to fix timing issues introduced with hooks. I just consider timing to be an implementation detail of the component that should not introduce failures or flakiness of tests.

@NicholasBoll
Copy link
Member

@mannycarrera4 Do you want to approve the IconButton differences? I think they are from the IconButton changes, but I wanted to be sure.

The other change is new visual states for the text area. I'm looping in UX to make sure we got it right and that it makes sense.

@anicholls anicholls merged commit b68987d into Workday:master Feb 1, 2020
@jpante jpante added this to the Component Testing Coverage milestone Apr 3, 2020
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.

6 participants