Skip to content

Commit

Permalink
test: 🚨 Individual msw db for each test.
Browse files Browse the repository at this point in the history
  • Loading branch information
singhAmandeep007 committed Jul 3, 2024
1 parent 457e04e commit 830bc0b
Show file tree
Hide file tree
Showing 9 changed files with 229 additions and 179 deletions.
4 changes: 2 additions & 2 deletions src/services/mocker/msw/controllers/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
export * from "./reminder";
export * from "./reminderGroup";
export { setupRemindersController } from "./reminder";
export { setupReminderGroupsController } from "./reminderGroup";
151 changes: 77 additions & 74 deletions src/services/mocker/msw/controllers/reminder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,49 +13,80 @@ import {

import { getUrlSearchParams } from "shared";

import { db } from "../db";
import { TDb } from "../db";

import { urlPrefix } from "../utils";

export const getReminders = http.get(urlPrefix("/reminders"), ({ request }) => {
const reminders = db.reminder.getAll();
export const setupRemindersController = (db: TDb) => {
const getReminders = http.get(urlPrefix("/reminders"), ({ request }) => {
const reminders = db.reminder.getAll();

const searchParams = getUrlSearchParams(request.url);
const searchParams = getUrlSearchParams(request.url);

let data: TReminder[] = reminders;
let data: TReminder[] = reminders;

if (searchParams["groupId"]) {
data = data.filter((reminder) => reminder?.group?.id === searchParams["groupId"]);
}
if (searchParams["groupId"]) {
data = data.filter((reminder) => reminder?.group?.id === searchParams["groupId"]);
}

if (searchParams["state"]) {
data = data.filter((reminder) => reminder.state === searchParams["state"]);
}
if (searchParams["state"]) {
data = data.filter((reminder) => reminder.state === searchParams["state"]);
}

return HttpResponse.json({ data }, { status: 200 });
});
return HttpResponse.json({ data }, { status: 200 });
});

export const createReminder = http.post<
never,
TCreateReminderRequestPayload,
TCreateReminderResponsePayload | THTTPError
>(urlPrefix("/reminders"), async ({ request }) => {
const { title, groupId } = await request.json();
const createReminder = http.post<never, TCreateReminderRequestPayload, TCreateReminderResponsePayload | THTTPError>(
urlPrefix("/reminders"),
async ({ request }) => {
const { title, groupId } = await request.json();

const group = db.reminderGroup.getAll().find((group) => group.id === groupId);
const group = db.reminderGroup.getAll().find((group) => group.id === groupId);

if (groupId && !group) {
return HttpResponse.json({ message: `Reminder Group with id ${groupId} not found!` }, { status: 404 });
}
if (groupId && !group) {
return HttpResponse.json({ message: `Reminder Group with id ${groupId} not found!` }, { status: 404 });
}

const newReminder = db.reminder.create({ title, group });
const newReminder = db.reminder.create({ title, group });

return HttpResponse.json({ data: newReminder }, { status: 201 });
}
);

const deleteReminder = http.delete<{ id: TDeleteReminderRequestPayload }, never, TDeleteReminderResponsePayload>(
urlPrefix("/reminders/:id"),
({ params }) => {
const reminder = db.reminder.findFirst({
where: {
id: {
equals: params.id,
},
},
});

return HttpResponse.json({ data: newReminder }, { status: 201 });
});
if (!reminder) {
return HttpResponse.json({ message: `Reminder with id ${params.id} not found!` }, { status: 404 });
}

db.reminder.delete({
where: {
id: {
equals: params.id,
},
},
});

return HttpResponse.json({ message: `Reminder with id ${params.id} deleted!` }, { status: 200 });
}
);

const updateReminder = http.patch<
{ id: TUpdateReminderRequestPayload["id"] },
TUpdateReminderRequestPayload,
TUpdateReminderResponsePayload | THTTPError
>(urlPrefix("/reminders/:id"), async ({ request, params }) => {
const { groupId, ...requestBody } = await request.json();

export const deleteReminder = http.delete<{ id: TDeleteReminderRequestPayload }, never, TDeleteReminderResponsePayload>(
urlPrefix("/reminders/:id"),
({ params }) => {
const reminder = db.reminder.findFirst({
where: {
id: {
Expand All @@ -68,59 +99,31 @@ export const deleteReminder = http.delete<{ id: TDeleteReminderRequestPayload },
return HttpResponse.json({ message: `Reminder with id ${params.id} not found!` }, { status: 404 });
}

db.reminder.delete({
const group = db.reminderGroup.findFirst({
where: {
id: {
equals: params.id,
equals: groupId,
},
},
});

return HttpResponse.json({ message: `Reminder with id ${params.id} deleted!` }, { status: 200 });
}
);
if (groupId && !group) {
return HttpResponse.json({ message: `Reminder Group with id ${groupId} not found!` }, { status: 404 });
}

export const updateReminder = http.patch<
{ id: TUpdateReminderRequestPayload["id"] },
TUpdateReminderRequestPayload,
TUpdateReminderResponsePayload | THTTPError
>(urlPrefix("/reminders/:id"), async ({ request, params }) => {
const { groupId, ...requestBody } = await request.json();

const reminder = db.reminder.findFirst({
where: {
id: {
equals: params.id,
const updatedReminder = db.reminder.update({
where: {
id: {
equals: params.id,
},
},
},
});

if (!reminder) {
return HttpResponse.json({ message: `Reminder with id ${params.id} not found!` }, { status: 404 });
}

const group = db.reminderGroup.findFirst({
where: {
id: {
equals: groupId,
data: {
...(requestBody as any),
},
},
});
}) as TReminder;

if (groupId && !group) {
return HttpResponse.json({ message: `Reminder Group with id ${groupId} not found!` }, { status: 404 });
}
return HttpResponse.json({ data: updatedReminder }, { status: 200 });
});

const updatedReminder = db.reminder.update({
where: {
id: {
equals: params.id,
},
},
data: {
...(requestBody as any),
},
}) as TReminder;

return HttpResponse.json({ data: updatedReminder }, { status: 200 });
});
return [getReminders, createReminder, deleteReminder, updateReminder];
};
166 changes: 85 additions & 81 deletions src/services/mocker/msw/controllers/reminderGroup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,105 +13,109 @@ import {
THTTPError,
} from "types";

import { db } from "../db";
import { TDb } from "../db";

import { urlPrefix } from "../utils";

export const getReminderGroups = http.get(urlPrefix("/reminder-groups"), () => {
return HttpResponse.json({ data: db.reminderGroup.getAll() }, { status: 200 });
});

export const getReminderGroup = http.get<
{ id: TGetReminderGroupRequestPayload },
never,
TGetReminderGroupResponsePayload | THTTPError
>(urlPrefix("/reminder-groups/:id"), ({ params }) => {
const reminderGroup = db.reminderGroup.findFirst({
where: {
id: {
equals: params.id,
},
},
export const setupReminderGroupsController = (db: TDb) => {
const getReminderGroups = http.get(urlPrefix("/reminder-groups"), () => {
return HttpResponse.json({ data: db.reminderGroup.getAll() }, { status: 200 });
});

if (!reminderGroup) {
return HttpResponse.json({ message: `Reminder Group with id ${params.id} not found!` }, { status: 404 });
}
const getReminderGroup = http.get<
{ id: TGetReminderGroupRequestPayload },
never,
TGetReminderGroupResponsePayload | THTTPError
>(urlPrefix("/reminder-groups/:id"), ({ params }) => {
const reminderGroup = db.reminderGroup.findFirst({
where: {
id: {
equals: params.id,
},
},
});

return HttpResponse.json({ data: reminderGroup }, { status: 200 });
});
if (!reminderGroup) {
return HttpResponse.json({ message: `Reminder Group with id ${params.id} not found!` }, { status: 404 });
}

export const createReminderGroup = http.post<
never,
TCreateReminderGroupRequestPayload,
TCreateReminderGroupResponsePayload | THTTPError
>(urlPrefix("/reminder-groups"), async ({ request }) => {
const requestBody = await request.json();
return HttpResponse.json({ data: reminderGroup }, { status: 200 });
});

const reminderGroup = db.reminderGroup.create({
...requestBody,
const createReminderGroup = http.post<
never,
TCreateReminderGroupRequestPayload,
TCreateReminderGroupResponsePayload | THTTPError
>(urlPrefix("/reminder-groups"), async ({ request }) => {
const requestBody = await request.json();

const reminderGroup = db.reminderGroup.create({
...requestBody,
});

return HttpResponse.json({ data: reminderGroup }, { status: 201 });
});

return HttpResponse.json({ data: reminderGroup }, { status: 201 });
});

export const deleteReminderGroup = http.delete<
{ id: TDeleteReminderGroupRequestPayload },
never,
TDeleteReminderGroupResponsePayload | THTTPError
>(urlPrefix("/reminder-groups/:id"), ({ params }) => {
const reminderGroup = db.reminderGroup.findFirst({
where: {
id: {
equals: params.id,
const deleteReminderGroup = http.delete<
{ id: TDeleteReminderGroupRequestPayload },
never,
TDeleteReminderGroupResponsePayload | THTTPError
>(urlPrefix("/reminder-groups/:id"), ({ params }) => {
const reminderGroup = db.reminderGroup.findFirst({
where: {
id: {
equals: params.id,
},
},
},
});
});

if (!reminderGroup) {
return HttpResponse.json({ message: `Reminder Group with id ${params.id} not found!` }, { status: 404 });
}
if (!reminderGroup) {
return HttpResponse.json({ message: `Reminder Group with id ${params.id} not found!` }, { status: 404 });
}

db.reminderGroup.delete({
where: {
id: {
equals: params.id,
db.reminderGroup.delete({
where: {
id: {
equals: params.id,
},
},
},
});
});

return HttpResponse.json({ message: `Reminder Group with id ${params.id} deleted!` }, { status: 200 });
});
return HttpResponse.json({ message: `Reminder Group with id ${params.id} deleted!` }, { status: 200 });
});

export const updateReminderGroup = http.patch<
{ id: TUpdateReminderGroupRequestPayload["id"] },
TUpdateReminderGroupRequestPayload,
TUpdateReminderGroupResponsePayload | THTTPError
>(urlPrefix("/reminder-groups/:id"), async ({ request, params }) => {
const requestBody = await request.json();

const reminderGroup = db.reminderGroup.findFirst({
where: {
id: {
equals: params.id,
const updateReminderGroup = http.patch<
{ id: TUpdateReminderGroupRequestPayload["id"] },
TUpdateReminderGroupRequestPayload,
TUpdateReminderGroupResponsePayload | THTTPError
>(urlPrefix("/reminder-groups/:id"), async ({ request, params }) => {
const requestBody = await request.json();

const reminderGroup = db.reminderGroup.findFirst({
where: {
id: {
equals: params.id,
},
},
},
});
});

if (!reminderGroup) {
return HttpResponse.json({ message: `Reminder Group with id ${params.id} not found!` }, { status: 404 });
}
if (!reminderGroup) {
return HttpResponse.json({ message: `Reminder Group with id ${params.id} not found!` }, { status: 404 });
}

const updatedReminder = db.reminderGroup.update({
where: {
id: {
equals: params.id,
const updatedReminder = db.reminderGroup.update({
where: {
id: {
equals: params.id,
},
},
},
data: {
...requestBody,
},
}) as TReminderGroup;
data: {
...requestBody,
},
}) as TReminderGroup;

return HttpResponse.json({ data: updatedReminder }, { status: 200 });
});

return HttpResponse.json({ data: updatedReminder }, { status: 200 });
});
return [getReminderGroups, getReminderGroup, createReminderGroup, deleteReminderGroup, updateReminderGroup];
};
6 changes: 5 additions & 1 deletion src/services/mocker/msw/db.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { factory, nullable, oneOf, primaryKey } from "@mswjs/data";
import { factory, nullable, oneOf, primaryKey, drop } from "@mswjs/data";

import { uuid } from "shared";

Expand Down Expand Up @@ -58,3 +58,7 @@ export const buildScenarios = (db: TDb) => {
};
return builder;
};

export const dropDb = (db: TDb) => {
drop(db);
};
Loading

0 comments on commit 830bc0b

Please sign in to comment.