Skip to content

Commit 8581ce1

Browse files
committed
Moved Jest tests to same folder as playwright tests, added new scripts to run from root
1 parent a94596c commit 8581ce1

File tree

7 files changed

+128
-5
lines changed

7 files changed

+128
-5
lines changed

apps/contact/tsconfig.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,6 @@
2424
},
2525
"types": ["jest", "node"]
2626
},
27-
"include": ["next-env.d.ts", "**/*.ts", "**/*.tsx", ".next/types/**/*.ts"],
27+
"include": ["next-env.d.ts", "**/*.ts", "**/*.tsx", ".next/types/**/*.ts", "../../tests/jest.setup.ts"],
2828
"exclude": ["node_modules"]
2929
}

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@
66
"dev": "turbo dev",
77
"lint": "turbo lint",
88
"format": "prettier --write \"**/*.{ts,tsx,astro}\"",
9-
"test:playwright": "cd tests && npx playwright test"
9+
"test:jest": "cd tests && npx jest",
10+
"test:playwright": "cd tests && npx playwright test",
11+
"test": "npm run test:jest && npm run test:playwright"
1012
},
1113
"devDependencies": {
1214
"@playwright/test": "^1.55.0",
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ jest.mock("@notionhq/client", () => {
1717
};
1818
});
1919

20-
jest.mock("../../app/(helpers)/slack", () => {
20+
jest.mock("../../apps/contact/app/(helpers)/slack", () => {
2121
return {
2222
notifyContactCreated: jest.fn(),
2323
};
@@ -26,8 +26,8 @@ jest.mock("../../app/(helpers)/slack", () => {
2626
process.env.NOTION_DATABASE_ID = "mocked-notion-database-id";
2727

2828
import { NextRequest } from "next/server";
29-
import { POST } from "../../app/api/contact/route";
30-
import { notifyContactCreated } from "../../app/(helpers)/slack";
29+
import { POST } from "../../apps/contact/app/api/contact/route";
30+
import { notifyContactCreated } from "../../apps/contact/app/(helpers)/slack";
3131

3232
const mockSlack = notifyContactCreated as jest.Mock;
3333

tests/jest/slack.test.tsx

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
import {
2+
notifyContactCreated,
3+
createPayload,
4+
} from "../../apps/contact/app/(helpers)/slack";
5+
6+
const mockData = {
7+
name: "Test",
8+
email: "test@test.com",
9+
url: "https://www.test.dev/",
10+
};
11+
12+
const mockBlocks = [
13+
{
14+
type: "header",
15+
text: {
16+
type: "plain_text",
17+
text: "We have 1 new message(s).",
18+
emoji: true,
19+
},
20+
},
21+
{
22+
type: "section",
23+
text: {
24+
type: "mrkdwn",
25+
text: `We got a new message from _${mockData.name}_ (_${mockData.email}_).`,
26+
},
27+
},
28+
{
29+
type: "divider",
30+
},
31+
{
32+
type: "section",
33+
text: {
34+
type: "mrkdwn",
35+
text: " ",
36+
},
37+
accessory: {
38+
type: "button",
39+
text: {
40+
type: "plain_text",
41+
text: "Show me the message",
42+
emoji: true,
43+
},
44+
value: "new_message_click",
45+
url: mockData.url,
46+
action_id: "button-action",
47+
},
48+
},
49+
];
50+
51+
describe("Slack functions", () => {
52+
it("should create expected payload", async () => {
53+
const payload = createPayload(mockData.name, mockData.email, mockData.url);
54+
55+
expect(payload.blocks).toEqual(mockBlocks);
56+
});
57+
58+
it("should call notifyContactCreated once if conditions are met", async () => {
59+
global.fetch = jest.fn().mockResolvedValue({
60+
status: 200,
61+
});
62+
63+
await notifyContactCreated(mockData.name, mockData.email, mockData.url);
64+
const [[url, options]] = (global.fetch as jest.Mock).mock.calls;
65+
const data = JSON.parse(options.body);
66+
const blocks = JSON.stringify(data.blocks);
67+
68+
expect(url).toBe("https://slack.com/api/chat.postMessage");
69+
expect(blocks).toMatch(JSON.stringify(mockBlocks));
70+
expect(fetch).toHaveBeenCalledTimes(1);
71+
});
72+
73+
it("should throw error if fetch fails", async () => {
74+
global.fetch = jest.fn().mockResolvedValue({
75+
status: 401,
76+
});
77+
78+
await expect(
79+
notifyContactCreated(mockData.name, mockData.email, mockData.url),
80+
).rejects.toEqual({
81+
body: "Could not send notification message to Slack",
82+
statusCode: 401,
83+
});
84+
expect(fetch).toHaveBeenCalledTimes(1);
85+
});
86+
});

tests/tsconfig.json

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
{
2+
"compilerOptions": {
3+
"target": "es2020",
4+
"lib": ["dom", "dom.iterable", "esnext"],
5+
"allowJs": true,
6+
"skipLibCheck": true,
7+
"strict": true,
8+
"noEmit": true,
9+
"esModuleInterop": true,
10+
"module": "esnext",
11+
"moduleResolution": "bundler",
12+
"resolveJsonModule": true,
13+
"isolatedModules": true,
14+
"jsx": "preserve",
15+
"forceConsistentCasingInFileNames": true,
16+
"incremental": true,
17+
"plugins": [
18+
{
19+
"name": "next"
20+
}
21+
],
22+
"paths": {
23+
"@/*": ["./*"]
24+
},
25+
"types": ["jest", "node"]
26+
},
27+
"include": [
28+
"next-env.d.ts",
29+
"**/*.ts",
30+
"**/*.tsx",
31+
".next/types/**/*.ts",
32+
"../../tests/jest.setup.ts"
33+
],
34+
"exclude": ["node_modules"]
35+
}

0 commit comments

Comments
 (0)