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

refactor(text-editor): covert tests to use react-testing-library #7016

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

robinzigmond
Copy link
Contributor

Proposed behaviour

TextEditor component should have unit tests written in React Testing Library

NOTE: due to many issues with the drafjs library and jsdom/RTL, a lot of functionality was not possible to sensibly test, so these tests have been added to Playwright instead. Many unit tests have been added just for coverage that do not otherwise provide much value. When we eventually move away from draftjs (which is no longer supported) we should write RTL tests for what is being covered by the new Playwright tests, and remove both those playwright tests and as many as possible of the current "coverage only" unit tests.

Current behaviour

Unit tests use Enzyme

Checklist

  • Commits follow our style guide
  • Related issues linked in commit messages if required
  • Screenshots are included in the PR if useful
  • All themes are supported if required
  • Unit tests added or updated if required
  • Playwright automation tests added or updated if required
  • Storybook added or updated if required
  • Translations added or updated (including creating or amending translation keys table in storybook) if required
  • Typescript d.ts file added or updated if required
  • Related docs have been updated if required

QA

  • Tested in provided StackBlitz sandbox/Storybook
  • Add new Playwright test coverage if required
  • Carbon implementation matches Design System/designs
  • UI Tests GitHub check reviewed if required

Additional context

Testing instructions

);
});

test("files a console warning when the `rows` prop is passed as a number less than 2", () => {
Copy link
Contributor

Choose a reason for hiding this comment

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

suggestion/question/nitpick: Is this a spelling mistake?

Suggested change
test("files a console warning when the `rows` prop is passed as a number less than 2", () => {
test("fires a console warning when the `rows` prop is passed as a number less than 2", () => {

expect(screen.getByText("talic texti")).toBeVisible();
}, 10000); // allow to run for up to 10 seconds due to timeout issues

// for coverage only - this behaviour doesn't work properly in jsdom (note incorrect order of text being entere).
Copy link
Contributor

Choose a reason for hiding this comment

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

nitpick:

Suggested change
// for coverage only - this behaviour doesn't work properly in jsdom (note incorrect order of text being entere).
// for coverage only - this behaviour doesn't work properly in jsdom (note incorrect order of text being entered).

expect(screen.getByRole("listitem")).toHaveTextContent("ist iteml");
}, 10000); // allow to run for up to 10 seconds due to timeout issues

// for coverage only - this behaviour doesn't work properly in jsdom (note incorrect order of text being entere).
Copy link
Contributor

Choose a reason for hiding this comment

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

nitpick:

Suggested change
// for coverage only - this behaviour doesn't work properly in jsdom (note incorrect order of text being entere).
// for coverage only - this behaviour doesn't work properly in jsdom (note incorrect order of text being entered).

});

// the next several tests are for coverage only - due to an issue with React itself, dispatching the (deprecated) `keypress` event
// with non-standard propertoes is the only way to trigger the `handleBeforeInput` handler in a test. See
Copy link
Contributor

Choose a reason for hiding this comment

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

nitpick:

Suggested change
// with non-standard propertoes is the only way to trigger the `handleBeforeInput` handler in a test. See
// with non-standard properties is the only way to trigger the `handleBeforeInput` handler in a test. See

Copy link
Contributor

Choose a reason for hiding this comment

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

image

Copy link
Contributor

@edleeks87 edleeks87 left a comment

Choose a reason for hiding this comment

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

Really good work @robinzigmond, I really appreciate the detail you've gone to to explain why certain things have been added (has definitely limited the questions/comments from me) and the effort you've put into investigating the issues with JSDOM and DraftJS


await user.keyboard("{ArrowRight}");
expect(screen.getByRole("button", { name: "bold" })).toHaveFocus();
}, 40000); // allow to run for up to 40 seconds(!) due to timeout issues
Copy link
Contributor

Choose a reason for hiding this comment

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

suggestion (non-blocking): it might be worth splitting these tests up a bit so we can avoid large timeouts here, I think we can live with the 10 second ones but exceeding that seems too much. If that doesn't work I think it'd be better to move them to playwright until we can rewrite the component.

Update: had a little play around locally, I think if we use a combination of async findBy querying for the tooltips and await waitFor we can remove the 10 second timeout tests (didn't work for the larger ones) so maybe we can break up the slower ones or move them to playwright


screen.getByRole("button", { name: buttonName }).focus();

expect(screen.getByTestId("tooltip")).toBeVisible();
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
expect(screen.getByTestId("tooltip")).toBeVisible();
expect(await screen.findByRole("tooltip")).toBeVisible();

Copy link
Contributor

Choose a reason for hiding this comment

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

you can also probably combine them with:

expect(await screen.findByRole("tooltip", {name: tooltipText})).toBeVisible();

expect(screen.getByTestId("tooltip")).toHaveTextContent(tooltipText);

screen.getByRole("button", { name: buttonName }).blur();
expect(screen.queryByTestId("tooltip")).not.toBeInTheDocument();
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
expect(screen.queryByTestId("tooltip")).not.toBeInTheDocument();
expect(screen.queryByRole("tooltip")).not.toBeInTheDocument();

}
);

test("calls the `onClose` callback of an `LinkPreview` component if clicked", async () => {
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
test("calls the `onClose` callback of an `LinkPreview` component if clicked", async () => {
test("calls the `onClose` callback of a `LinkPreview` component if clicked", async () => {

});

// the next several tests are for coverage only - due to an issue with React itself, dispatching the (deprecated) `keypress` event
// with non-standard propertoes is the only way to trigger the `handleBeforeInput` handler in a test. See
Copy link
Contributor

Choose a reason for hiding this comment

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

image

await expect(innerTextList(page, "ol", 3)).toHaveText("baz");
});

test("when a `list toolbar button is clicked, added text is rendered in an unordered list, with enter separating list items", async ({
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
test("when a `list toolbar button is clicked, added text is rendered in an unordered list, with enter separating list items", async ({
test("when a `list` toolbar button is clicked, added text is rendered in an unordered list, with enter separating list items", async ({

await expect(innerTextList(page, "ul", 3)).toHaveText("baz");
});

test("when an list button is toggled on and off, new text is entered without the corresponding formatting, while the previously-entered text keeps the formatting", async ({
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
test("when an list button is toggled on and off, new text is entered without the corresponding formatting, while the previously-entered text keeps the formatting", async ({
test("when a `list` button is toggled on and off, new text is entered without the corresponding formatting, while the previously-entered text keeps the formatting", async ({

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

Successfully merging this pull request may close these issues.

3 participants