Skip to content

Commit

Permalink
feat: updated test (#287)
Browse files Browse the repository at this point in the history
  • Loading branch information
Fran McDade authored and Fran McDade committed Dec 12, 2024
1 parent bb7602f commit 931ae52
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 2 deletions.
26 changes: 26 additions & 0 deletions src/providers/fileManifestState/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,21 @@ import {
UpdateFileManifestPayload,
} from "../fileManifestState";

/**
* Checks if all form facets are in a selected state.
* The form facets are considered selected if they are present in the filters.
* @param state - File manifest state.
* @returns true if all form facets are fully selected.
*/
export function areAllFormFacetsSelected(state: FileManifestState): boolean {
const { filters, selectedFormFacetNames } = state;
for (const facetName of [...selectedFormFacetNames]) {
if (isFacetSelected(filters, facetName)) continue;
return false;
}
return true;
}

/**
* Determines if all form facet terms are fully selected.
* @param state - File manifest state.
Expand Down Expand Up @@ -53,12 +68,23 @@ export function getRequestFilters(
): Filters | undefined {
if (state.filesFacetsStatus !== FILES_FACETS_STATUS.COMPLETED) return;
if (state.selectedFormFacetNames.size === 0) return;
if (!areAllFormFacetsSelected(state)) return;
// Form terms are partially selected; return filters.
if (!areAllFormFiltersSelected(state)) return state.filters;
// Form terms are fully selected; return filters excluding form filters.
return excludeFullySelectedFormFilters(state);
}

/**
* Returns true if the facet is selected i.e. present in the filters.
* @param filters - Filters.
* @param facetName - Facet name.
* @returns true if the facet is selected.
*/
function isFacetSelected(filters: Filters, facetName: string): boolean {
return filters.some(({ categoryKey }) => categoryKey === facetName);
}

/**
* Transitions the files facets' status.
* @param shouldTransition - True if transitioning, false otherwise.
Expand Down
38 changes: 36 additions & 2 deletions tests/fileManifestRequestFilters.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,16 +40,27 @@ const SELECTED_FILTERS_FORM_COMPLETE_SET: Filters = [
{ categoryKey: "category02", value: ["value02", "value03", "value04"] },
];

const SELECTED_FILTERS_FORM_INCOMPLETE_SET: Filters = [
{ categoryKey: "category01", value: ["value01", "value02"] },
];

const SELECTED_FILTERS_FORM_SUBSET: Filters = [
{ categoryKey: "category01", value: ["value01"] },
{ categoryKey: "category02", value: ["value02", "value03", "value04"] },
];

const FILTERS: Filters = SELECTED_FILTERS;

const FILTERS_COMPLETE_SET: Filters = [
...SELECTED_FILTERS,
...SELECTED_FILTERS_FORM_COMPLETE_SET,
];

const FILTERS_INCOMPLETE_SET: Filters = [
...SELECTED_FILTERS,
...SELECTED_FILTERS_FORM_INCOMPLETE_SET,
];

const FILTERS_SUBSET: Filters = [
...SELECTED_FILTERS,
...SELECTED_FILTERS_FORM_SUBSET,
Expand Down Expand Up @@ -81,16 +92,39 @@ describe("fileManifestRequestFilters", () => {
).toBeUndefined();
});

test("returns request filters when status is COMPLETED", () => {
test("returns undefined when status is COMPLETED but no form facet terms are selected", () => {
expect(
getRequestFilters({
...FILE_MANIFEST_STATE_COMPLETED,
filesFacets: FILES_FACETS as FileFacet[],
filters: FILTERS, // Entity list filters.
})
).toBeUndefined();
});

test("returns undefined when status is COMPLETED but one or more form facets have no terms selected", () => {
expect(
getRequestFilters({
...FILE_MANIFEST_STATE_COMPLETED,
filesFacets: FILES_FACETS as FileFacet[],
filters: FILTERS_INCOMPLETE_SET,
selectedFormFacetNames: new Set(FORM_FACET_NAMES),
})
).toEqual(FILE_MANIFEST_STATE_COMPLETED.filters);
).toBeUndefined();
});
});

test("returns request filters when status is COMPLETED and at least one term is selected for each form facet", () => {
expect(
getRequestFilters({
...FILE_MANIFEST_STATE_COMPLETED,
filesFacets: FILES_FACETS as FileFacet[],
filters: FILTERS_SUBSET,
selectedFormFacetNames: new Set(FORM_FACET_NAMES),
})
).toEqual(FILTERS_SUBSET);
});

describe("when form terms are not-selected", () => {
test("returns undefined", () => {
expect(
Expand Down

0 comments on commit 931ae52

Please sign in to comment.