diff --git a/builder-frontend/src/components/screenerEditor/manageBenefits/benefitList/BenefitList.tsx b/builder-frontend/src/components/screenerEditor/manageBenefits/benefitList/BenefitList.tsx index e7768190..0e106e48 100644 --- a/builder-frontend/src/components/screenerEditor/manageBenefits/benefitList/BenefitList.tsx +++ b/builder-frontend/src/components/screenerEditor/manageBenefits/benefitList/BenefitList.tsx @@ -113,6 +113,11 @@ const BenefitList = ({ setSelectExistingBenefitModal(false)} importBenefit={actions.importBenefit} + // Names are the only way the frontend can tell what's already in the screener + // Null for a new screener (Screener.create() doesn't set benefits), despite the type + existingBenefitNames={(screenerBenefits() ?? []).map( + (benefit) => benefit.name, + )} /> )} {benefitIdToRemove() !== null && ( diff --git a/builder-frontend/src/components/screenerEditor/manageBenefits/benefitList/modals/SelectExistingBenefitModal.test.tsx b/builder-frontend/src/components/screenerEditor/manageBenefits/benefitList/modals/SelectExistingBenefitModal.test.tsx new file mode 100644 index 00000000..1198903e --- /dev/null +++ b/builder-frontend/src/components/screenerEditor/manageBenefits/benefitList/modals/SelectExistingBenefitModal.test.tsx @@ -0,0 +1,137 @@ +// @vitest-environment jsdom + +import { createSignal } from "solid-js"; +import { render } from "solid-js/web"; +import { afterEach, describe, expect, it, vi } from "vitest"; + +import SelectExistingBenefitModal from "./SelectExistingBenefitModal"; +import { fetchLibraryBenefits } from "@/api/benefit"; +import type { Benefit } from "@/types"; + +const libraryBenefits: Benefit[] = [ + { id: "snap", name: "SNAP", description: "Food assistance", checks: [] }, + { id: "liheap", name: "LIHEAP", description: "Energy help", checks: [] }, +]; + +vi.mock("@/api/benefit", () => ({ + fetchLibraryBenefits: vi.fn(async () => libraryBenefits), +})); + +// Let the library benefits resource resolve and the modal re-render. +const flush = () => new Promise((resolve) => setTimeout(resolve, 0)); + +describe("SelectExistingBenefitModal", () => { + let dispose: (() => void) | undefined; + + afterEach(() => { + dispose?.(); + document.body.innerHTML = ""; + }); + + // Takes accessors so tests can change the screener's benefits after rendering + const renderModal = ( + existingBenefitNames: () => string[], + importBenefit: (benefitId: string) => Promise = async () => {}, + closeModal: () => void = () => {}, + ) => { + const container = document.createElement("div"); + // Solid handles onClick at the document, so the container must be in it + document.body.appendChild(container); + dispose = render( + () => ( + + ), + container, + ); + return container; + }; + + it("shows every library benefit when the screener has none of them", async () => { + const container = renderModal(() => []); + await flush(); + + expect(container.textContent).toContain("SNAP"); + expect(container.textContent).toContain("LIHEAP"); + }); + + it("hides library benefits already in the screener, ignoring case and spaces", async () => { + const container = renderModal(() => [" snap "]); + await flush(); + + expect(container.textContent).not.toContain("SNAP"); + expect(container.textContent).toContain("LIHEAP"); + }); + + it("still renders when a screener benefit has no name", async () => { + const container = renderModal(() => [null as unknown as string]); + await flush(); + + expect(container.textContent).toContain("SNAP"); + expect(container.textContent).toContain("LIHEAP"); + }); + + it("says so when every library benefit is already in the screener", async () => { + const container = renderModal(() => ["SNAP", "LIHEAP"]); + await flush(); + + expect(container.textContent).toContain( + "All library benefits have already been added to this screener.", + ); + expect(container.textContent).not.toContain( + "No library benefits are currently available.", + ); + }); + + it("updates the list when the screener's benefits change", async () => { + const [names, setNames] = createSignal([]); + const container = renderModal(names); + await flush(); + expect(container.textContent).toContain("SNAP"); + + setNames(["SNAP"]); + + expect(container.textContent).not.toContain("SNAP"); + expect(container.textContent).toContain("LIHEAP"); + }); + + it("keeps the benefit being added visible until the import finishes", async () => { + const [names, setNames] = createSignal([]); + let finishImport: () => void = () => {}; + const closeModal = vi.fn(); + const container = renderModal( + names, + () => new Promise((resolve) => (finishImport = resolve)), + closeModal, + ); + await flush(); + + // SNAP is listed first, so its button is the first "Add to screener" + container.querySelector("button")!.click(); + // The screener refetch lands before importBenefit resolves + setNames(["SNAP"]); + + expect(container.textContent).toContain("SNAP"); + expect(container.textContent).toContain("Adding..."); + expect(closeModal).not.toHaveBeenCalled(); + + finishImport(); + await flush(); + + expect(closeModal).toHaveBeenCalledOnce(); + }); + + it("shows the load error without crashing", async () => { + vi.mocked(fetchLibraryBenefits).mockRejectedValueOnce(new Error("boom")); + const container = renderModal(() => []); + await flush(); + + expect(container.textContent).toContain("Error loading benefits: boom"); + expect(container.textContent).not.toContain( + "All library benefits have already been added to this screener.", + ); + }); +}); diff --git a/builder-frontend/src/components/screenerEditor/manageBenefits/benefitList/modals/SelectExistingBenefitModal.tsx b/builder-frontend/src/components/screenerEditor/manageBenefits/benefitList/modals/SelectExistingBenefitModal.tsx index c1bdc79e..9dd8c9c1 100644 --- a/builder-frontend/src/components/screenerEditor/manageBenefits/benefitList/modals/SelectExistingBenefitModal.tsx +++ b/builder-frontend/src/components/screenerEditor/manageBenefits/benefitList/modals/SelectExistingBenefitModal.tsx @@ -1,16 +1,46 @@ -import { createResource, createSignal, For, Show } from "solid-js"; +import { + createMemo, + createResource, + createSignal, + For, + Match, + Show, + Switch, +} from "solid-js"; import { fetchLibraryBenefits } from "@/api/benefit"; import type { Benefit } from "@/types"; +// Treat a missing name as an empty string +const normalizeBenefitName = (name: string | null | undefined) => + (name ?? "").trim().toLowerCase(); + const SelectExistingBenefitModal = (props: { importBenefit: (benefitId: string) => Promise; closeModal: () => void; + existingBenefitNames: string[]; }) => { const [availableBenefits] = createResource(fetchLibraryBenefits); const [importingId, setImportingId] = createSignal(null); const [error, setError] = createSignal(null); + // Library benefits with a matching name are hidden so the same benefit isn't added twice + const benefitsNotInScreener = createMemo(() => { + // Reading an errored resource throws, so show no benefits instead + if (availableBenefits.error) return []; + const existingNames = new Set( + // Ignore case and surrounding whitespace so small differences still match + props.existingBenefitNames.map(normalizeBenefitName), + ); + return (availableBenefits() ?? []).filter( + // Keep the benefit being added in the list until the modal closes, since the + // screener refetches before importBenefit resolves + (benefit) => + benefit.id === importingId() || + !existingNames.has(normalizeBenefitName(benefit.name)), + ); + }); + const importBenefit = async (benefitId: string) => { setImportingId(benefitId); setError(null); @@ -34,25 +64,32 @@ const SelectExistingBenefitModal = (props: { description, and eligibility checks.

- -
Loading library benefits...
-
- - - - -
No library benefits are currently available.
-
+ + +
Loading library benefits...
+
+ + + + +
No library benefits are currently available.
+
+ +
+ All library benefits have already been added to this screener. +
+
+
- + {(benefit) => (
{benefit.name}