Skip to content

Commit 9d323c0

Browse files
authored
React: Update vitest imports (#28270)
* chore: Update vitest imports * chore: Refactor CustomButton test with proper indentations
1 parent 5084af2 commit 9d323c0

File tree

1 file changed

+19
-19
lines changed

1 file changed

+19
-19
lines changed

react/react_testing/mocking_callbacks_and_components.md

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -37,38 +37,38 @@ Nothing fancy. `CustomButton` is a component with a prop passed in. We're intere
3737
```jsx
3838
// CustomButton.test.jsx
3939

40-
import { vi } from 'vitest'
40+
import { vi, describe, it, expect } from 'vitest'
4141
import { render, screen } from "@testing-library/react";
4242
import userEvent from "@testing-library/user-event";
4343
import CustomButton from "./CustomButton";
4444

4545
describe("CustomButton", () => {
46-
it("should render a button with the text 'Click me'", () => {
47-
render(<CustomButton onClick={() => {}} />);
46+
it("should render a button with the text 'Click me'", () => {
47+
render(<CustomButton onClick={() => {}} />);
4848

49-
const button = screen.getByRole("button", { name: "Click me" });
49+
const button = screen.getByRole("button", { name: "Click me" });
5050

51-
expect(button).toBeInTheDocument();
52-
});
51+
expect(button).toBeInTheDocument();
52+
});
5353

54-
it("should call the onClick function when clicked", async () => {
55-
const onClick = vi.fn();
56-
const user = userEvent.setup()
57-
render(<CustomButton onClick={onClick} />);
54+
it("should call the onClick function when clicked", async () => {
55+
const onClick = vi.fn();
56+
const user = userEvent.setup()
57+
render(<CustomButton onClick={onClick} />);
5858

59-
const button = screen.getByRole("button", { name: "Click me" });
59+
const button = screen.getByRole("button", { name: "Click me" });
6060

61-
await user.click(button);
61+
await user.click(button);
6262

63-
expect(onClick).toHaveBeenCalled();
64-
});
63+
expect(onClick).toHaveBeenCalled();
64+
});
6565

66-
it("should not call the onClick function when it isn't clicked", async () => {
67-
const onClick = vi.fn();
68-
render(<CustomButton onClick={onClick} />);
66+
it("should not call the onClick function when it isn't clicked", async () => {
67+
const onClick = vi.fn();
68+
render(<CustomButton onClick={onClick} />);
6969

70-
expect(onClick).not.toHaveBeenCalled();
71-
});
70+
expect(onClick).not.toHaveBeenCalled();
71+
});
7272
});
7373
```
7474

0 commit comments

Comments
 (0)