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: fix linting errors in RTL-migrated test files #6709

Merged
merged 1 commit into from
May 20, 2024

Conversation

Parsium
Copy link
Contributor

@Parsium Parsium commented May 2, 2024

Proposed Behaviour

  • Change file extension of existing test files fully migrated to RTL to *.test.*.
  • Resolve all linting errors present in the current RTL tests.

Current Behaviour

  • Both Enzyme and RTL tests are currently stored in files with the *.spec.* extension.
  • Some of the existing RTL tests do not adhere to the best practices recommended by RTL's maintainers.

Additional Context

This PR is the second part of a two-phased process to introduce linting support for RTL tests, following the first PR, #6715. This division has been implemented to streamline the code review process.

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

await user.tab();
const user = userEvent.setup({
advanceTimers: jest.advanceTimersByTime,
delay: null,
Copy link
Contributor Author

@Parsium Parsium May 2, 2024

Choose a reason for hiding this comment

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

We should be cautious about using the delay option to userEvent.setup.

By default, user-event adds a small delay between subsequent inputs, like key presses, to simulate how a real user would provide input. Setting delay to null removes it entirely. While this may be useful in some cases, it could potentially mask issues related to timeouts or promises in our code.

@nineteen88
Copy link
Contributor

I see some of the previous spec files that were more or less already RTL have had their filename changed so that RTL linting will occur on them but we should also make a note of these files that have been converted so they don't get missed when we sweep them for refactor. There's still quite a lot of bad practices in those files that we want to get on top of.

@Parsium Parsium changed the title chore: set-up linting plugins for jest-dom and React Testing Library chore: address linting errors in existing RTL tests May 3, 2024
@Parsium Parsium changed the title chore: address linting errors in existing RTL tests refactor: address linting errors in existing RTL tests May 3, 2024
@Parsium Parsium force-pushed the linting-rtl-tests branch 2 times, most recently from 406ad3d to 27997f0 Compare May 3, 2024 10:43
@Parsium
Copy link
Contributor Author

Parsium commented May 3, 2024

Adding DO NOT MERGE label - since this PR depends on the release of #6715 first

@Parsium
Copy link
Contributor Author

Parsium commented May 3, 2024

I see some of the previous spec files that were more or less already RTL have had their filename changed so that RTL linting will occur on them but we should also make a note of these files that have been converted so they don't get missed when we sweep them for refactor. There's still quite a lot of bad practices in those files that we want to get on top of.

Raised FE-6549 for this 👍🏼

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.

Great work @Parsium some comments from me but mainly non-blocking

src/__internal__/focus-trap/focus-trap.test.tsx Outdated Show resolved Hide resolved
});

it("refocuses the container within the trap when flag is set, if the wrapper has no tabindex", () => {
const { rerender } = render(
mockComponentToRender({ autoFocus: false, triggerRefocusFlag: false })
);
// need to blur the wrapper to remove the tabindex
fireEvent.blur(screen.getByTestId(WRAPPER_ID));
fireEvent.blur(screen.getByRole("dialog"));
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): we could use user.tab() here to mimic the user behaviour, happy for that to be done in a new ticket though as we're just addressing linting complaints here etc

@@ -25,7 +25,7 @@ import useLocale from "../../hooks/__internal__/useLocale";
export interface FileInputProps
extends Pick<ValidationProps, "error">,
Pick<InputProps, "id" | "name" | "required">,
TagProps,
Omit<TagProps, "data-component">,
Copy link
Contributor

Choose a reason for hiding this comment

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

Praise: good spot 👏

@@ -35,7 +35,7 @@ describe("FixedNavigationBarContextProvider", () => {
<MockComponent position={position} orientation="top" offset="20px" />
);
const result = screen.getByTestId("output");
expect(result.textContent).toBe("");
expect(result).toHaveTextContent("");
Copy link
Contributor

Choose a reason for hiding this comment

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

Praise: nice 👏

src/components/step-flow/step-flow.test.tsx Outdated Show resolved Hide resolved
return renderRTL(
<Tooltip {...props} message={message || props.message}>
{props.children || children}
function renderTooltip(props: Partial<TooltipProps> = {}) {
Copy link
Contributor

Choose a reason for hiding this comment

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

comment (non-blocking): same comment about abstracting the render, happy to sort all this later though 👍

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah, this file needs a good review due to the nested structure and styling rules, so I'll leave this for now.

let middleware;
if (useFloatingSpy.mock.calls[0][0]?.middleware?.[0]) {
middleware = useFloatingSpy.mock.calls[0][0]?.middleware?.[0];
describe.each(["top", "bottom", "left", "right"] as const)(
Copy link
Contributor

Choose a reason for hiding this comment

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

comment (non-blocking): this is the prime example of a test that we need to refactor 😆. Again happy for it to be part of future work

src/hooks/__internal__/useModalAria/useModalAria.test.tsx Outdated Show resolved Hide resolved
Copy link
Contributor

@nineteen88 nineteen88 left a comment

Choose a reason for hiding this comment

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

Good work James. Can see a lot of very good improvements overall. A few things still don't sit right with me as previously mentioned/discussed but we'll get there!

src/__internal__/focus-trap/focus-trap.test.tsx Outdated Show resolved Hide resolved
await shiftTabPress();
expect(screen.getByText(BUTTON_TWO)).toHaveFocus();
});
it("loops focus to the last focusable element, when focus on first radio button shift-tab pressed", async () => {
Copy link
Contributor

Choose a reason for hiding this comment

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

praise: much improved language/description of the test

Applies to all of those changes

expect(hintText).toBeVisible();
});

it("accepts data tag props", () => {
it("renders root container with correct data tag props", () => {
Copy link
Contributor

Choose a reason for hiding this comment

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

praise: much improved test description

Applies for the others you've made as well

src/components/file-input/file-input.test.tsx Outdated Show resolved Hide resolved
src/components/file-input/file-input.test.tsx Outdated Show resolved Hide resolved
src/components/file-input/file-input.test.tsx Outdated Show resolved Hide resolved
src/components/step-flow/step-flow.test.tsx Show resolved Hide resolved
@Parsium Parsium force-pushed the linting-rtl-tests branch 4 times, most recently from a4e424b to 3e8a87a Compare May 15, 2024 11:28
expect(screen.getByText("bar")).toBeVisible();
});

it("when the 'title' prop is passed, the correct element and text renders", () => {
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): language still wrong on this test

Might be worth adding to the re-visit ticket rather than dealing with it now unless there were other changes to be made

}
);

it("when the 'showCloseIcon' prop is true, the correct element renders", () => {
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): language still wrong on this test

Might be worth adding to the re-visit ticket rather than dealing with it now unless there were other changes to be made

@edleeks87 edleeks87 marked this pull request as ready for review May 20, 2024 12:35
@edleeks87 edleeks87 requested review from a team as code owners May 20, 2024 12:35
@Parsium Parsium changed the title refactor: address linting errors in existing RTL tests refactor: fix linting errors in RTL-migrated test files May 20, 2024
@Parsium Parsium merged commit e1c45e2 into master May 20, 2024
21 checks passed
@Parsium Parsium deleted the linting-rtl-tests branch May 20, 2024 12:53
@carbonci
Copy link
Collaborator

🎉 This PR is included in version 135.1.2 🎉

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
Development

Successfully merging this pull request may close these issues.

None yet

4 participants