Mocking Callbacks And Components: Redundant button presence check and unnecessary async keyword in CustomButton tests
Checks
- [X] This is not a duplicate of an existing issue (please have a look through our open issues list to make sure)
- [X] I have thoroughly read and understand The Odin Project Contributing Guide
- [X] Would you like to work on this issue?
Describe your suggestion
Issue:
-
Redundant button presence check:
- The test for rendering the button with the text "Click me" uses
screen.getByRole("button", { name: "Click me" })to find the button. SincegetByRolewill throw an error if the button is not found, the additionalexpect(button).toBeInTheDocument()is redundant.
- The test for rendering the button with the text "Click me" uses
-
Unnecessary
asynckeyword:- In the test that verifies the
onClickfunction is not called when the button isn't clicked, theasynckeyword is used. However, there are no asynchronous operations (likeawaitor asynchronous events) in this test, making theasynckeyword unnecessary.
- In the test that verifies the
Proposed Solution:
-
Remove redundant button presence check:
Before:
const button = screen.getByRole("button", { name: "Click me" }); expect(button).toBeInTheDocument();After:
const button = screen.getByRole("button", { name: "Click me" }); expect(button).toHaveTextContent("Click me"); -
Remove the
asynckeyword where not needed:Before:
it("should not call the onClick function when it isn't clicked", async () => { const onClick = vi.fn(); render(<CustomButton onClick={onClick} />); expect(onClick).not.toHaveBeenCalled(); });After:
it("should not call the onClick function when it isn't clicked", () => { const onClick = vi.fn(); render(<CustomButton onClick={onClick} />); expect(onClick).not.toHaveBeenCalled(); });
Path
Node / JS
Lesson Url
https://www.theodinproject.com/lessons/node-path-react-new-mocking-callbacks-and-components
(Optional) Discord Name
de_kr1sh
(Optional) Additional Comments
No response
Thank you for making this issue @dekr1sh -- and thank you for writing it in such a detailed way!
I'll ping our @TheOdinProject/javascript team to get their opinions on your proposed changes.
- Let's replace
getByRolewithqueryByRoleand keep the expect statement - Agreed
@01zulfi @dekr1sh
The async thing is fine to fix, but the getByRole thing should be left as is,
query* is only for negative assertions to prevent an error being thrown. Positive query* queries that fail will only return null, and can only be tested against that, so the test output is minimal and unhelpful. Positive get* queries call screen.debug() when they fail and throw, which is significantly more helpful.
get* queries still ought to have assertions if asserting something. While redundant when the code is actually run, explicit assertions are definitely not redundant to developers. Especially when get* queries don't always come with assertions (e.g. grabbing an element to then interact with it). i.e. get* queries should ideally just be used as queries and not only sometimes double as assertions. Assertions should still be explicit.
Reopening for assignment. Please do not change any of the actual queries (see above comment).
Acceptance criteria
- [ ] Remove the unnecessary
asynckeyword from the last test callback in the code block.
Comment below if you'd like to be assigned to work on this issue. Please do not open a PR unless you've been assigned by a maintainer.
May I be assigned to this issue?
Go for it