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

chore: move autosuggest tests from Enzyme to RTL #2611

Merged
merged 1 commit into from
Sep 12, 2023

Conversation

cintnguyen
Copy link
Contributor

Description

Move autosuggest tests from Enzyme to RTL

Deploy Preview

N/A

Merge Checklist

  • Is there adequate test coverage for your changes?

Post-merge Checklist

  • Verify your changes were released to NPM at the expected version.
  • If you'd like, share your contribution in #show-and-tell.
  • 🎉 🙌 Celebrate! Thanks for your contribution.

@netlify
Copy link

netlify bot commented Sep 8, 2023

Deploy Preview for paragon-openedx ready!

Built without sensitive environment variables

Name Link
🔨 Latest commit 33fb35b
🔍 Latest deploy log https://app.netlify.com/sites/paragon-openedx/deploys/64ff8d0881419e00080fd481
😎 Deploy Preview https://deploy-preview-2611--paragon-openedx.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

@codecov
Copy link

codecov bot commented Sep 8, 2023

Codecov Report

Patch coverage has no change and project coverage change: +0.16% 🎉

Comparison is base (f1c3596) 91.72% compared to head (71bc0ae) 91.88%.

❗ Current head 71bc0ae differs from pull request most recent head 33fb35b. Consider uploading reports for the commit 33fb35b to get more accurate results

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #2611      +/-   ##
==========================================
+ Coverage   91.72%   91.88%   +0.16%     
==========================================
  Files         235      235              
  Lines        4217     4214       -3     
  Branches     1020     1021       +1     
==========================================
+ Hits         3868     3872       +4     
+ Misses        345      338       -7     
  Partials        4        4              
Files Changed Coverage Δ
src/Form/FormAutosuggest.jsx 96.66% <ø> (+0.96%) ⬆️
src/Form/FormAutosuggestOption.jsx 100.00% <ø> (ø)

