|
| 1 | +// @vitest-environment jsdom |
| 2 | + |
| 3 | +import { createSignal } from "solid-js"; |
| 4 | +import { render } from "solid-js/web"; |
| 5 | +import { afterEach, describe, expect, it, vi } from "vitest"; |
| 6 | + |
| 7 | +import SelectExistingBenefitModal from "./SelectExistingBenefitModal"; |
| 8 | +import { fetchLibraryBenefits } from "@/api/benefit"; |
| 9 | +import type { Benefit } from "@/types"; |
| 10 | + |
| 11 | +const libraryBenefits: Benefit[] = [ |
| 12 | + { id: "snap", name: "SNAP", description: "Food assistance", checks: [] }, |
| 13 | + { id: "liheap", name: "LIHEAP", description: "Energy help", checks: [] }, |
| 14 | +]; |
| 15 | + |
| 16 | +vi.mock("@/api/benefit", () => ({ |
| 17 | + fetchLibraryBenefits: vi.fn(async () => libraryBenefits), |
| 18 | +})); |
| 19 | + |
| 20 | +// Let the library benefits resource resolve and the modal re-render. |
| 21 | +const flush = () => new Promise((resolve) => setTimeout(resolve, 0)); |
| 22 | + |
| 23 | +describe("SelectExistingBenefitModal", () => { |
| 24 | + let dispose: (() => void) | undefined; |
| 25 | + |
| 26 | + afterEach(() => { |
| 27 | + dispose?.(); |
| 28 | + document.body.innerHTML = ""; |
| 29 | + }); |
| 30 | + |
| 31 | + // Takes accessors so tests can change the screener's benefits after rendering |
| 32 | + const renderModal = ( |
| 33 | + existingBenefitNames: () => string[], |
| 34 | + importBenefit: (benefitId: string) => Promise<void> = async () => {}, |
| 35 | + closeModal: () => void = () => {}, |
| 36 | + ) => { |
| 37 | + const container = document.createElement("div"); |
| 38 | + // Solid handles onClick at the document, so the container must be in it |
| 39 | + document.body.appendChild(container); |
| 40 | + dispose = render( |
| 41 | + () => ( |
| 42 | + <SelectExistingBenefitModal |
| 43 | + importBenefit={importBenefit} |
| 44 | + closeModal={closeModal} |
| 45 | + existingBenefitNames={existingBenefitNames()} |
| 46 | + /> |
| 47 | + ), |
| 48 | + container, |
| 49 | + ); |
| 50 | + return container; |
| 51 | + }; |
| 52 | + |
| 53 | + it("shows every library benefit when the screener has none of them", async () => { |
| 54 | + const container = renderModal(() => []); |
| 55 | + await flush(); |
| 56 | + |
| 57 | + expect(container.textContent).toContain("SNAP"); |
| 58 | + expect(container.textContent).toContain("LIHEAP"); |
| 59 | + }); |
| 60 | + |
| 61 | + it("hides library benefits already in the screener, ignoring case and spaces", async () => { |
| 62 | + const container = renderModal(() => [" snap "]); |
| 63 | + await flush(); |
| 64 | + |
| 65 | + expect(container.textContent).not.toContain("SNAP"); |
| 66 | + expect(container.textContent).toContain("LIHEAP"); |
| 67 | + }); |
| 68 | + |
| 69 | + it("still renders when a screener benefit has no name", async () => { |
| 70 | + const container = renderModal(() => [null as unknown as string]); |
| 71 | + await flush(); |
| 72 | + |
| 73 | + expect(container.textContent).toContain("SNAP"); |
| 74 | + expect(container.textContent).toContain("LIHEAP"); |
| 75 | + }); |
| 76 | + |
| 77 | + it("says so when every library benefit is already in the screener", async () => { |
| 78 | + const container = renderModal(() => ["SNAP", "LIHEAP"]); |
| 79 | + await flush(); |
| 80 | + |
| 81 | + expect(container.textContent).toContain( |
| 82 | + "All library benefits have already been added to this screener.", |
| 83 | + ); |
| 84 | + expect(container.textContent).not.toContain( |
| 85 | + "No library benefits are currently available.", |
| 86 | + ); |
| 87 | + }); |
| 88 | + |
| 89 | + it("updates the list when the screener's benefits change", async () => { |
| 90 | + const [names, setNames] = createSignal<string[]>([]); |
| 91 | + const container = renderModal(names); |
| 92 | + await flush(); |
| 93 | + expect(container.textContent).toContain("SNAP"); |
| 94 | + |
| 95 | + setNames(["SNAP"]); |
| 96 | + |
| 97 | + expect(container.textContent).not.toContain("SNAP"); |
| 98 | + expect(container.textContent).toContain("LIHEAP"); |
| 99 | + }); |
| 100 | + |
| 101 | + it("keeps the benefit being added visible until the import finishes", async () => { |
| 102 | + const [names, setNames] = createSignal<string[]>([]); |
| 103 | + let finishImport: () => void = () => {}; |
| 104 | + const closeModal = vi.fn(); |
| 105 | + const container = renderModal( |
| 106 | + names, |
| 107 | + () => new Promise<void>((resolve) => (finishImport = resolve)), |
| 108 | + closeModal, |
| 109 | + ); |
| 110 | + await flush(); |
| 111 | + |
| 112 | + // SNAP is listed first, so its button is the first "Add to screener" |
| 113 | + container.querySelector("button")!.click(); |
| 114 | + // The screener refetch lands before importBenefit resolves |
| 115 | + setNames(["SNAP"]); |
| 116 | + |
| 117 | + expect(container.textContent).toContain("SNAP"); |
| 118 | + expect(container.textContent).toContain("Adding..."); |
| 119 | + expect(closeModal).not.toHaveBeenCalled(); |
| 120 | + |
| 121 | + finishImport(); |
| 122 | + await flush(); |
| 123 | + |
| 124 | + expect(closeModal).toHaveBeenCalledOnce(); |
| 125 | + }); |
| 126 | + |
| 127 | + it("shows the load error without crashing", async () => { |
| 128 | + vi.mocked(fetchLibraryBenefits).mockRejectedValueOnce(new Error("boom")); |
| 129 | + const container = renderModal(() => []); |
| 130 | + await flush(); |
| 131 | + |
| 132 | + expect(container.textContent).toContain("Error loading benefits: boom"); |
| 133 | + expect(container.textContent).not.toContain( |
| 134 | + "All library benefits have already been added to this screener.", |
| 135 | + ); |
| 136 | + }); |
| 137 | +}); |
0 commit comments