Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,11 @@ const BenefitList = ({
<SelectExistingBenefitModal
closeModal={() => 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 && (
Expand Down
Original file line number Diff line number Diff line change
@@ -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<void> = 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(
() => (
<SelectExistingBenefitModal
importBenefit={importBenefit}
closeModal={closeModal}
existingBenefitNames={existingBenefitNames()}
/>
),
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<string[]>([]);
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<string[]>([]);
let finishImport: () => void = () => {};
const closeModal = vi.fn();
const container = renderModal(
names,
() => new Promise<void>((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.",
);
});
});
Original file line number Diff line number Diff line change
@@ -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) =>

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

normalizeBenefitName is similar/same as logic in ImportScreenerForm.tsx:19 and PrintSummary.ts:38. Maybe extract a helper to /src/utils/?

(name ?? "").trim().toLowerCase();

const SelectExistingBenefitModal = (props: {
importBenefit: (benefitId: string) => Promise<void>;
closeModal: () => void;
existingBenefitNames: string[];
}) => {
const [availableBenefits] = createResource<Benefit[]>(fetchLibraryBenefits);
const [importingId, setImportingId] = createSignal<string | null>(null);
const [error, setError] = createSignal<string | null>(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);
Expand All @@ -34,25 +64,32 @@ const SelectExistingBenefitModal = (props: {
description, and eligibility checks.
</p>

<Show when={availableBenefits.loading}>
<div>Loading library benefits...</div>
</Show>
<Show when={availableBenefits.error}>
<div class="text-red-600" role="alert">
Error loading benefits: {availableBenefits.error.message}
</div>
</Show>
<Show when={error()}>
<div class="text-red-600 mb-3" role="alert">
{error()}
</div>
</Show>
<Show when={availableBenefits()?.length === 0}>
<div>No library benefits are currently available.</div>
</Show>
<Switch>
<Match when={availableBenefits.loading}>
<div>Loading library benefits...</div>
</Match>
<Match when={availableBenefits.error}>
<div class="text-red-600" role="alert">
Error loading benefits: {availableBenefits.error.message}
</div>
</Match>
<Match when={availableBenefits()?.length === 0}>
<div>No library benefits are currently available.</div>
</Match>
<Match when={benefitsNotInScreener().length === 0}>
<div>
All library benefits have already been added to this screener.
</div>
</Match>
</Switch>

<div class="max-h-96 space-y-3 overflow-y-auto">
<For each={availableBenefits()}>
<For each={benefitsNotInScreener()}>
{(benefit) => (
<div class="border-2 border-gray-200 rounded p-4">
<div class="font-bold text-lg">{benefit.name}</div>
Expand Down
Loading