-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdelete-stage-race.test.tsx
80 lines (76 loc) · 2.42 KB
/
delete-stage-race.test.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import {
fireEvent,
screen,
waitForElementToBeRemoved,
waitFor,
within,
} from "@testing-library/react";
import { mockAdapter, mockDeleteStageRace, setupApp } from "./utils";
const stageRaces = [
{
id: 1,
name: "Stage Race 1",
stages: [
{
id: "klsa2dr6",
name: "Stage 1",
date: "2020-01-01",
},
],
},
{
id: 2,
name: "Stage Race 2",
stages: [
{
id: "klsa3ny2",
name: "Stage 1",
date: "2021-01-01",
},
],
},
];
beforeEach(() => {
mockAdapter.reset();
});
test("calls DELETE '/stage-races/{id}' on 'Delete' button click and handles success", async () => {
await setupApp({ stageRaces });
// initial list items (stage races 1 & 2)
expect(screen.getByTestId("stage-race-1")).toBeInTheDocument();
expect(screen.getByTestId("stage-race-2")).toBeInTheDocument();
// delete stage race 1
mockDeleteStageRace("1");
fireEvent.click(
within(screen.getByTestId("stage-race-1")).getByText("Delete")
);
// subsequent list items (stage race 1 only)
await waitForElementToBeRemoved(() => screen.getByTestId("stage-race-1"));
expect(screen.getByTestId("stage-race-2")).toBeInTheDocument();
// DELETE '/stage-races/1' was called once
expect(mockAdapter.history.delete.length).toBe(1);
expect(mockAdapter.history.delete[0].url).toBe("/stage-races/1");
});
test("calls DELETE '/stage-races/{id}' on 'Delete' button click and handles error", async () => {
await setupApp({ stageRaces });
// initial list items (stage races 1 & 2)
expect(screen.getByTestId("stage-race-1")).toBeInTheDocument();
expect(screen.getByTestId("stage-race-2")).toBeInTheDocument();
// attempt to delete stage race 2
mockDeleteStageRace("2", 500);
fireEvent.click(
within(screen.getByTestId("stage-race-2")).getByText("Delete")
);
// error renders
await waitFor(() => screen.getByText("Error deleting stage race"));
// error can be cleared
fireEvent.click(screen.getByLabelText("Clear error"));
expect(
screen.queryByText("Error deleting stage race")
).not.toBeInTheDocument();
// subsequent list items (still stage races 1 & 2)
expect(screen.getByTestId("stage-race-1")).toBeInTheDocument();
expect(screen.getByTestId("stage-race-2")).toBeInTheDocument();
// DELETE '/stage-races/2' was called once
expect(mockAdapter.history.delete.length).toBe(1);
expect(mockAdapter.history.delete[0].url).toBe("/stage-races/2");
});