-
Notifications
You must be signed in to change notification settings - Fork 86
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
Conversation
There was a problem hiding this 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
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"); | ||
}); |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
src/components/form/form.test.tsx
Outdated
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"); | ||
}); |
There was a problem hiding this comment.
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:
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(); | |
}); |
There was a problem hiding this comment.
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 👍
There was a problem hiding this comment.
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", () => { |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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 = () => { |
There was a problem hiding this comment.
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 👏
031f2bd
to
600aca2
Compare
🎉 This PR is included in version 142.9.1 🎉 The release is available on: Your semantic-release bot 📦🚀 |
Proposed behaviour
Form component tests should use react-testing-library
Current behaviour
tests use Enzyme
Checklist
d.ts
file added or updated if requiredQA
Additional context
Testing instructions