Skip to content

Commit

Permalink
Merge pull request #16 from theapexlab/test-restructure
Browse files Browse the repository at this point in the history
Restructure test util files
  • Loading branch information
BaDo2001 authored Nov 9, 2023
2 parents 0c06f93 + dc6311b commit 16d211b
Show file tree
Hide file tree
Showing 29 changed files with 92 additions and 108 deletions.
20 changes: 10 additions & 10 deletions tests/integration/askBirthday.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ import { testItems, users } from "@/db/schema";
import { constructAskBirthdayMessageReplacement } from "@/services/slack/constructAskBirthdayMessage";
import { constructBirthdayConfirmedMessage } from "@/services/slack/constructBirthdayConfirmedMessage";
import { constructConfirmBirthdayMessage } from "@/services/slack/constructConfirmBirthdayMessage";
import { timeout, waitTimeout } from "@/testUtils/constants";
import { deleteLastDm } from "@/testUtils/deleteLastDm";
import { sendMockSlackInteraction } from "@/testUtils/sendMockSlackInteraction";
import { pollInterval, timeout, waitTimeout } from "@/testUtils/constants";
import { deleteLastDm } from "@/testUtils/integration/deleteLastDm";
import { sendSlackInteraction } from "@/testUtils/integration/sendSlackInteraction";
import { app } from "@/testUtils/integration/testSlackApp";
import { waitForDm } from "@/testUtils/integration/waitForDm";
import { testDb, waitForTestItem } from "@/testUtils/testDb";
import { app } from "@/testUtils/testSlackApp";
import { waitForDm } from "@/testUtils/waitForDm";
import {
birthdayConfirmActionId,
birthdayIncorrectActionId,
Expand Down Expand Up @@ -41,7 +41,7 @@ describe("Slack interactions", () => {
async () => {
const eventId = "AB1_" + Date.now().toString();

await sendMockSlackInteraction({
await sendSlackInteraction({
type: "block_actions",
user: {
id: constants.userId,
Expand Down Expand Up @@ -93,7 +93,7 @@ describe("Slack interactions", () => {
async () => {
const eventId = "AB3_" + Date.now().toString();

await sendMockSlackInteraction({
await sendSlackInteraction({
type: "block_actions",
user: {
id: constants.userId,
Expand Down Expand Up @@ -122,7 +122,7 @@ describe("Slack interactions", () => {
it(
"Should save user to db when birthday is confirmed",
async () => {
await sendMockSlackInteraction({
await sendSlackInteraction({
type: "block_actions",
user: {
id: constants.userId,
Expand Down Expand Up @@ -153,7 +153,7 @@ describe("Slack interactions", () => {
},
{
timeout: waitTimeout,
interval: 1_000,
interval: pollInterval,
},
);

Expand All @@ -171,7 +171,7 @@ describe("Slack interactions", () => {
async () => {
const eventId = "AB4_" + Date.now().toString();

await sendMockSlackInteraction({
await sendSlackInteraction({
type: "block_actions",
user: {
id: import.meta.env.VITE_SLACK_USER_ID,
Expand Down
12 changes: 6 additions & 6 deletions tests/integration/askPresentIdeas.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ import { afterEach, beforeAll, describe, expect, it, vi } from "vitest";

import { presentIdeas, testItems, users } from "@/db/schema";
import { constructPresentIdeaSavedMessage } from "@/services/slack/constructPresentIdeaSavedMessage";
import { timeout, waitTimeout } from "@/testUtils/constants";
import { deleteLastDm } from "@/testUtils/deleteLastDm";
import { sendMockSlackInteraction } from "@/testUtils/sendMockSlackInteraction";
import { pollInterval, timeout, waitTimeout } from "@/testUtils/constants";
import { deleteLastDm } from "@/testUtils/integration/deleteLastDm";
import { sendSlackInteraction } from "@/testUtils/integration/sendSlackInteraction";
import { waitForDm } from "@/testUtils/integration/waitForDm";
import { testDb, waitForTestItem } from "@/testUtils/testDb";
import { waitForDm } from "@/testUtils/waitForDm";
import {
presentIdeasInputActionId,
presentIdeasSaveButtonBlockId,
Expand Down Expand Up @@ -99,7 +99,7 @@ describe("Present ideas", () => {

const eventId = "PI2_" + Date.now().toString();

await sendMockSlackInteraction({
await sendSlackInteraction({
type: "block_actions",
user: {
id: constants.userId,
Expand Down Expand Up @@ -142,7 +142,7 @@ describe("Present ideas", () => {
},
{
timeout: waitTimeout,
interval: 1_000,
interval: pollInterval,
},
);

Expand Down
28 changes: 24 additions & 4 deletions tests/integration/iceBreakerQuestions.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,40 @@ import { eq } from "drizzle-orm";
import { afterEach, beforeAll, describe, expect, it, vi } from "vitest";

import { iceBreakerThreads, users } from "@/db/schema";
import { timeout, waitTimeout } from "@/testUtils/constants";
import { deleteLastRandomChannelPost } from "@/testUtils/deleteLastRandomChannelPost";
import { pollInterval, timeout, waitTimeout } from "@/testUtils/constants";
import {
generateIceBreakerTestUsers,
usersInWindow,
usersOutsideWindow,
} from "@/testUtils/generateIceBreakerTestUsers";
import { app } from "@/testUtils/integration/testSlackApp";
import { waitForPostInRandom } from "@/testUtils/integration/waitForPostInRandom";
import { testDb } from "@/testUtils/testDb";
import { waitForPostInRandom } from "@/testUtils/waitForPostInRandomChannel";

const constants = vi.hoisted(() => ({
teamId: "T1",
}));

export const deleteLastRandomChannelPost = async () => {
const chat = await app.client.conversations.history({
channel: import.meta.env.VITE_RANDOM_SLACK_CHANNEL_ID,
limit: 1,
});

await Promise.all(
chat.messages?.map(async (message) => {
if (!message.ts) {
return;
}

await app.client.chat.delete({
channel: import.meta.env.VITE_RANDOM_SLACK_CHANNEL_ID,
ts: message.ts,
});
}) ?? [],
);
};

describe("Icebreaker questions", () => {
beforeAll(async () => {
await testDb.delete(users);
Expand Down Expand Up @@ -107,7 +127,7 @@ describe("Icebreaker questions", () => {
},
{
timeout: waitTimeout,
interval: 1_000,
interval: pollInterval,
},
);

Expand Down
18 changes: 9 additions & 9 deletions tests/integration/slackEvents.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ import { and, eq } from "drizzle-orm";
import { afterEach, beforeAll, describe, expect, it, vi } from "vitest";

import { users } from "@/db/schema";
import { timeout, waitTimeout } from "@/testUtils/constants";
import { deleteLastDm } from "@/testUtils/deleteLastDm";
import { pollInterval, timeout, waitTimeout } from "@/testUtils/constants";
import { deleteLastDm } from "@/testUtils/integration/deleteLastDm";
import { waitForDm } from "@/testUtils/integration/waitForDm";
import { testDb } from "@/testUtils/testDb";
import { waitForDm } from "@/testUtils/waitForDm";
import type { SlackCallbackRequest } from "@/types/SlackEventRequest";

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

export const sendMockSlackEvent = async (body: SlackCallbackRequest) =>
export const sendSlackEvent = async (body: SlackCallbackRequest) =>
fetch(`${import.meta.env.VITE_API_URL}/slack/event`, {
method: "POST",
headers: {
Expand All @@ -39,7 +39,7 @@ describe("Slack events", () => {
it(
"Should return challenge on slack event endpoint",
async () => {
const res = await sendMockSlackEvent({
const res = await sendSlackEvent({
type: "url_verification",
challenge: constants.challenge,
});
Expand All @@ -56,7 +56,7 @@ describe("Slack events", () => {
async () => {
const eventId = "E1_" + Date.now().toString();

await sendMockSlackEvent({
await sendSlackEvent({
type: "event_callback",
event: {
type: "member_joined_channel",
Expand All @@ -82,7 +82,7 @@ describe("Slack events", () => {
async () => {
const eventId = "E2_" + Date.now().toString();

await sendMockSlackEvent({
await sendSlackEvent({
type: "event_callback",
event: {
type: "member_joined_channel",
Expand Down Expand Up @@ -114,7 +114,7 @@ describe("Slack events", () => {
birthday: constants.birthday,
});

await sendMockSlackEvent({
await sendSlackEvent({
type: "event_callback",
event: {
type: "member_left_channel",
Expand Down Expand Up @@ -146,7 +146,7 @@ describe("Slack events", () => {
},
{
timeout: waitTimeout,
interval: 1_000,
interval: pollInterval,
},
);
},
Expand Down
4 changes: 2 additions & 2 deletions tests/unit/askBirthday.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import "@/testUtils/mocks/mockSlackApp";
import "@/testUtils/unit/mockSlackApp";

import type { Mock } from "vitest";
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
Expand All @@ -8,7 +8,7 @@ import { handler } from "@/functions/events/askBirthday";
import { constructAskBirthdayMessage } from "@/services/slack/constructAskBirthdayMessage";
import { createSlackApp } from "@/services/slack/createSlackApp";
import { getUserInfo } from "@/services/slack/getUserInfo";
import { sendMockSqsMessage } from "@/testUtils/sendMockSqsMessage";
import { sendMockSqsMessage } from "@/testUtils/unit/sendMockSqsMessage";

const constants = vi.hoisted(() => ({
otherBotUserId: "B999",
Expand Down
19 changes: 14 additions & 5 deletions tests/unit/askPresentIdeas.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import "@/testUtils/mocks/mockDb";
import "@/testUtils/mocks/mockEventBridge";
import "@/testUtils/mocks/mockSlackApp";
import "@/testUtils/unit/mockDb";
import "@/testUtils/unit/mockEventBridge";
import "@/testUtils/unit/mockSlackApp";

import {
EventBridgeClient,
Expand All @@ -19,12 +19,21 @@ import {
} from "vitest";

import { users } from "@/db/schema";
import { callWithMockCronEvent } from "@/testUtils/callWithMockCronEvent";
import { mockEventBridgePayload } from "@/testUtils/mocks/mockEventBridgePayload";
import { handler } from "@/functions/cron/daily";
import { testDb } from "@/testUtils/testDb";
import { mockEventBridgePayload } from "@/testUtils/unit/mockEventBridgePayload";
import { mockLambdaEvent } from "@/testUtils/unit/mockLambdaPayload";

dayjs.extend(utc);

export const callWithMockCronEvent = async (eventId: string) =>
handler({
...mockLambdaEvent,
queryStringParameters: {
eventId,
},
});

const constants = vi.hoisted(() => ({
userId: "U001",
teamId: "T001",
Expand Down
10 changes: 5 additions & 5 deletions tests/unit/askPresentIdeasFromTeam.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import "@/testUtils/mocks/mockDb";
import "@/testUtils/mocks/mockEventBridge";
import "@/testUtils/mocks/mockSlackApp";
import "@/testUtils/unit/mockDb";
import "@/testUtils/unit/mockEventBridge";
import "@/testUtils/unit/mockSlackApp";

import {
EventBridgeClient,
Expand All @@ -21,9 +21,9 @@ import {
import { users } from "@/db/schema";
import type { Events } from "@/events";
import { handler as askPresentIdeasFromTeam } from "@/functions/events/askPresentIdeasFromTeam";
import { mockEventBridgePayload } from "@/testUtils/mocks/mockEventBridgePayload";
import { sendMockSqsMessage } from "@/testUtils/sendMockSqsMessage";
import { testDb } from "@/testUtils/testDb";
import { mockEventBridgePayload } from "@/testUtils/unit/mockEventBridgePayload";
import { sendMockSqsMessage } from "@/testUtils/unit/sendMockSqsMessage";

dayjs.extend(utc);

Expand Down
6 changes: 3 additions & 3 deletions tests/unit/botJoined.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import "@/testUtils/mocks/mockEventBridge";
import "@/testUtils/unit/mockEventBridge";

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

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

const constants = vi.hoisted(() => ({
channelMembers: ["U001", "U002", "U003", "U004"],
Expand Down
4 changes: 2 additions & 2 deletions tests/unit/getBirthdaysBetween.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import "@/testUtils/mocks/mockDb";
import "@/testUtils/unit/mockDb";

import dayjs from "dayjs";
import { afterEach, beforeAll, describe, expect, it, vi } from "vitest";
Expand All @@ -10,8 +10,8 @@ import {
usersInWindow,
usersOutsideWindow,
} from "@/testUtils/generateIceBreakerTestUsers";
import { testCases } from "@/testUtils/iceBreakerTestCases";
import { testDb } from "@/testUtils/testDb";
import { testCases } from "@/testUtils/unit/iceBreakerTestCases";

const constants = vi.hoisted(() => ({
teamId: "T1",
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/getIceBreakerWindow.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import MockDate from "mockdate";
import { afterAll, describe, expect, it } from "vitest";

import { getIceBreakerWindow } from "@/services/birthday/getIcebreakerWindow";
import { testCases } from "@/testUtils/iceBreakerTestCases";
import { testCases } from "@/testUtils/unit/iceBreakerTestCases";

describe("getIceBreakerWindow", () => {
afterAll(() => {
Expand Down
6 changes: 3 additions & 3 deletions tests/unit/memberJoinedChannel.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import "@/testUtils/mocks/mockEventBridge";
import "@/testUtils/unit/mockEventBridge";

import {
EventBridgeClient,
Expand All @@ -10,8 +10,8 @@ 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/mocks/mockEventBridgePayload";
import { sendMockSqsMessage } from "@/testUtils/sendMockSqsMessage";
import { mockEventBridgePayload } from "@/testUtils/unit/mockEventBridgePayload";
import { sendMockSqsMessage } from "@/testUtils/unit/sendMockSqsMessage";

const constants = vi.hoisted(() => ({
birthdayBotUserId: "B001",
Expand Down
6 changes: 3 additions & 3 deletions tests/unit/slackEvent.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import "@/testUtils/mocks/mockEventBridge";
import "@/testUtils/unit/mockEventBridge";

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

import { handler } from "@/functions/lambdas/slack-event";
import { mockEventBridgePayload } from "@/testUtils/mocks/mockEventBridgePayload";
import { mockEventBridgePayload } from "@/testUtils/unit/mockEventBridgePayload";
import {
mockLambdaContext,
mockLambdaEvent,
} from "@/testUtils/mocks/mockLambdaPayload";
} from "@/testUtils/unit/mockLambdaPayload";
import type {
SlackCallbackRequest,
SlackEvent,
Expand Down
11 changes: 0 additions & 11 deletions tests/utils/callWithMockCronEvent.ts

This file was deleted.

14 changes: 0 additions & 14 deletions tests/utils/callWithMockSlackEvent.ts

This file was deleted.

1 change: 1 addition & 0 deletions tests/utils/constants.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export const timeout = 20_000;
export const waitTimeout = 18_000;
export const pollInterval = 1_000;
Loading

0 comments on commit 16d211b

Please sign in to comment.