-
Notifications
You must be signed in to change notification settings - Fork 729
Document using with Jest mock functions #138
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
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.
Nobody merge this yet. I have thoughts and feedback that I'll give when I'm back at my keyboard.
Thanks for this. I think it's an important thing to include in the docs. However I want to change a few things. Here's how I would write it: // button.js
const Button = ({onClick, children}) => <button onClick={onClick}>{children}</button>
// __tests__/button.js
test('calls onClick prop when clicked', () => {
const handleClick = jest.fn()
const {getByText} = render(<Button onClick={handleClick}>Click Me</Button>)
fireEvent.click(getByText(/click me/i))
expect(handleClick).toHaveBeenCalledTimes(1)
}) I'll add comments to the lines in your original sample to explain why I made the changes that I did. |
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.
Here are my notes. What do you think?
|
||
```jsx | ||
const Button = ({onClick,children}) => <button onClick={onClick}>{children}</button> | ||
describe('Button component', () => { |
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 prefer to avoid nesting. We already know that this is about the button component because we're in the file that's testing the button component.
```jsx | ||
const Button = ({onClick,children}) => <button onClick={onClick}>{children}</button> | ||
describe('Button component', () => { | ||
let onClick; |
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.
Sharing this variable through a bunch of tests makes it harder to maintain long-term. This isn't quite the same thing, but it's similar to what I talk about in Test Isolation with React.
beforeEach(() => { | ||
onClick = jest.fn(); | ||
}); | ||
afterEach(cleanup); |
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.
We can just assume that people have the cleanup stuff working in their setup.
it('Fires on click event', () => { | ||
const { container } = render( | ||
<Button | ||
label={'With Callback'} |
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.
The Button component does nothing with the label
prop so I removed that.
Trigger Alert | ||
</Button> | ||
); | ||
const input = container.querySelector('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.
We're not looking for an input, and we should avoid using and suggesting querySelector
and instead go with a more semantic selector like getByText
.
); | ||
const input = container.querySelector('button'); | ||
fireEvent.click(input); | ||
expect(onClick.mock.calls[0].length).toBe(1); |
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.
There's a built-in assertion for this, so I switched to that one.
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.
This is great. Makes more sense this way.
I think that the docs could use a similar example for onChange. Should this be a new PR? These are the examples abstracted out from what I'm actually working on. An input component with some logic inside of it:
Tests that change handler is called and that is called with the right value: // __tests__/input.js
describe("text input", () => {
afterEach(cleanup);
test("calls onChange prop when changed", () => {
const onChange = jest.fn();
const { getByTestId } = render(<Input onChange={onChange} value={""} />);
fireEvent.change(getByTestId("input-test"), {
target: { value: "New Value" }
});
expect(onChange).toHaveBeenCalledTimes(1);
});
test("When input is changed, the new value, not the event is returned by", () => {
const onChange = jest.fn();
const { getByTestId } = render(<Input onChange={onChange} value={""} />);
fireEvent.change(getByTestId("input-test"), {
target: { value: "New Value" }
});
expect(onChange).toHaveBeenCalledWith("New Value");
});
}); These are nested with |
I think that's a great idea. Let's do a different PR for the |
Sounds great. I'll copy that into a real PR, and update it to query by label or text once this is merged. |
Hi @Shelob9! Any chance you could work on Kent's feedback and update the PR? :) |
@afontcu Thanks for the nudge, this got lost in all things. I'm updating now. |
Now that I look at the issue with fresh eyes, I'm wondering if we should use a React-based example to illustrate an example in DOM Testing Library docs? We could either (1) move the example to React docs, and add demos for other frameworks, or (2) use plain DOM example with Thoughts? 🙌 |
Yeah. I don't think we should use the react stuff with DOM Testing Library. Let's make it as vanilla as possible.
I like both ideas. We should have demos/examples for all frameworks, including vanilla (document.createElement) stuff for DOM Testing Library. |
This was something I needed to do today, and it wasn't documented, so I wanted to share.