Repository navigation
[#571] Improve library benefit picker #592
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
Open
sahilds1
wants to merge
3
commits into
CodeForPhilly:main
Choose a base branch
from
sahilds1:571-improve-library-benefit-picker
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
137 changes: 137 additions & 0 deletions
137
...ents/screenerEditor/manageBenefits/benefitList/modals/SelectExistingBenefitModal.test.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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.", | ||
| ); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
normalizeBenefitNameis similar/same as logic inImportScreenerForm.tsx:19andPrintSummary.ts:38. Maybe extract a helper to/src/utils/?