zenoamaro / react-quill

A Quill component for React.
https://zenoamaro.github.io/react-quill
MIT License
6.77k stars 923 forks source link

How to test with React Testing Library? #729

Open MateusFP95 opened 3 years ago

MateusFP95 commented 3 years ago

That's actually not a issue, but I didn't find any doc on internet.

How to test with React Testing Library?

How can I simulate a user input on a field?

ReactQuill version

alexkrolick commented 3 years ago

JSDOM doesn't implement all the DOM events and layout stuff you need to test this library fully. A full browser environment is recommended. The Quill tests are in Selenium and React-Quill uses Cypress. See https://github.com/zenoamaro/react-quill/blob/master/test/index.spec.js

You may be able to simulate basic stuff with RTL in JSDOM by firing change events on the content-editable .ql-editor

tgfischer commented 1 year ago

You should be able to use @testing-library/user-event to simulate the user typing into the .ql-editor.

For example, a test I've written that does this looks something like this

import { render } from "@testing-library/react";
import userEvent from "@testing-library/user-event";

...

it("should type something into the editor, and verify the content is saved", () => {
  const { container } = render(<Editor ... />);

  // This might look a bit different if you use `userEvent.setup`
  // https://testing-library.com/docs/user-event/setup#starting-a-session-per-setup
  userEvent.type(container.querySelector(".ql-editor")!, "Hello, World!");

  // Trigger some sort of event to save the notepad content
  userEvent.click(screen.getByRole("button", { name: "Save" }));

  // Assert the request was made. You should be able to verify the content contains `Hello, World!` now
});
Hari-Krishna-B commented 1 year ago

The above solution throws TypeError: Cannot read property 'rangeCount' of undefined. Any lead on this?

incepter commented 11 months ago

Testing with vitest , testing-library and happy-dom makes the test hang forever

HugoPoi commented 10 months ago

I confirm what @incepter said, there is some infinite loop when Quill is loaded in HappyDom, but it works with JSDom.

RichMatthews commented 1 month ago

Any movement on this??