Skip to content

Commit

Permalink
add api mocks
Browse files Browse the repository at this point in the history
  • Loading branch information
nipuna-g committed Dec 3, 2023
1 parent 4e0fae5 commit 91c3a2c
Show file tree
Hide file tree
Showing 18 changed files with 237,756 additions and 219 deletions.
3 changes: 2 additions & 1 deletion client/.env.example
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
SERVER_URL=http://localhost:8000
SERVER_URL=http://localhost:8000
NEXT_PUBLIC_API_MOCKING=enabled
4 changes: 4 additions & 0 deletions client/api/sheety/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ import { getSheetyUrl } from "./utils";

export const getSheetyData = async <T>(sheet: Sheets): Promise<T[]> => {
try {
if (process.env.NEXT_PUBLIC_API_MOCKING === "enabled") {
await window.__mswStart;
}

const sheetPath = getSheetyUrl(sheet);
const res = await axios.get<Record<Sheets, T[]>>(sheetPath, {
headers: {
Expand Down
4 changes: 4 additions & 0 deletions client/mocks/browser.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import { setupWorker } from "msw/browser";
import { handlers } from "./handlers";

export const worker = setupWorker(...handlers);
41 changes: 41 additions & 0 deletions client/mocks/handlers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { SHEETY_BASE_URL, Sheets } from "api/sheety/constants";
import { http, HttpResponse } from "msw";
import { methodsSuccessResponse } from "./responses/methods";
import { categoriesSuccessResponse } from "./responses/categories";
import { instructionsSuccessResponse } from "./responses/instructions";
import { facilitiesSuccessResponse } from "./responses/facilities";
import { pickupSuccessResponse } from "./responses/pickup";
import { itemsSuccessResponse } from "./responses/items";
import { unrecyclabilitySuccessResponse } from "./responses/unrecyclability";
import { onemapSuccessResponse } from "./responses/onemap";

export const handlers = [
http.get(getUrlFromSheetName(Sheets.ITEMS_SHEET_NAME), () => {
return HttpResponse.json(itemsSuccessResponse);
}),
http.get(getUrlFromSheetName(Sheets.METHODS_SHEET_NAME), () => {
return HttpResponse.json(methodsSuccessResponse);
}),
http.get(getUrlFromSheetName(Sheets.CATEGORIES_SHEET_NAME), () => {
return HttpResponse.json(categoriesSuccessResponse);
}),
http.get(getUrlFromSheetName(Sheets.INSTRUCTIONS_SHEET_NAME), () => {
return HttpResponse.json(instructionsSuccessResponse);
}),
http.get(getUrlFromSheetName(Sheets.FACILITIES_SHEET_NAME), () => {
return HttpResponse.json(facilitiesSuccessResponse);
}),
http.get(getUrlFromSheetName(Sheets.PICKUP_SHEET_NAME), () => {
return HttpResponse.json(pickupSuccessResponse);
}),
http.get(getUrlFromSheetName(Sheets.UNRECYCLABILITY_SHEET_NAME), () => {
return HttpResponse.json(unrecyclabilitySuccessResponse);
}),
http.get("https://developers.onemap.sg/commonapi/search*", () => {
return HttpResponse.json(onemapSuccessResponse);
}),
];

function getUrlFromSheetName(sheetName: string) {
return SHEETY_BASE_URL + sheetName.split(" ")[0] + "*";
}
8 changes: 8 additions & 0 deletions client/mocks/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
async function initMocks() {
const { worker } = await import("./browser");
return await worker.start();
}

window.__mswStart = initMocks();

export {};
131 changes: 131 additions & 0 deletions client/mocks/responses/categories.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
export const categoriesSuccessResponse = {
"categoriesList (final)": [
{
itemCategories: "CONTENT_CONTAINER",
method1: "RECYCLE",
id: 2,
},
{
itemCategories: "FOOD_DRINK_CONTAINER",
method1: "RECYCLE",
id: 3,
},
{
itemCategories: "RECYCLABLE",
method1: "RECYCLE",
id: 4,
},
{
itemCategories: "PAPER_AND_CARDBOARD",
method1: "RECYCLE",
id: 5,
},
{
itemCategories: "BAGS_AND_ACCESSORIES",
method1: "RECYCLE",
method2: "RESELL",
method3: "DONATE",
method4: "REPAIR",
id: 6,
},
{
itemCategories: "HOUSEHOLD_GOODS",
method1: "RECYCLE",
method2: "RESELL",
id: 7,
},
{
itemCategories: "ICT_EQUIPMENT",
method1: "RECYCLE",
method2: "REPAIR",
method3: "DONATE",
method4: "REPAIR",
id: 8,
},
{
itemCategories: "INKS_AND_TONER_CARTRIDGES",
method1: "RECYCLE",
id: 9,
},
{
itemCategories: "LAMPS",
method1: "RECYCLE",
id: 10,
},
{
itemCategories: "OTHER_ELECTRICAL_APPLIANCES",
method1: "DONATE",
method2: "REPAIR",
method3: "RESELL",
id: 11,
},
{
itemCategories: "SHOES",
method1: "DONATE",
method2: "REPAIR",
id: 12,
},
{
itemCategories: "STATIONERY",
method1: "DONATE",
method2: "RESELL",
id: 13,
},
{
itemCategories: "TOYS",
method1: "DONATE",
method2: "RESELL",
id: 14,
},
{
itemCategories: "BABY_CHILDREN_ITEMS",
method1: "DONATE",
method2: "RESELL",
id: 15,
},
{
itemCategories: "CLOTHING",
method1: "DONATE",
method2: "REPAIR",
method3: "RESELL",
id: 16,
},
{
itemCategories: "FURNITURE",
method1: "DONATE",
method2: "RESELL",
id: 17,
},
{
itemCategories: "LARGE_HOUSEHOLD_APPLIANCES",
method1: "DONATE",
id: 18,
},
{
itemCategories: "FOOD",
method1: "DONATE",
id: 19,
},
{
itemCategories: "BOOKS",
method1: "DONATE",
method2: "RESELL",
id: 20,
},
{
itemCategories: "PORTABLE_BATTERIES",
method1: "RECYCLE",
id: 21,
},
{
itemCategories: "LAMPS",
method1: "RECYCLE",
id: 22,
},
{
itemCategories: "GENERAL_WASTE",
method1: "RECYCLE",
id: 23,
},
],
};
Loading

0 comments on commit 91c3a2c

Please sign in to comment.