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(form): convert tests to use react-testing-library #6929

Merged
merged 2 commits into from
Sep 6, 2024

Conversation

robinzigmond
Copy link
Contributor

Proposed behaviour

Form component tests should use react-testing-library

Current behaviour

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

Copy link
Contributor

@nuria1110 nuria1110 left a comment

Choose a reason for hiding this comment

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

Looking good 👍, just a couple non-blocking comments

Comment on lines +21 to +40
test("renders buttons passed as the leftSideButtons prop", () => {
render(
<Form
leftSideButtons={
<>
<Button>Left1</Button>
<Button>Left2</Button>
</>
}
saveButton={<Button>Save</Button>}
/>
);

const footerButtons = within(screen.getByTestId("form-footer")).getAllByRole(
"button"
);
expect(footerButtons[0]).toHaveTextContent("Left1");
expect(footerButtons[1]).toHaveTextContent("Left2");
expect(footerButtons[2]).toHaveTextContent("Save");
});
Copy link
Contributor

Choose a reason for hiding this comment

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

nit-pick (non-blocking): Since saveButton is already being tested separately I don't think its necessary to also test it 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.

So the idea here is that we're checking that the leftSideButtons are actually on the left - ie they appear before the Save button. So I think it's important to render the Save button here to check that the first 2 buttons (in DOM order) are the left buttons, and that the save button comes next. I take the point that maybe the assertion that the third button (footerButtons[2]) is the save button isn't necessary, but personally I think having that there in the assertions makes it clearer what is being tested here, ie that the left buttons come first, then the save button.

Happy to wait to see what a second reviewer thinks though.

Copy link
Contributor

Choose a reason for hiding this comment

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

I can see both sides here tbh, I think we have the left and right side buttons captured in chromatic so the order isn't critical to me but it's not a huge issue to test that as it's trivial etc

Comment on lines 42 to 50
test("renders the button passed as the saveButton prop in the form footer", () => {
render(<Form saveButton={<Button>Custom Save Button</Button>} />);

const footerButton = within(screen.getByTestId("form-footer")).getByRole(
"button"
);
expect(footerButton).toHaveTextContent("Custom Save Button");
});
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): If only the saveButton is being rendered we could just assert that the button is visible:

Suggested change
test("renders the button passed as the saveButton prop in the form footer", () => {
render(<Form saveButton={<Button>Custom Save Button</Button>} />);
const footerButton = within(screen.getByTestId("form-footer")).getByRole(
"button"
);
expect(footerButton).toHaveTextContent("Custom Save Button");
});
test("renders the button passed as the saveButton prop in the form footer", () => {
render(<Form saveButton={<Button>Custom Save Button</Button>} />);
expect(screen.getByRole("button", { name: "Custom Save Button" }).toBeVisible();
});

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think the within is important here, because part of the test is that the button is rendered inside the form footer. But you're right that using the name option and a toBeVisible check is probably better than generically finding a button and checking its text content, so I'm happy to change that 👍

Copy link
Contributor

Choose a reason for hiding this comment

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

Yep agree within with toBeVisible works best imo

expect(footerButton).toHaveTextContent("Custom Save Button");
});

test("renders buttons passed as the rightSideButtons prop", () => {
Copy link
Contributor

Choose a reason for hiding this comment

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

nit-pick (non-blocking): Same as above, I don't think its necessary to also test saveButton 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.

same reply as above 😃

@@ -454,3 +455,33 @@ export const MockFormForAriaLiveDemo = () => {
};

MockFormForAriaLiveDemo.storyName = "Mock form for aria live demo";

export const MarginTest = () => {
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 idea moving this to chromatic 👏

@robinzigmond robinzigmond marked this pull request as ready for review September 6, 2024 10:41
@robinzigmond robinzigmond requested a review from a team as a code owner September 6, 2024 10:41
@robinzigmond robinzigmond merged commit ab28819 into master Sep 6, 2024
25 checks passed
@robinzigmond robinzigmond deleted the FE-6593-form-rtl branch September 6, 2024 10:47
@carbonci
Copy link
Collaborator

🎉 This PR is included in version 142.9.1 🎉

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.

4 participants