-
Notifications
You must be signed in to change notification settings - Fork 6
fix(OneFilterPicker): track nested filter keys when options load async #5270
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
factmarc10010
wants to merge
3
commits into
main
Choose a base branch
from
fix/nested-filter-keys-async-options
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
142 changes: 142 additions & 0 deletions
142
packages/react/src/patterns/OneFilterPicker/__test__/nestedAsyncOptions.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,142 @@ | ||
| import userEvent from "@testing-library/user-event" | ||
| import "@testing-library/jest-dom/vitest" | ||
| import { describe, expect, it, vi } from "vitest" | ||
|
|
||
| import { zeroRender as render, screen, waitFor } from "@/testing/test-utils" | ||
|
|
||
| import type { FiltersDefinition } from "../types" | ||
|
|
||
| import { OneFilterPicker } from "../index" | ||
|
|
||
| /** | ||
| * With async options the picker can't walk the tree to find the nested child | ||
| * keys, so it used to treat the parent as empty: no active dot, no count, | ||
| * nothing to clear or to drop with the chip. `nestedFilterKeys` declares them. | ||
| */ | ||
| const officeOptions = [ | ||
| { | ||
| value: "101", | ||
| label: "Barcelona HQ", | ||
| children: { | ||
| filterKey: "space", | ||
| options: [ | ||
| { value: "1", label: "Floor 1" }, | ||
| { value: "2", label: "Floor 2" }, | ||
| ], | ||
| }, | ||
| }, | ||
| ] | ||
|
|
||
| const asyncDefinition = { | ||
| office: { | ||
| type: "in", | ||
| label: "Office", | ||
| options: { | ||
| nestedFilterKeys: ["space"], | ||
| options: async () => officeOptions, | ||
| }, | ||
| }, | ||
| space: { | ||
| type: "in", | ||
| label: "Space", | ||
| hideSelector: true, | ||
| options: { | ||
| options: [ | ||
| { value: "1", label: "Floor 1" }, | ||
| { value: "2", label: "Floor 2" }, | ||
| ], | ||
| }, | ||
| }, | ||
| } as const satisfies FiltersDefinition | ||
|
|
||
| const openPicker = async (user: ReturnType<typeof userEvent.setup>) => { | ||
| await user.click(screen.getByRole("button", { name: /filters/i })) | ||
| await waitFor(() => | ||
| expect(screen.getByRole("button", { name: "Office" })).toBeInTheDocument() | ||
| ) | ||
| } | ||
|
|
||
| describe("OneFilterPicker - nested filters with async options", () => { | ||
| it("marks the parent filter as active when only a nested child is selected", async () => { | ||
| const user = userEvent.setup({ pointerEventsCheck: 0 }) | ||
|
|
||
| render( | ||
| <OneFilterPicker | ||
| filters={asyncDefinition} | ||
| value={{ space: ["1"] }} | ||
| onChange={vi.fn()} | ||
| /> | ||
| ) | ||
|
|
||
| await openPicker(user) | ||
|
|
||
| expect( | ||
| screen.getByRole("button", { | ||
| name: "Office", | ||
| description: "Active filters: Office", | ||
| }) | ||
| ).toBeInTheDocument() | ||
| }) | ||
|
|
||
| it("counts nested child selections in the parent's selected label", async () => { | ||
| const user = userEvent.setup({ pointerEventsCheck: 0 }) | ||
|
|
||
| render( | ||
| <OneFilterPicker | ||
| filters={asyncDefinition} | ||
| value={{ space: ["1", "2"] }} | ||
| onChange={vi.fn()} | ||
| /> | ||
| ) | ||
|
|
||
| await openPicker(user) | ||
| await user.click(screen.getByRole("button", { name: "Office" })) | ||
|
|
||
| expect(await screen.findByText("2 selected")).toBeInTheDocument() | ||
| }) | ||
|
|
||
| it("clears nested child selections from the parent's select-all toggle", async () => { | ||
| const user = userEvent.setup({ pointerEventsCheck: 0 }) | ||
| const onChange = vi.fn() | ||
|
|
||
| render( | ||
| <OneFilterPicker | ||
| filters={asyncDefinition} | ||
| value={{ space: ["1"] }} | ||
| onChange={onChange} | ||
| /> | ||
| ) | ||
|
|
||
| await openPicker(user) | ||
| await user.click(screen.getByRole("button", { name: "Office" })) | ||
|
|
||
| await user.click( | ||
| await screen.findByRole("checkbox", { name: /select all/i }) | ||
| ) | ||
| await user.click(screen.getByRole("button", { name: /apply filters/i })) | ||
|
|
||
| // Empty values are dropped on apply: an orphaned `space: ["1"]` would survive. | ||
| expect(onChange).toHaveBeenCalledWith({}) | ||
| }) | ||
|
|
||
| it("drops nested child selections together with the parent chip", async () => { | ||
| const user = userEvent.setup({ pointerEventsCheck: 0 }) | ||
| const onChange = vi.fn() | ||
|
|
||
| render( | ||
| <OneFilterPicker | ||
| filters={asyncDefinition} | ||
| value={{ office: ["101"], space: ["1"] }} | ||
| onChange={onChange} | ||
| /> | ||
| ) | ||
|
|
||
| const removeChip = await screen.findByRole("button", { | ||
| name: "Close", | ||
| description: /^Office:/, | ||
| }) | ||
| await user.click(removeChip) | ||
|
|
||
| expect(onChange).toHaveBeenCalledWith({}) | ||
| }) | ||
| }) |
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
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
103 changes: 103 additions & 0 deletions
103
...c/patterns/OneFilterPicker/filterTypes/InFilter/components/__tests__/option-utils.test.ts
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,103 @@ | ||
| import { describe, expect, it } from "vitest" | ||
|
|
||
| import type { FilterTypeSchema } from "../../../types" | ||
| import type { InFilterOptionItem, InFilterOptions } from "../../types" | ||
|
|
||
| import { getCacheKey, loadOptions } from "../../useLoadOptions" | ||
| import { collectNestedFilterKeys } from "../option-utils" | ||
|
|
||
| type Schema = FilterTypeSchema<InFilterOptions<string>> | ||
|
|
||
| const workplaceOptions: InFilterOptionItem<string>[] = [ | ||
| { | ||
| value: "barcelona", | ||
| label: "Barcelona Office", | ||
| children: { | ||
| filterKey: "workArea", | ||
| options: [ | ||
| { | ||
| value: "floor-1", | ||
| label: "Floor 1", | ||
| children: { | ||
| filterKey: "desk", | ||
| options: [{ value: "desk-1", label: "Desk 1" }], | ||
| }, | ||
| }, | ||
| ], | ||
| }, | ||
| }, | ||
| { value: "remote", label: "Remote" }, | ||
| ] | ||
|
|
||
| describe("collectNestedFilterKeys", () => { | ||
| it("walks literal option arrays at every depth", () => { | ||
| const schema: Schema = { | ||
| label: "Workplace", | ||
| options: { options: workplaceOptions }, | ||
| } | ||
|
|
||
| expect(collectNestedFilterKeys(schema)).toEqual(["workArea", "desk"]) | ||
| }) | ||
|
|
||
| it("returns no keys for a flat filter", () => { | ||
| const schema: Schema = { | ||
| label: "Department", | ||
| options: { options: [{ value: "eng", label: "Engineering" }] }, | ||
| } | ||
|
|
||
| expect(collectNestedFilterKeys(schema)).toEqual([]) | ||
| }) | ||
|
|
||
| it("reads the keys declared in the schema when options are async", () => { | ||
| const schema: Schema = { | ||
| label: "Workplace", | ||
| options: { | ||
| nestedFilterKeys: ["workArea"], | ||
| options: async () => workplaceOptions, | ||
| }, | ||
| } | ||
|
|
||
| expect(collectNestedFilterKeys(schema)).toEqual(["workArea"]) | ||
| }) | ||
|
|
||
| it("reads the keys declared in the schema when options come from a source", () => { | ||
| const schema: Schema = { | ||
| label: "Workplace", | ||
| options: { | ||
| nestedFilterKeys: ["workArea"], | ||
| source: { dataAdapter: { fetchData: async () => ({ records: [] }) } }, | ||
| mapOptions: (item: InFilterOptionItem<string>) => item, | ||
| }, | ||
| } | ||
|
|
||
| expect(collectNestedFilterKeys(schema)).toEqual(["workArea"]) | ||
| }) | ||
|
|
||
| it("falls back to the options resolved by a previous load", async () => { | ||
| const schema: Schema = { | ||
| label: "Workplace (cached)", | ||
| options: { cache: true, options: async () => workplaceOptions }, | ||
| } | ||
|
|
||
| expect(collectNestedFilterKeys(schema)).toEqual([]) | ||
|
|
||
| await loadOptions(getCacheKey(schema), () => workplaceOptions, true) | ||
|
|
||
| expect(collectNestedFilterKeys(schema)).toEqual(["workArea", "desk"]) | ||
| }) | ||
|
|
||
| it("merges the declared keys with the ones found in the options", async () => { | ||
| const schema: Schema = { | ||
| label: "Workplace (merged)", | ||
| options: { | ||
| cache: true, | ||
| nestedFilterKeys: ["workArea"], | ||
| options: async () => workplaceOptions, | ||
| }, | ||
| } | ||
|
|
||
| await loadOptions(getCacheKey(schema), () => workplaceOptions, true) | ||
|
|
||
| expect(collectNestedFilterKeys(schema)).toEqual(["workArea", "desk"]) | ||
| }) | ||
| }) |
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
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
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
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.
This fallback doesn't fire where it's needed. With async options and no
nestedFilterKeys, the dot stays off after the options load and only appears on a later mount:nestedKeysMapinFilterList.tsxis auseMemokeyed ondefinition, so the cache filling later never invalidates it. Same filter, two behaviors, depending on history the user can't see. It also makes render impure, since this reads a mutable module-levelMapduring render.Suggest dropping the fallback and letting
nestedFilterKeysbe the single answer for non-literal options. It's deterministic and already carries the fix on its own: all four integration tests pass on the declared keys alone.Heads up that
option-utils.test.ts:76can't catch this. It callsloadOptions(getCacheKey(schema), ...)with the same object and asserts the read back, so it covers the Map round-trip, not the component path.