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

Setup user-event and change some fireEvent to userEvent in tests #371

Merged
merged 4 commits into from
Apr 8, 2024

Conversation

djalmaaraujo
Copy link
Contributor

Description

React Testing Library UserEvent API seems to have a better API than fireEvent to deal with user events. @brookewp mentioned this during a call with us, and I believe we should change some tests to have a better coverage of user events.

Checklist

  • This PR has good automated test coverage
  • The storybook for the component has been updated

Steps to Test

  1. Pull down PR.
  2. npm run install.
  3. npm run test.
  4. Tests should pass.

@djalmaaraujo djalmaaraujo requested review from brookewp and a team April 2, 2024 19:02
Copy link

netlify bot commented Apr 2, 2024

Deploy Preview for vip-design-system-components ready!

Name Link
🔨 Latest commit 9470f80
🔍 Latest deploy log https://app.netlify.com/sites/vip-design-system-components/deploys/660d47db16f71100083ee644
😎 Deploy Preview https://deploy-preview-371--vip-design-system-components.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.

@@ -16,6 +16,9 @@ module.exports = {
'id-length': 'off',
'import/no-extraneous-dependencies': 'off',
'jsx-a11y/click-events-have-key-events': 'off',
'@typescript-eslint/no-unsafe-call': 'off',
Copy link
Contributor Author

Choose a reason for hiding this comment

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

We also disable this in the VIP dashboard, making it impossible to pass tests using user events.

Copy link
Contributor

Choose a reason for hiding this comment

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

When you replaced fireEvent with userEvent (i.e. in the Accordion test), the test would fail unless this rule was disabled? I haven't been able to replicate the error so I'm curious if I'm missing something here.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, it's an issue that the community still needs to solve. I tried many different things and didn't work. Many TS errors cannot be solved.

testing-library/user-event#1146

Copy link
Contributor

Choose a reason for hiding this comment

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

Thanks for the context!

const { container } = renderComponent();

fireEvent.click( screen.getByRole( 'button', { name: 'trigger two' } ) );
await user.click( screen.getByRole( 'button', { name: 'trigger two' } ) );

// Should find the open content
expect( screen.queryByText( 'content one' ) ).toBeNull();
Copy link
Contributor

Choose a reason for hiding this comment

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

Not a cause of this PR, but I noticed the comments are mixed up in the second test. It should be:

diff --git a/src/system/Accordion/Accordion.test.tsx b/src/system/Accordion/Accordion.test.tsx
index cc9d93a..cbd460c 100644
--- a/src/system/Accordion/Accordion.test.tsx
+++ b/src/system/Accordion/Accordion.test.tsx
@@ -52,11 +52,11 @@ describe( '<Accordion />', () => {
 
-		// Should find the open content
+		// Should not find the closed content
 		expect( screen.queryByText( 'content one' ) ).toBeNull();
 
-		// Should not find the closed content
-		expect( screen.queryByText( 'content two' ) ).toBeInTheDocument();
+		// Should find the open content
+		expect( screen.getByText( 'content two' ) ).toBeInTheDocument();

We could also change the queryBy to getBy for the second assertion since I believe queryBy is usually used when we expect it not to be found.

Copy link
Contributor

Choose a reason for hiding this comment

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

This could be a follow-up, but if we're already editing the test... 😄 we could make the test more robust by making it more specific and checking against things like the expanded/open state which would affect the user's experience. Also asserting toBeVisible, which checks if it's in the document like toBeInTheDocument, but it also checks to ensure it's not hidden via attributes or CSS.

diff --git a/src/system/Accordion/Accordion.test.tsx b/src/system/Accordion/Accordion.test.tsx
index d770ccc..26d5f34 100644
--- a/src/system/Accordion/Accordion.test.tsx
+++ b/src/system/Accordion/Accordion.test.tsx
@@ -51,13 +51,20 @@ describe( '<Accordion />', () => {
 
 		const { container } = renderComponent();
 
-		await user.click( screen.getByRole( 'button', { name: 'trigger two' } ) );
-
-		// Should find the open content
-		expect( screen.queryByText( 'content one' ) ).toBeNull();
+		await user.click( screen.getByRole( 'button', { name: 'trigger two', expanded: false } ) );
 
 		// Should not find the closed content
-		expect( screen.queryByText( 'content two' ) ).toBeInTheDocument();
+		expect(
+			screen.getByRole( 'button', { name: 'trigger one', expanded: false } )
+		).toHaveAttribute( 'data-state', 'closed' );
+		expect( screen.queryByText( 'content one' ) ).not.toBeInTheDocument();
+
+		// Should find the open content
+		expect( screen.getByRole( 'button', { name: 'trigger two', expanded: true } ) ).toHaveAttribute(
+			'data-state',
+			'open'
+		);
+		expect( screen.getByText( 'content two' ) ).toBeVisible();
 
 		// Check for accessibility issues
 		expect( await axe( container ) ).toHaveNoViolations();

@brookewp
Copy link
Contributor

brookewp commented Apr 2, 2024

Thank you for opening this to implement userEvent! 🎉 Are the CSS changes a result of auto-formatting based on some rules?

@djalmaaraujo
Copy link
Contributor Author

Thank you for opening this to implement userEvent! 🎉 Are the CSS changes a result of auto-formatting based on some rules?

CSS changes were a minor thing I ended up fixing because the tests were warning me something annoying in the terminal.

Copy link
Contributor

@brookewp brookewp left a comment

Choose a reason for hiding this comment

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

Thanks again for adding this! 😄 Tests are passing! 🚀

@djalmaaraujo djalmaaraujo merged commit 616f37a into trunk Apr 8, 2024
8 checks passed
@djalmaaraujo djalmaaraujo deleted the add/user-event branch April 8, 2024 13:13
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.

None yet

2 participants