Skip to content

Commit

Permalink
Merge pull request #9 from theapexlab/test-refactors
Browse files Browse the repository at this point in the history
Test refactors
  • Loading branch information
BaDo2001 authored Nov 3, 2023
2 parents bec4688 + 72c4ef2 commit 8e383e8
Show file tree
Hide file tree
Showing 22 changed files with 178 additions and 158 deletions.
19 changes: 19 additions & 0 deletions .eslintrc.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,23 @@ module.exports = defineConfig({
"import/order": "off",
"import/prefer-default-export": "off",
},
overrides: [
{
files: ["*.test.ts"],
rules: {
"simple-import-sort/imports": [
"error",
{
groups: [
["^\\u0000"],
["^@?\\w"],
["^@\\/"],
["^\\.\\.(?!/?$)", "^\\.\\./?$"],
["^\\./(?=.*/)(?!/?$)", "^\\.(?!/?$)", "^\\./?$"],
],
},
],
},
},
],
});
12 changes: 3 additions & 9 deletions tests/integration/iceBreakerQuestions.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { App } from "@slack/bolt";
import { eq } from "drizzle-orm";
import { afterEach, beforeAll, describe, expect, it, vi } from "vitest";

Expand All @@ -13,19 +12,14 @@ import {
import { testDb } from "@/testUtils/testDb";
import { waitForPostInRandom } from "@/testUtils/waitForPostInRandomChannel";

const app = new App({
signingSecret: import.meta.env.VITE_SLACK_SIGNING_SECRET,
token: import.meta.env.VITE_SLACK_BOT_TOKEN,
});

describe("Icebreaker questions", () => {
beforeAll(async () => {
await testDb.delete(users);
await testDb.delete(iceBreakerThreads);
}, 10_000);

afterEach(async () => {
await deleteLastRandomChannelPost(app);
await deleteLastRandomChannelPost();
await testDb.delete(users);
await testDb.delete(iceBreakerThreads);
}, 10_000);
Expand All @@ -39,7 +33,7 @@ describe("Icebreaker questions", () => {
`${import.meta.env.VITE_API_URL}/icebreaker?eventId=${eventId}`,
);

const message = await waitForPostInRandom(app, eventId);
const message = await waitForPostInRandom(eventId);

expect(message.blocks?.length).toBe(1);
},
Expand All @@ -57,7 +51,7 @@ describe("Icebreaker questions", () => {
`${import.meta.env.VITE_API_URL}/icebreaker?eventId=${eventId}`,
);

const message = await waitForPostInRandom(app, eventId);
const message = await waitForPostInRandom(eventId);

expect(message.blocks?.length).toBe(2);

Expand Down
23 changes: 14 additions & 9 deletions tests/integration/slackEvents.test.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import { App } from "@slack/bolt";
import { and, eq } from "drizzle-orm";
import { afterEach, beforeAll, describe, expect, it, vi } from "vitest";

import { users } from "@/db/schema";
import { timeout } from "@/testUtils/constants";
import { deleteLastDmMessage } from "@/testUtils/deleteLastDmMessage";
import { sendMockSlackEvent } from "@/testUtils/sendMockSlackEvent";
import { testDb } from "@/testUtils/testDb";
import { waitForDm } from "@/testUtils/waitForDm";
import type { SlackCallbackRequest } from "@/types/SlackEventRequest";

const constants = vi.hoisted(() => ({
challenge: "challenge",
Expand All @@ -16,18 +15,24 @@ const constants = vi.hoisted(() => ({
userId: "U1",
}));

const app = new App({
signingSecret: import.meta.env.VITE_SLACK_SIGNING_SECRET,
token: import.meta.env.VITE_SLACK_BOT_TOKEN,
});
export const sendMockSlackEvent = async (body: SlackCallbackRequest) =>
fetch(`${import.meta.env.VITE_API_URL}/slack/event`, {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify(body),
})
.then((res) => res.json())
.catch((error) => console.error(error.stack));

describe("Slack events", () => {
beforeAll(async () => {
await testDb.delete(users);
}, 10_000);

afterEach(async () => {
await deleteLastDmMessage(app);
await deleteLastDmMessage();
await testDb.delete(users);
}, 10_000);

Expand Down Expand Up @@ -58,7 +63,7 @@ describe("Slack events", () => {
event_id: eventId,
});

await waitForDm(app, eventId);
await waitForDm(eventId);
},
timeout,
);
Expand All @@ -79,7 +84,7 @@ describe("Slack events", () => {
event_id: eventId,
});

await waitForDm(app, eventId);
await waitForDm(eventId);
},
timeout,
);
Expand Down
27 changes: 21 additions & 6 deletions tests/integration/slackInteractions.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { App } from "@slack/bolt";
import { eq } from "drizzle-orm";
import { afterAll, beforeEach, describe, expect, it, vi } from "vitest";

Expand All @@ -7,8 +6,9 @@ import { constructAskBirthdayMessageReplacement } from "@/services/slack/constru
import { constructBirthdayConfirmedMessage } from "@/services/slack/constructBirthdayConfirmedMessage";
import { constructConfirmBirthdayMessage } from "@/services/slack/constructConfirmBirthdayMessage";
import { timeout } from "@/testUtils/constants";
import { sendMockSlackInteraction } from "@/testUtils/sendMockSlackInteraction";
import { testDb, waitForTestItem } from "@/testUtils/testDb";
import { app } from "@/testUtils/testSlackApp";
import type { SlackInteractionRequest } from "@/types/SlackInteractionRequest";

const constants = vi.hoisted(() => ({
responseUrl: import.meta.env.VITE_API_URL + "/slack/test-payload",
Expand All @@ -17,10 +17,25 @@ const constants = vi.hoisted(() => ({
userId: "U1",
}));

const app = new App({
signingSecret: import.meta.env.VITE_SLACK_SIGNING_SECRET,
token: import.meta.env.VITE_SLACK_BOT_TOKEN,
});
export const sendMockSlackInteraction = async (
body: SlackInteractionRequest,
) => {
const urlEncodedBody = new URLSearchParams({
payload: JSON.stringify(body),
});

const encodedBody = urlEncodedBody.toString();

return fetch(`${import.meta.env.VITE_API_URL}/slack/interaction`, {
method: "POST",
headers: {
"Content-Type": "application/x-www-form-urlencoded",
},
body: encodedBody,
})
.then((res) => res.json())
.catch((error) => console.error(error.stack));
};

describe("Slack interactions", () => {
beforeEach(async () => {
Expand Down
12 changes: 2 additions & 10 deletions tests/unit/askBirthday.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import "@/testUtils/mocks/mockSlackApp";

import type { Mock } from "vitest";
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";

Expand All @@ -15,16 +17,6 @@ const constants = vi.hoisted(() => ({
eventId: "E001",
}));

vi.mock("@/services/slack/createSlackApp", () => ({
createSlackApp: vi.fn().mockReturnValue({
client: {
chat: {
postMessage: vi.fn(),
},
},
}),
}));

vi.mock("@/services/slack/getUserInfo", async () => ({
getUserInfo: vi.fn().mockResolvedValue({
user: {
Expand Down
16 changes: 3 additions & 13 deletions tests/unit/botJoined.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import "@/testUtils/mocks/mockEventBridge";

import {
EventBridgeClient,
PutEventsCommand,
Expand All @@ -6,7 +8,7 @@ import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";

import type { Events } from "@/events";
import { handler } from "@/functions/events/botJoined";
import { mockEventBridgePayload } from "@/testUtils/mockEventBridgePayload";
import { mockEventBridgePayload } from "@/testUtils/mocks/mockEventBridgePayload";
import { sendMockSqsMessage } from "@/testUtils/sendMockSqsMessage";

const constants = vi.hoisted(() => ({
Expand All @@ -15,18 +17,6 @@ const constants = vi.hoisted(() => ({
eventId: "E001",
}));

vi.mock("@aws-sdk/client-eventbridge", async () => {
const EventBridgeClient = vi.fn();
EventBridgeClient.prototype.send = vi.fn();

const PutEventsCommand = vi.fn();

return {
EventBridgeClient,
PutEventsCommand,
};
});

vi.mock("@/services/slack/getChannelMembers", () => ({
getChannelMembers: vi.fn().mockResolvedValue(constants.channelMembers),
}));
Expand Down
8 changes: 3 additions & 5 deletions tests/unit/getBirthdaysBetween.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import "@/testUtils/mocks/mockDb";

import dayjs from "dayjs";
import { afterEach, beforeAll, describe, expect, it, vi } from "vitest";
import { afterEach, beforeAll, describe, expect, it } from "vitest";

import { getBirthdaysBetween } from "@/db/queries/getBirthdays";
import { users } from "@/db/schema";
Expand All @@ -11,10 +13,6 @@ import {
import { testCases } from "@/testUtils/iceBreakerTestCases";
import { testDb } from "@/testUtils/testDb";

vi.mock("@/db/index", async () => ({
db: await import("@/testUtils/testDb").then(({ testDb }) => testDb),
}));

describe("Get birthdays between", () => {
beforeAll(async () => {
await testDb.delete(users);
Expand Down
16 changes: 3 additions & 13 deletions tests/unit/memberJoinedChannel.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import "@/testUtils/mocks/mockEventBridge";

import {
EventBridgeClient,
PutEventsCommand,
Expand All @@ -8,7 +10,7 @@ import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
import type { Events } from "@/events";
import { handler } from "@/functions/events/memberJoinedChannel";
import { getUserInfo } from "@/services/slack/getUserInfo";
import { mockEventBridgePayload } from "@/testUtils/mockEventBridgePayload";
import { mockEventBridgePayload } from "@/testUtils/mocks/mockEventBridgePayload";
import { sendMockSqsMessage } from "@/testUtils/sendMockSqsMessage";

const constants = vi.hoisted(() => ({
Expand All @@ -20,18 +22,6 @@ const constants = vi.hoisted(() => ({
eventId: "E001",
}));

vi.mock("@aws-sdk/client-eventbridge", async () => {
const EventBridgeClient = vi.fn();
EventBridgeClient.prototype.send = vi.fn();

const PutEventsCommand = vi.fn();

return {
EventBridgeClient,
PutEventsCommand,
};
});

vi.mock("@/services/slack/getBotUserId", () => ({
getBotUserId: vi.fn().mockResolvedValue(constants.birthdayBotUserId),
}));
Expand Down
35 changes: 21 additions & 14 deletions tests/unit/slackEvent.test.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,21 @@
import "@/testUtils/mocks/mockEventBridge";

import {
EventBridgeClient,
PutEventsCommand,
} from "@aws-sdk/client-eventbridge";
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";

import { callWithMockSlackEvent } from "@/testUtils/callWithMockSlackEvent";
import { mockEventBridgePayload } from "@/testUtils/mockEventBridgePayload";
import type { SlackEvent } from "@/types/SlackEventRequest";
import { handler } from "@/functions/lambdas/slack-event";
import { mockEventBridgePayload } from "@/testUtils/mocks/mockEventBridgePayload";
import {
mockLambdaContext,
mockLambdaEvent,
} from "@/testUtils/mocks/mockLambdaPayload";
import type {
SlackCallbackRequest,
SlackEvent,
} from "@/types/SlackEventRequest";

const constants = vi.hoisted(() => ({
challenge: "challenge",
Expand All @@ -16,17 +25,15 @@ const constants = vi.hoisted(() => ({
teamId: "T001",
}));

vi.mock("@aws-sdk/client-eventbridge", async () => {
const EventBridgeClient = vi.fn();
EventBridgeClient.prototype.send = vi.fn();

const PutEventsCommand = vi.fn();

return {
EventBridgeClient,
PutEventsCommand,
};
});
export const callWithMockSlackEvent = async (body: SlackCallbackRequest) =>
handler(
{
...mockLambdaEvent,
body: JSON.stringify(body),
},
mockLambdaContext,
() => {},
);

describe("Handle slack events", () => {
let eventBridge: EventBridgeClient;
Expand Down
42 changes: 4 additions & 38 deletions tests/utils/callWithMockSlackEvent.ts
Original file line number Diff line number Diff line change
@@ -1,48 +1,14 @@
import { handler } from "@/functions/lambdas/slack-event";
import type { SlackCallbackRequest } from "@/types/SlackEventRequest";

import { mockLambdaContext, mockLambdaEvent } from "./mocks/mockLambdaPayload";

export const callWithMockSlackEvent = async (body: SlackCallbackRequest) =>
handler(
{
...mockLambdaEvent,
body: JSON.stringify(body),
headers: {},
isBase64Encoded: false,
rawPath: "",
rawQueryString: "",
requestContext: {
accountId: "",
apiId: "",
domainName: "",
domainPrefix: "",
http: {
method: "",
path: "",
protocol: "",
sourceIp: "",
userAgent: "",
},
requestId: "",
routeKey: "",
stage: "",
time: "",
timeEpoch: 0,
},
routeKey: "",
version: "",
},
{
awsRequestId: "",
callbackWaitsForEmptyEventLoop: false,
functionName: "",
functionVersion: "",
invokedFunctionArn: "",
logGroupName: "",
logStreamName: "",
memoryLimitInMB: "",
getRemainingTimeInMillis: () => 0,
done: () => {},
fail: () => {},
succeed: () => {},
},
mockLambdaContext,
() => {},
);
Loading

0 comments on commit 8e383e8

Please sign in to comment.