/
ArchiveModal.unit.spec.js
172 lines (140 loc) · 4.34 KB
/
ArchiveModal.unit.spec.js
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
import React from "react";
import { render, screen, waitFor } from "@testing-library/react";
import ArchiveModal from "./ArchiveModal";
const getAlert = ({ creator = getUser(), channels = [getChannel()] } = {}) => ({
creator,
channels,
created_at: "2021-05-08T02:02:07.441Z",
});
const getPulse = ({ creator = getUser(), channels = [getChannel()] } = {}) => ({
creator,
channels,
created_at: "2021-05-08T02:02:07.441Z",
});
const getUser = ({ id = 1 } = {}) => ({
id,
common_name: "John Doe",
});
const getChannel = ({
channel_type = "email",
schedule_type = "hourly",
recipients = [getUser()],
} = {}) => {
return {
channel_type,
schedule_type,
recipients,
schedule_hour: 8,
schedule_day: "mon",
schedule_frame: "first",
};
};
describe("ArchiveModal", () => {
it("should render an email alert", () => {
const alert = getAlert();
render(<ArchiveModal item={alert} type="alert" />);
screen.getByText("Delete this alert?");
screen.getByText("Yes, delete this alert");
screen.getByText("You created this alert on May 8, 2021", { exact: false });
screen.getByText("It’s currently being sent to 1 email.", { exact: false });
});
it("should render an email pulse", () => {
const pulse = getPulse();
render(<ArchiveModal item={pulse} type="pulse" />);
screen.getByText("Delete this subscription?");
screen.getByText("Yes, delete this subscription");
screen.getByText("May 8, 2021", { exact: false });
screen.getByText("It’s currently being sent to 1 email.", { exact: false });
});
it("should render a slack pulse", () => {
const pulse = getPulse({
channels: [getChannel({ channel_type: "slack" })],
});
render(<ArchiveModal item={pulse} type="pulse" />);
screen.getByText("1 Slack channel", { exact: false });
});
it("should render an alert with both email and slack channels", () => {
const alert = getAlert({
channels: [
getChannel({
channel_type: "email",
recipients: [getUser(), getUser()],
}),
getChannel({
channel_type: "slack",
recipients: [getUser(), getUser(), getUser()],
}),
],
});
render(<ArchiveModal item={alert} type="alert" />);
screen.getByText("2 emails and 3 Slack channels", { exact: false });
});
it("should render a telegram pulse", () => {
const pulse = getPulse({
channels: [getChannel({ channel_type: "telegram" })],
});
render(<ArchiveModal item={pulse} type="pulse" />);
screen.getByText("1 Telegram channel", { exact: false });
});
it("should render an alert with email, slack and telegram channels", () => {
const alert = getAlert({
channels: [
getChannel({
channel_type: "email",
recipients: [getUser(), getUser()],
}),
getChannel({
channel_type: "slack",
recipients: [getUser(), getUser(), getUser()],
}),
getChannel({
channel_type: "telegram",
recipients: [getUser(), getUser(), getUser()],
}),
],
});
render(<ArchiveModal item={alert} type="alert" />);
screen.getByText("2 emails and 3 Slack channels and 3 Telegram channels", {
exact: false,
});
});
it("should close on submit", async () => {
const alert = getAlert();
const onArchive = jest.fn();
const onClose = jest.fn();
onArchive.mockResolvedValue();
render(
<ArchiveModal
item={alert}
type="alert"
onArchive={onArchive}
onClose={onClose}
/>,
);
screen.getByText("Yes, delete this alert").click();
waitFor(() => {
expect(onArchive).toHaveBeenCalled(alert, true);
expect(onClose).toHaveBeenCalled();
});
});
it("should not close on a submit error", async () => {
const alert = getAlert();
const onArchive = jest.fn();
const onClose = jest.fn();
onArchive.mockRejectedValue({ data: { message: "An error occurred" } });
render(
<ArchiveModal
item={alert}
type="alert"
onArchive={onArchive}
onClose={onClose}
/>,
);
screen.getByText("Yes, delete this alert").click();
waitFor(() => {
screen.getByText("An error occurred");
expect(onArchive).toHaveBeenCalled(alert, true);
expect(onClose).not.toHaveBeenCalled();
});
});
});