-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcontactApi.test.tsx
More file actions
187 lines (157 loc) · 5.25 KB
/
contactApi.test.tsx
File metadata and controls
187 lines (157 loc) · 5.25 KB
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
const mockNotion = jest.fn();
jest.mock("@notionhq/client", () => {
const actual = jest.requireActual("@notionhq/client");
return {
...actual,
Client: jest.fn().mockImplementation(() => ({
pages: {
create: mockNotion,
},
})),
isFullPage: jest.fn(() => true),
};
});
jest.mock("../../helpers/slack", () => {
return {
notifyContactCreated: jest.fn(),
};
});
process.env.NOTION_DATABASE_ID = "mocked-notion-database-id";
import { NextRequest } from "next/server";
import { POST } from "../api/contact/route";
import { notifyContactCreated } from "../../helpers/slack";
const mockSlack = notifyContactCreated as jest.Mock;
const mockBody = {
name: "Test",
email: "test@example.com",
message: "This is a test",
hasConsent: true,
};
const mockInvalidBody = {
name: "",
email: "test@example.com",
message: "",
hasConsent: true,
};
const mockNoConsent = {
name: "Test No Consent",
email: "testNoConsent@example.com",
message: "This is a no consent test",
hasConsent: false,
};
describe("POST /api/contact", () => {
beforeEach(() => {
mockNotion.mockResolvedValue({
id: "fake-page-id",
url: "https://www.notion.so/Message-from-Test-123456789-fakepageid",
});
mockSlack.mockResolvedValue({ message: "success" });
const { isFullPage } = require("@notionhq/client");
isFullPage.mockImplementation(() => true);
});
afterAll(() => {
delete process.env.NOTION_DATABASE_ID;
});
it("should call Notion, Slack and return 200 response on success", async () => {
const request = new NextRequest("http://localhost", {
method: "POST",
body: JSON.stringify(mockBody),
headers: {
"Content-Type": "application/json",
},
});
const response = await POST(request);
const data = await response.json();
expect(mockNotion).toHaveBeenCalledTimes(1);
expect(mockSlack).toHaveBeenCalledTimes(1);
expect(response.status).toBe(200);
expect(data).toEqual({ message: "Success" });
});
it("shouldn't call Notion or Slack and return 400 response when incorect header Content-Type is set", async () => {
const request = new NextRequest("http://localhost", {
method: "POST",
body: JSON.stringify(mockBody),
headers: {
"Content-Type": "",
},
});
const response = await POST(request);
expect(response.status).toBe(400);
expect(mockNotion).toHaveBeenCalledTimes(0);
expect(mockSlack).toHaveBeenCalledTimes(0);
});
it("should call Notion but not Slack if isFullPage is false", async () => {
const { isFullPage } = require("@notionhq/client");
isFullPage.mockImplementation(() => false);
const request = new NextRequest("http://localhost", {
method: "POST",
body: JSON.stringify(mockBody),
headers: {
"Content-Type": "application/json",
},
});
const response = await POST(request);
const data = await response.json();
expect(mockNotion).toHaveBeenCalledTimes(1);
expect(mockSlack).toHaveBeenCalledTimes(0);
expect(response.status).toBe(501);
expect(data).toEqual({ message: "Failed to create notion page" });
});
it("should call Notion but not Slack if the page is not created", async () => {
mockNotion.mockResolvedValue({ id: undefined });
const request = new NextRequest("http://localhost", {
method: "POST",
body: JSON.stringify(mockBody),
headers: {
"Content-Type": "application/json",
},
});
const response = await POST(request);
const data = await response.json();
expect(mockNotion).toHaveBeenCalledTimes(1);
expect(mockSlack).toHaveBeenCalledTimes(0);
expect(response.status).toBe(501);
expect(data).toEqual({ message: "Failed to create notion page" });
});
it("shouldn't call Notion or Slack and return 400 response when no body was passed", async () => {
const request = new NextRequest("http://localhost", {
method: "POST",
body: JSON.stringify({}),
headers: {
"Content-Type": "application/json",
},
});
const response = await POST(request);
expect(response.status).toBe(400);
expect(mockNotion).toHaveBeenCalledTimes(0);
expect(mockSlack).toHaveBeenCalledTimes(0);
});
it("shouldn't call Notion or Slack and return 400 response when invalid body was passed", async () => {
const request = new NextRequest("http://localhost", {
method: "POST",
body: JSON.stringify(mockInvalidBody),
headers: {
"Content-Type": "application/json",
},
});
const response = await POST(request);
expect(response.status).toBe(400);
expect(mockNotion).toHaveBeenCalledTimes(0);
expect(mockSlack).toHaveBeenCalledTimes(0);
});
it("should return 403 response when no consent from user", async () => {
const request = new NextRequest("http://localhost", {
method: "POST",
body: JSON.stringify(mockNoConsent),
headers: {
"Content-Type": "application/json",
},
});
const response = await POST(request);
const data = await response.json();
expect(response.status).toBe(403);
expect(data).toEqual({ message: "No consent by user" });
expect(mockNotion).toHaveBeenCalledTimes(0);
expect(mockSlack).toHaveBeenCalledTimes(0);
});
});