Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(tests): tests for opensearch utils #1041

Merged
merged 12 commits into from
Jan 21, 2025
14 changes: 14 additions & 0 deletions mocks/data/items.ts
Original file line number Diff line number Diff line change
Expand Up @@ -515,4 +515,18 @@ export const TEST_TEMP_EXT_ITEM = items[
EXISTING_ITEM_TEMPORARY_EXTENSION_ID
] as opensearch.main.ItemResult;

export const itemList = Object.values(items);

export const getFilteredItemList = (filters: string[]) => {
return itemList.filter((item) => filters.includes(item?._source?.authority || ""));
};

export const docList = Object.values(items).map(
(item) => (item?._source || {}) as opensearch.main.Document,
);

export const getFilteredDocList = (filters: string[]) => {
return docList.filter((item) => filters.includes(item?.authority || ""));
};

export default items;
23 changes: 17 additions & 6 deletions mocks/handlers/api/search.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,23 @@
import { http, HttpResponse } from "msw";
import { cpocsList } from "../../data/cpocs";
import { http, HttpResponse, PathParams } from "msw";
import { getFilteredItemList } from "../../data/items";
import { getFilterValueAsStringArray } from "../search.utils";
import { SearchQueryBody } from "../../index.d";

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

const must = query?.bool?.must;

if (index === "main") {
const authorityValues =
getFilterValueAsStringArray(must, "terms", "authority.keyword") ||
getFilterValueAsStringArray(must, "terms", "authority") ||
[];
const itemList = getFilteredItemList(authorityValues);

if (index === "cpocs") {
return HttpResponse.json({
took: 3,
timed_out: false,
Expand All @@ -22,7 +33,7 @@ const defaultApiSearchHandler = http.post(
relation: "eq",
},
max_score: 1,
hits: cpocsList,
hits: itemList,
},
});
}
Expand Down
32 changes: 32 additions & 0 deletions mocks/handlers/search.utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,38 @@ const parseValueAsNumberArray = (value: string | string[] | undefined): number[]
);
};

export const getFilterValueAsString = (
query: QueryContainer | QueryContainer[] | undefined,
queryKey: keyof QueryContainer,
filterName: string,
): string | undefined => {
const value = getFilterValue(query, queryKey, filterName);

return parseValueAsStringArray(value).join(",");
};

export const getFilterValueAsStringArray = (
query: QueryContainer | QueryContainer[] | undefined,
queryKey: keyof QueryContainer,
filterName: string,
): string[] => {
const value = getFilterValue(query, queryKey, filterName);

return parseValueAsStringArray(value);
};

const parseValueAsStringArray = (value: string | string[] | undefined): string[] => {
if (value == undefined) {
return [];
}

if (typeof value === "string") {
return value.split(",").map((val) => val.trim());
}

return value.filter((val) => val && typeof val === "string").map((val) => val.trim()) || [];
};

export const getTermValues = (
query: QueryContainer | QueryContainer[] | undefined,
filterName: string,
Expand Down
34 changes: 17 additions & 17 deletions react-app/src/api/useSearch.test.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
import { describe, expect, it } from "vitest";
import { getOsData } from "./useSearch";
import { cpocsList } from "mocks/data/cpocs";
import { getMainExportData } from "./useSearch";
import { DEFAULT_FILTERS } from "@/components/Opensearch/main/useOpensearch";
import { getFilteredDocList } from "mocks/data/items";

describe("getOsData tests", () => {
it("should return cpocs", async () => {
const results = await getOsData({
index: "cpocs",
sort: {
field: "lastName",
order: "asc",
},
pagination: {
number: 0,
size: 20,
},
filters: [],
});
expect(results.hits.hits).toEqual(cpocsList);
describe("getMainExportData tests", () => {
it("should return spa items", async () => {
const results = await getMainExportData(DEFAULT_FILTERS.spas.filters);
expect(results).toEqual(getFilteredDocList(["Medicaid SPA", "CHIP SPA"]));
});

it("should return waiver items", async () => {
const results = await getMainExportData(DEFAULT_FILTERS.waivers.filters);
expect(results).toEqual(getFilteredDocList(["1915(b)", "1915(c)"]));
});

it("should return an empty array if there are no filters", async () => {
const results = await getMainExportData();
expect(results).toEqual([]);
});
});
Loading
Loading