Skip to content

Commit 994e902

Browse files
committed
Added notin helper tests
1 parent b8d08e4 commit 994e902

File tree

2 files changed

+75
-28
lines changed

2 files changed

+75
-28
lines changed
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
const mockNotion = jest.fn();
2+
jest.mock("@notionhq/client", () => {
3+
const actual = jest.requireActual("@notionhq/client");
4+
5+
return {
6+
...actual,
7+
Client: jest.fn().mockImplementation(() => ({
8+
pages: {
9+
create: mockNotion,
10+
},
11+
})),
12+
isFullPage: jest.fn(() => true),
13+
};
14+
});
15+
16+
jest.mock("../(helpers)/slack", () => {
17+
return {
18+
notifyContactCreated: jest.fn(),
19+
};
20+
});
21+
22+
import { notifyContactCreated } from "../(helpers)/slack";
23+
import { processContact } from "../(helpers)/notion";
24+
25+
const mockSlack = notifyContactCreated as jest.Mock;
26+
27+
const mockData = {
28+
id: "123",
29+
email: "test@test.com",
30+
name: "Test Test",
31+
message: "This is a test message",
32+
databaseID: "mocked-notion-database-id",
33+
source: "Unknown",
34+
};
35+
36+
describe("Notion helper", () => {
37+
beforeEach(() => {
38+
mockNotion.mockResolvedValue({ id: "fake-page-id" });
39+
mockSlack.mockResolvedValue({ message: "success" });
40+
const { isFullPage } = require("@notionhq/client");
41+
isFullPage.mockImplementation(() => true);
42+
});
43+
44+
describe("processContact", () => {
45+
it("should call createContact and notifyContactCreated", async () => {
46+
const response = await processContact(mockData);
47+
48+
expect(response).toBe("fake-page-id");
49+
expect(mockNotion).toHaveBeenCalledTimes(1);
50+
expect(mockSlack).toHaveBeenCalledTimes(1);
51+
});
52+
});
53+
});

apps/contact/app/tests/slack.test.tsx

Lines changed: 22 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -45,39 +45,33 @@ const mockBlocks = [
4545
},
4646
];
4747

48-
describe("Slack functions", () => {
49-
it("should create expected payload", async () => {
50-
const payload = createPayload(mockData.name, mockData.email, mockData.url);
48+
describe("Slack helpers", () => {
49+
describe("createPayload", () => {
50+
it("should create expected payload", async () => {
51+
const payload = createPayload(
52+
mockData.name,
53+
mockData.email,
54+
mockData.url,
55+
);
5156

52-
expect(payload.blocks).toEqual(mockBlocks);
53-
});
54-
55-
it("should call notifyContactCreated once if conditions are met", async () => {
56-
global.fetch = jest.fn().mockResolvedValue({
57-
status: 200,
57+
expect(payload.blocks).toEqual(mockBlocks);
5858
});
59-
60-
await notifyContactCreated(mockData.name, mockData.email, mockData.url);
61-
const [[url, options]] = (global.fetch as jest.Mock).mock.calls;
62-
const data = JSON.parse(options.body);
63-
const blocks = JSON.stringify(data.blocks);
64-
65-
expect(url).toBe("https://slack.com/api/chat.postMessage");
66-
expect(blocks).toMatch(JSON.stringify(mockBlocks));
67-
expect(fetch).toHaveBeenCalledTimes(1);
6859
});
6960

70-
it("should throw error if fetch fails", async () => {
71-
global.fetch = jest.fn().mockResolvedValue({
72-
status: 401,
73-
});
61+
describe("notifyContactCreated", () => {
62+
it("should send message on slack with correct payload", async () => {
63+
global.fetch = jest.fn().mockResolvedValue({
64+
status: 200,
65+
});
66+
67+
await notifyContactCreated(mockData.name, mockData.email, mockData.url);
68+
const [[url, options]] = (global.fetch as jest.Mock).mock.calls;
69+
const body = JSON.parse(options.body);
70+
const blocks = JSON.stringify(body.blocks);
7471

75-
await expect(
76-
notifyContactCreated(mockData.name, mockData.email, mockData.url),
77-
).rejects.toEqual({
78-
body: "Could not send notification message to Slack",
79-
statusCode: 401,
72+
expect(url).toBe("https://slack.com/api/chat.postMessage");
73+
expect(blocks).toMatch(JSON.stringify(mockBlocks));
74+
expect(fetch).toHaveBeenCalledTimes(1);
8075
});
81-
expect(fetch).toHaveBeenCalledTimes(1);
8276
});
8377
});

0 commit comments

Comments
 (0)