-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNote.spec.tsx
53 lines (43 loc) · 1.74 KB
/
Note.spec.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import { MemoryRouter, Route, Routes } from "react-router-dom";
import type { Notes } from "../../api/note/schema";
import { getMockNotes, handlers } from "../../mocks/handlers";
import { server } from "../../mocks/node";
import { render, screen } from "../../test-utils";
import { Note } from "./Note";
const setup = (mockNotes: Notes) => {
/**
* Use our entire mock api so we can test the GET and PUT
* functionality
*/
server.use(...handlers);
return render(
<MemoryRouter initialEntries={[`/${mockNotes[0]?.id}`]}>
<Routes>
<Route path="/:id" element={<Note />} />
</Routes>
</MemoryRouter>,
);
};
describe("Page - Note", () => {
it("should render a note", async () => {
const mockNotes = getMockNotes();
setup(mockNotes);
expect(await screen.findByRole("button", { name: "Edit" })).toBeVisible();
expect(screen.getByRole("heading", { name: mockNotes[0]?.title })).toBeVisible();
expect(screen.getByText(mockNotes[0]?.content || "")).toBeVisible();
});
it("should allow the user to edit a note", async () => {
const mockNotes = getMockNotes();
const { user } = setup(mockNotes);
await user.click(await screen.findByRole("button", { name: "Edit" }));
expect(await screen.queryByRole("button", { name: "Edit" })).not.toBeInTheDocument();
// Fill form
await user.type(await screen.findByLabelText("Title"), "!!!");
await user.type(await screen.findByLabelText("Content"), "!!!");
await user.click(screen.getByRole("button", { name: "Update" }));
// New content
expect(await screen.findByRole("button", { name: "Edit" })).toBeVisible();
expect(screen.getByRole("heading", { name: /!!!$/ })).toBeVisible();
expect(screen.getByRole("paragraph").textContent).toEqual(expect.stringContaining("!!!"));
});
});