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

Testing RuxInput in React with react-testing-library #1114

Closed
micahjones13 opened this issue Apr 13, 2023 · 6 comments
Closed

Testing RuxInput in React with react-testing-library #1114

micahjones13 opened this issue Apr 13, 2023 · 6 comments
Assignees
Labels
react This is a React specific issue. ticket created A ticket has been created about this issue on the Astro JIRA board.

Comments

@micahjones13
Copy link
Collaborator

image
I am trying to replicate this test and am having a few problems. For the RuxInput, the setInput is not a prop to RuxInput, I changed this to onRuxinput and that resolved the issue with syntax. When I run this test, the userEvent.type(shadowInput, 'User input!') does not seem to fire the onRuxinput which makes it hard to test functionality that would go this RuxInput. Any tips or advice would be appreciated.

Originally posted by @jarod-oa in #1113 (comment)

@micahjones13 micahjones13 self-assigned this Apr 13, 2023
@micahjones13 micahjones13 changed the title ![image](https://user-images.githubusercontent.com/130700314/231860280-d6b4f55c-a982-4675-8da5-1e368c8a4112.png) Testing RuxInput in React with react-testing-library Apr 13, 2023
@micahjones13 micahjones13 added react This is a React specific issue. ticket created A ticket has been created about this issue on the Astro JIRA board. labels Apr 13, 2023
@mrclean-oa
Copy link

Possibly related to #115?

@micahjones13
Copy link
Collaborator Author

For the RuxInput, the setInput is not a prop to RuxInput, I changed this to onRuxinput and that resolved the issue with syntax

Yup, looks like I screwed up the code in the docs, thanks for catching that. I'll change it ASAP. It should look like:

    <div>
      <RuxInput
        label="Rux Input"
        type="text"
        data-testid="rux-input-test"
        value={input}
        onRuxinput={(e: CustomEvent<HTMLRuxInputElement>) => {
          const target = e.target as HTMLInputElement;
          setInput(target.value);
        }}
      />
    </div>

When I run this test, the userEvent.type(shadowInput, 'User input!') does not seem to fire

Are you getting any sort of error when you run the test, or is it just failing with no helpful output?

In case it's helpful, I created a fresh repo to test this all out in. It's working, but the test command had to be updated to
"react-scripts test --transformIgnorePatterns=\"node_modules/(?!@astrouxds/react)/\" --env=jsdom" in order to run without error.

I apologize again for the incorrect documentation. Please let me know if the above helps, or if there is anything else I can do!

@jarod-oa
Copy link

jarod-oa commented Apr 17, 2023

Hey Micah, this still doesn't fix my issue.

form

<div>
      <RuxInput
        label="Rux Input"
        data-testid="rux-input-test"
        value={input}
        onRuxinput={(e: CustomEvent<HTMLRuxInputElement>) => {
          const target = e.target as HTMLInputElement;
          setInput(target.value);
          console.log("hello")
        }}
      />
    </div>

test

    render(
      <RuxInputTest />
    );
    const ruxInput = await screen.getByTestId('rux-input-test') as HTMLInputElement;
    await act(async () => {
      await userEvent.type(ruxInput, 'every character fires console.log')
    });
    expect(value.testValue).toBe(230);
  });

package.json

"test": "jest --env=jsdom",

jest.config.js

transformIgnorePatterns: ["node_modules/(?!@astrouxds/react)"],

babel.config.json

{
  "plugins": [
    "@babel/plugin-proposal-class-properties",
    "@babel/transform-runtime"
  ],
  "presets": [
    "@babel/preset-env",
    "@babel/preset-typescript",
    "@babel/preset-react"
  ]
}

Sorry this is a lot, but using the jest with the UserEvent.type() should make a console.log fire for every character but it is never happening. When I replace the with a native tag it works as expected. Does the use of jest change this?

@micahjones13
Copy link
Collaborator Author

Alright, I've mimicked that test as much as I could, and I'm seeing the console logs fire when I run the tests. I did have to change it a bit because I'm just missing some context, but this is what I've got:

//imports 
import { render, fireEvent, act, screen } from "@testing-library/react";
import userEvent from "@testing-library/user-event";
import { within } from "testing-library__dom";
import RuxInputTest from "./form";

test("Test", async () => {
    render(<RuxInputTest />);
    const ruxInput = screen.getByTestId("rux-input-test") as HTMLInputElement;
    const shadowInput: HTMLInputElement = await within(
      ruxInput
    ).findByLabelText("Rux Input");
    await act(async () => {
      await userEvent.type(shadowInput, "every character fires console.log");
    });
    expect(shadowInput.value).toBe("every character fires console.log");
  });

I think the important part here is probably using the within function from testing-library__dom. This allows the test to pierce the shadow dom of RuxInput, which then allows for the userEvent.type to work.

@jarod-oa
Copy link

Hey Micah,

This got it to work, thanks for the help. I thought I had tried the testing-library__dom but must not have done it correctly. Sorry about the code being a little bit off, I had changed it for a function and didn't change it back to how it was to match your test.

@micahjones13
Copy link
Collaborator Author

No worries! I'm glad it's working out. RTL and shadow dom are not friends, so hopefully it goes smoother from here on out. If not, please feel free to reach out to us! You can open a new issue/discussion, or if you'd rather you can email me directly at [email protected].

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
react This is a React specific issue. ticket created A ticket has been created about this issue on the Astro JIRA board.
Projects
None yet
Development

No branches or pull requests

3 participants