... and 1 file with indirect coverage changes

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@@ -13,7 +13,7 @@ function FormAutosuggestOption({
<MenuItem
as="li"
role="option"
tabindex="-1"
Copy link
Contributor

Choose a reason for hiding this comment

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

good catch!

it('renders component with options', () => {
const { getByTestId, container } = render(<FormAutosuggestTestComponent />);
const input = getByTestId('autosuggest_textbox_input');
fireEvent.click(input);
Copy link
Member

Choose a reason for hiding this comment

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

[curious] Is there a reason to use fireEvent here (and elsewhere throughout the tests) versus using userEvent?

If it's possible to use userEvent.click instead, it's generally preferable since it simulates more of the true user interactions in the browser, not just the low-level click itself.

For example, userEvent.click also simulates a previous hover event that a real user would trigger prior to clicking.

Resources:

Copy link
Contributor Author

Choose a reason for hiding this comment

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

});
it('renders without loading state', () => {
const { container } = render(<FormAutosuggestTestComponent />);
expect(container.querySelector('.pgn__form-autosuggest__dropdown-loading')).toBeNull();
Copy link
Member

Choose a reason for hiding this comment

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

Is there a way to avoid using the class name implementation detail in this test?

For example, if the component renders a Spinner with the supplied screenReaderText prop, could this test assert that the Spinner is not rendered based on a data-testid attribute versus relying on the class name that could change at any time. If the class name did change down the road, this test would still pass.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

const wrapper = mount(<FormAutosuggestWrapper value="Test Value" />);
it('render with loading state', () => {
const { container } = render(<FormAutosuggestWrapper isLoading />);
expect(container.querySelector('.pgn__form-autosuggest__dropdown-loading')).toBeTruthy();
Copy link
Member

Choose a reason for hiding this comment

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

Similar to above comment, could we either assert that the screenReaderText passed to the Spinner is rendered, or alternatively, rely on a more stable data-testid instead of the class name?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@cintnguyen cintnguyen force-pushed the formAutosuggest-rtl branch 3 times, most recently from 6c9a96e to 0248452 Compare September 11, 2023 21:14
Copy link
Member

@adamstankiewicz adamstankiewicz left a comment

Choose a reason for hiding this comment

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

LGTM!

@brian-smith-tcril
Copy link
Contributor

So the current failing test is the error state test. It was written by having the simulated user enter the textbox and press escape. This worked before 4816baa#diff-96e13c1a773ec8d218ea5667d3dec6c8f6eeadcb00c81584f50d6c3d4d52135fL133, but now that doesn't trigger the error state.

It should probably be updated to simulate a click outside, as that will still trigger the error state (I verified on https://paragon-openedx.netlify.app/components/form/form-autosuggest/)

@cintnguyen
Copy link
Contributor Author

So the current failing test is the error state test. It was written by having the simulated user enter the textbox and press escape. This worked before 4816baa#diff-96e13c1a773ec8d218ea5667d3dec6c8f6eeadcb00c81584f50d6c3d4d52135fL133, but now that doesn't trigger the error state.

It should probably be updated to simulate a click outside, as that will still trigger the error state (I verified on https://paragon-openedx.netlify.app/components/form/form-autosuggest/)

Fix here: https://github.com/openedx/paragon/compare/ba5085a9a58e9a35f545070541decaba69c8130f..33fb35b955f6e7de8b872791fe5794616300cff9

@@ -102,6 +102,7 @@ function FormAutosuggest({
const iconToggle = (
<IconButton
className="pgn__form-autosuggest__icon-button"
data-testid="autosuggest_iconbutton"
Copy link
Contributor

Choose a reason for hiding this comment

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

We recently rewrote all tests (except FormAutosuggest) to RTL. For uniformity of test identifiers, we agreed to use a kebab case. Can you convert the names of the identifiers to a uniform format? autosuggest-iconbutton

@@ -253,7 +255,7 @@ function FormAutosuggest({
>
{isLoading ? (
<div className="pgn__form-autosuggest__dropdown-loading">
<Spinner animation="border" variant="dark" screenReaderText={screenReaderText} />
<Spinner animation="border" variant="dark" screenReaderText={screenReaderText} data-testid="autosuggest_loading_spinner" />
Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe more readable

Suggested change
<Spinner animation="border" variant="dark" screenReaderText={screenReaderText} data-testid="autosuggest_loading_spinner" />
<Spinner
animation="border"
variant="dark"
screenReaderText={screenReaderText}
data-testid="autosuggest-loading-spinner"
/>

@@ -230,6 +231,7 @@ function FormAutosuggest({
onChange={handleOnChange}
onClick={handleClick}
trailingElement={iconToggle}
data-testid="autosuggest_textbox_input"
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
data-testid="autosuggest_textbox_input"
data-testid="autosuggest-textbox-input"

@@ -12,8 +12,9 @@ function FormAutosuggestOption({
return (
<MenuItem
as="li"
data-testid="autosuggest_optionitem"
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
data-testid="autosuggest_optionitem"
data-testid="autosuggest-optionitem"

import PropTypes from 'prop-types';
import { render, screen } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import '@testing-library/jest-dom';
Copy link
Contributor

Choose a reason for hiding this comment

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

Is it better to move this line to setupTests.js? Did you encounter some problems with this?

@brian-smith-tcril brian-smith-tcril merged commit 23b83c5 into openedx:master Sep 12, 2023
7 checks passed
@brian-smith-tcril
Copy link
Contributor

@PKulkoRaccoonGang I'm sorry I was a bit hasty and didn't see these comments before hitting merge. We should be able to address them in a quick follow-up PR.

@edx-semantic-release
Copy link
Contributor

🎉 This PR is included in version 21.1.8 🎉

The release is available on:

Your semantic-release bot 📦🚀

@edx-semantic-release
Copy link
Contributor

🎉 This PR is included in version 22.0.0-alpha.6 🎉

The release is available on:

Your semantic-release bot 📦🚀

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

Successfully merging this pull request may close these issues.

5 participants