Skip to content

Commit

Permalink
feat(test): refactor test (#1037)
Browse files Browse the repository at this point in the history
* renaming handlers for clarity
  • Loading branch information
thetif authored Jan 17, 2025
1 parent 4a6f3cb commit ac1d50f
Show file tree
Hide file tree
Showing 35 changed files with 240 additions and 152 deletions.
2 changes: 1 addition & 1 deletion lib/lambda/getAllForms.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { describe, expect, it, vi } from "vitest";
import { getAllForms, mapWebformsKeys } from "./getAllForms";
import { getAllForms } from "./getAllForms";
import * as wfv from "libs/webforms";

vi.mock("../libs/webforms", () => ({
Expand Down
6 changes: 3 additions & 3 deletions lib/lambda/getCpocs.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { describe, it, expect } from "vitest";
import { APIGatewayEvent } from "aws-lambda";
import { handler } from "./getCpocs";
import { mockedServiceServer } from "mocks/server";
import { emptyCpocSearchHandler, errorCpocSearchHandler } from "mocks";
import { emptyOSCpocSearchHandler, errorOSCpocSearchHandler } from "mocks";
import { cpocsList } from "mocks/data/cpocs";

describe("getCpocs Handler", () => {
Expand All @@ -18,7 +18,7 @@ describe("getCpocs Handler", () => {
// TODO - should this be removed? when will the result be empty and not
// just a result with an empty hit array
it("should return 400 if no Cpocs are found", async () => {
mockedServiceServer.use(emptyCpocSearchHandler);
mockedServiceServer.use(emptyOSCpocSearchHandler);

const event = { body: JSON.stringify({}) } as APIGatewayEvent;

Expand All @@ -39,7 +39,7 @@ describe("getCpocs Handler", () => {
});

it("should return 500 if an error occurs during processing", async () => {
mockedServiceServer.use(errorCpocSearchHandler);
mockedServiceServer.use(errorOSCpocSearchHandler);

const event = { body: JSON.stringify({}) } as APIGatewayEvent;

Expand Down
4 changes: 2 additions & 2 deletions lib/lambda/getSubTypes.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {
medicaidSubtypes,
chipSubtypes,
} from "mocks/data/types";
import { TestSubtypeItemResult, errorSubtypeSearchHandler } from "mocks";
import { TestSubtypeItemResult, errorOSSubtypeSearchHandler } from "mocks";
import { mockedServiceServer as mockedServer } from "mocks/server";

describe("getSubTypes Handler", () => {
Expand Down Expand Up @@ -40,7 +40,7 @@ describe("getSubTypes Handler", () => {
});

it("should return 500 if there is a server error", async () => {
mockedServer.use(errorSubtypeSearchHandler);
mockedServer.use(errorOSSubtypeSearchHandler);

const event = {
body: JSON.stringify({
Expand Down
4 changes: 2 additions & 2 deletions lib/lambda/getTypes.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
medicaidTypes,
chipTypes,
} from "mocks/data/types";
import { TestTypeItemResult, errorTypeSearchHandler } from "mocks";
import { TestTypeItemResult, errorOSTypeSearchHandler } from "mocks";
import { mockedServiceServer as mockedServer } from "mocks/server";

describe("getTypes Handler", () => {
Expand All @@ -33,7 +33,7 @@ describe("getTypes Handler", () => {
});

it("should return 500 if there is a server error", async () => {
mockedServer.use(errorTypeSearchHandler);
mockedServer.use(errorOSTypeSearchHandler);

const event = {
body: JSON.stringify({ authorityId: MEDICAID_SPA_AUTHORITY_ID }),
Expand Down
4 changes: 2 additions & 2 deletions lib/lambda/sinkMainProcessors.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import {
NOT_FOUND_ITEM_ID,
convertObjToBase64,
createKafkaRecord,
errorMainMultiDocumentHandler,
errorOSMainMultiDocumentHandler,
} from "mocks";
import { mockedServiceServer as mockedServer } from "mocks/server";
import {
Expand Down Expand Up @@ -845,7 +845,7 @@ describe("insertNewSeatoolRecordsFromKafkaIntoMako", () => {
});

it("handles errors in getting mako timestamps", async () => {
mockedServer.use(errorMainMultiDocumentHandler);
mockedServer.use(errorOSMainMultiDocumentHandler);

await insertNewSeatoolRecordsFromKafkaIntoMako(
[
Expand Down
7 changes: 0 additions & 7 deletions lib/vitest.setup.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import { afterAll, afterEach, beforeAll, beforeEach, vi } from "vitest";
import {
API_CONFIG,
API_ENDPOINT,
AUTH_CONFIG,
IDENTITY_POOL_ID,
OPENSEARCH_DOMAIN,
OPENSEARCH_INDEX_NAMESPACE,
Expand All @@ -22,12 +20,7 @@ import {
} from "mocks";
import { ConfigResourceTypes } from "kafkajs";
import { mockedServiceServer as mockedServer } from "mocks/server";
import { Amplify } from "aws-amplify";
type CreateType<T> = T & { default: T };
Amplify.configure({
API: API_CONFIG,
Auth: AUTH_CONFIG,
});

vi.mock("kafkajs", async (importOriginal) => ({
...(await importOriginal<typeof import("kafkajs")>()),
Expand Down
4 changes: 2 additions & 2 deletions mocks/consts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ export const AUTH_CONFIG = {
userPoolWebClientId: USER_POOL_CLIENT_ID,
oauth: {
domain: USER_POOL_CLIENT_DOMAIN,
redirectSignIn: "http://localhost",
redirectSignOut: "http://localhost",
redirectSignIn: "http://localhost:5000/",
redirectSignOut: "http://localhost:5000/",
scope: ["email", "openid"],
responseType: "code",
},
Expand Down
2 changes: 1 addition & 1 deletion mocks/data/users/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { reviewers, makoReviewer, superReviewer } from "./cmsReviewer";
import { helpDeskUsers, helpDeskUser } from "./helpDeskUsers";
import { readOnlyUsers, readOnlyUser } from "./readOnlyCMSUsers";
import { stateSubmitters, makoStateSubmitter, coStateSubmitter } from "./stateSubmitters";
import { convertUserAttributes } from "mocks/handlers/authUtils";
import { convertUserAttributes } from "mocks/handlers/auth.utils";

export const noRoleUser: TestUserData = {
UserAttributes: [
Expand Down
44 changes: 23 additions & 21 deletions mocks/handlers/api/cpocs.ts
Original file line number Diff line number Diff line change
@@ -1,30 +1,32 @@
import { http, HttpResponse } from "msw";
import { cpocsList } from "../../data/cpocs";

const defaultCpocHandler = http.post(/\/getCpocs$/, async () =>
HttpResponse.json({
took: 3,
timed_out: false,
_shards: {
total: 5,
successful: 5,
skipped: 0,
failed: 0,
},
hits: {
total: {
value: 654,
relation: "eq",
const defaultApiCpocHandler = http.post(
"https://test-domain.execute-api.us-east-1.amazonaws.com/mocked-tests/getCpocs",
async () =>
HttpResponse.json({
took: 3,
timed_out: false,
_shards: {
total: 5,
successful: 5,
skipped: 0,
failed: 0,
},
max_score: 1,
hits: cpocsList,
},
}),
hits: {
total: {
value: 654,
relation: "eq",
},
max_score: 1,
hits: cpocsList,
},
}),
);

export const errorCpocHandler = http.post(
/\/getCpocs/,
export const errorApiCpocHandler = http.post(
"https://test-domain.execute-api.us-east-1.amazonaws.com/mocked-tests/getCpocs",
() => new HttpResponse(null, { status: 500 }),
);

export const cpocHandlers = [defaultCpocHandler];
export const cpocHandlers = [defaultApiCpocHandler];
13 changes: 8 additions & 5 deletions mocks/handlers/api/index.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,23 @@
import { cpocHandlers } from "./cpocs";
import { itemHandlers } from "./items";
import { packageActionHandlers } from "./packageActions";
import { searchHandlers } from "./search";
import { submissionHandlers } from "./submissions";
import { typeHandlers } from "./types";

export const apiHandlers = [
...cpocHandlers,
...itemHandlers,
...packageActionHandlers,
...searchHandlers,
...submissionHandlers,
...typeHandlers,
];

export { errorCpocHandler } from "./cpocs";
export { errorItemHandler, errorItemExistsHandler } from "./items";
export { errorPackageActionsHandler } from "./packageActions";
export { errorAttachmentUrlHandler } from "./submissions";
export { errorSubTypesHandler, errorTypeHandler } from "./types";
export { errorApiCpocHandler } from "./cpocs";
export { errorApiItemHandler, errorApiItemExistsHandler } from "./items";
export { errorApiPackageActionsHandler } from "./packageActions";
export { errorApiSearchHandler } from "./search";
export { errorApiAttachmentUrlHandler } from "./submissions";
export { errorApiSubTypesHandler, errorApiTypeHandler } from "./types";
export { mockCurrentAuthenticatedUser, mockUseGetUser, mockUserAttributes } from "./user";
34 changes: 20 additions & 14 deletions mocks/handlers/api/items.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,28 @@ import { http, HttpResponse } from "msw";
import items, { GET_ERROR_ITEM_ID } from "../../data/items";
import type { GetItemBody } from "../../index.d";

const defaultItemHandler = http.post<GetItemBody, GetItemBody>(/\/item$/, async ({ request }) => {
const { id } = await request.json();
const defaultApiItemHandler = http.post<GetItemBody, GetItemBody>(
"https://test-domain.execute-api.us-east-1.amazonaws.com/mocked-tests/item",
async ({ request }) => {
const { id } = await request.json();

if (id == GET_ERROR_ITEM_ID) {
return new HttpResponse("Internal server error", { status: 500 });
}
if (id == GET_ERROR_ITEM_ID) {
return new HttpResponse("Internal server error", { status: 500 });
}

const item = items[id] || null;
const item = items[id] || null;

return item ? HttpResponse.json(item) : new HttpResponse(null, { status: 404 });
});
return item ? HttpResponse.json(item) : new HttpResponse(null, { status: 404 });
},
);

export const errorItemHandler = http.post(/\/item$/, () => new HttpResponse(null, { status: 500 }));
export const errorApiItemHandler = http.post(
"https://test-domain.execute-api.us-east-1.amazonaws.com/mocked-tests/item",
() => new HttpResponse(null, { status: 500 }),
);

const defaultItemExistsHandler = http.post<GetItemBody, GetItemBody>(
/\/itemExists$/,
const defaultApiItemExistsHandler = http.post<GetItemBody, GetItemBody>(
"https://test-domain.execute-api.us-east-1.amazonaws.com/mocked-tests/itemExists",
async ({ request }) => {
const { id } = await request.json();
if (id == GET_ERROR_ITEM_ID) {
Expand All @@ -27,9 +33,9 @@ const defaultItemExistsHandler = http.post<GetItemBody, GetItemBody>(
},
);

export const errorItemExistsHandler = http.post(
/\/itemExists$/,
export const errorApiItemExistsHandler = http.post(
"https://test-domain.execute-api.us-east-1.amazonaws.com/mocked-tests/itemExists",
() => new HttpResponse(null, { status: 500 }),
);

export const itemHandlers = [defaultItemHandler, defaultItemExistsHandler];
export const itemHandlers = [defaultApiItemHandler, defaultApiItemExistsHandler];
11 changes: 6 additions & 5 deletions mocks/handlers/api/packageActions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ import items from "mocks/data/items";
import { opensearch, UserRoles } from "shared-types";
import { getAvailableActions } from "shared-utils";

const defaultPackageActionsHandler = http.post<PathParams, PackageActionsRequestBody>(
/\/getPackageActions$/,
const defaultApiPackageActionsHandler = http.post<PathParams, PackageActionsRequestBody>(
"https://test-domain.execute-api.us-east-1.amazonaws.com/mocked-tests/getPackageActions",

async ({ request }) => {
const { id } = await request.json();

Expand Down Expand Up @@ -38,9 +39,9 @@ const defaultPackageActionsHandler = http.post<PathParams, PackageActionsRequest
},
);

export const errorPackageActionsHandler = http.post<PathParams, PackageActionsRequestBody>(
/\/getPackageActions$/,
export const errorApiPackageActionsHandler = http.post<PathParams, PackageActionsRequestBody>(
"https://test-domain.execute-api.us-east-1.amazonaws.com/mocked-tests/getPackageActions",
() => new HttpResponse(null, { status: 500 }),
);

export const packageActionHandlers = [defaultPackageActionsHandler];
export const packageActionHandlers = [defaultApiPackageActionsHandler];
39 changes: 39 additions & 0 deletions mocks/handlers/api/search.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { http, HttpResponse } from "msw";
import { cpocsList } from "../../data/cpocs";

const defaultApiSearchHandler = http.post(
"https://test-domain.execute-api.us-east-1.amazonaws.com/mocked-tests/search/:index",
({ params }) => {
const { index } = params;

if (index === "cpocs") {
return HttpResponse.json({
took: 3,
timed_out: false,
_shards: {
total: 5,
successful: 5,
skipped: 0,
failed: 0,
},
hits: {
total: {
value: 654,
relation: "eq",
},
max_score: 1,
hits: cpocsList,
},
});
}

return new HttpResponse(null, { status: 200 });
},
);

export const errorApiSearchHandler = http.post(
"https://test-domain.execute-api.us-east-1.amazonaws.com/mocked-tests/search/:index",
() => new HttpResponse("Internal server error", { status: 500 }),
);

export const searchHandlers = [defaultApiSearchHandler];
Loading

0 comments on commit ac1d50f

Please sign in to comment.