-
Notifications
You must be signed in to change notification settings - Fork 87
fix: bulk select normalization #7022
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
speaker-ender
wants to merge
1
commit into
main
Choose a base branch
from
fix/bulk-select-normalization--squashed
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
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
173 changes: 173 additions & 0 deletions
173
clients/admin-ui/src/features/common/hooks/useNodeMap.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,173 @@ | ||
| import { act, renderHook } from "@testing-library/react"; | ||
|
|
||
| import useNodeMap, { mapNodes, Node, NodeMap } from "./useNodeMap"; | ||
|
|
||
| describe("Normalized Data Hook", () => { | ||
| it("returns empty array when no data provided", () => { | ||
| const { result } = renderHook(() => useNodeMap()); | ||
| const { nodes } = result.current; | ||
| expect([...nodes.values()]).toStrictEqual([]); | ||
| }); | ||
|
|
||
| it("adds new data nodes to state when updated", () => { | ||
| const INITIAL_DATA = [{ key: 1, title: "first" }]; | ||
| const NEXT_DATA = [{ key: 2, title: "second" }]; | ||
| const { result } = renderHook((props) => useNodeMap(props), { | ||
| initialProps: mapNodes(INITIAL_DATA), | ||
| }); | ||
| const { nodes, update } = result.current; | ||
|
|
||
| expect([...nodes.values()]).toStrictEqual(INITIAL_DATA); | ||
|
|
||
| act(() => update(mapNodes(NEXT_DATA))); | ||
|
|
||
| expect([...result.current.nodes.values()]).toStrictEqual([ | ||
| ...INITIAL_DATA, | ||
| ...NEXT_DATA, | ||
| ]); | ||
| }); | ||
|
|
||
| it("updates existing nodes", () => { | ||
| const INITIAL_DATA = [ | ||
| { key: 1, title: "first" }, | ||
| { key: 2, title: "second" }, | ||
| ]; | ||
| const NEXT_DATA = [ | ||
| { key: 1, title: "changed" }, | ||
| { key: 3, title: "third" }, | ||
| ]; | ||
| const { result } = renderHook((props) => useNodeMap(props), { | ||
| initialProps: mapNodes(INITIAL_DATA), | ||
| }); | ||
| const { nodes, update } = result.current; | ||
|
|
||
| expect([...nodes.values()]).toStrictEqual(INITIAL_DATA); | ||
|
|
||
| act(() => update(mapNodes(NEXT_DATA))); | ||
|
|
||
| expect([...result.current.nodes.values()]).toStrictEqual([ | ||
| { key: 1, title: "changed" }, | ||
| { key: 2, title: "second" }, | ||
| { key: 3, title: "third" }, | ||
| ]); | ||
| }); | ||
|
|
||
| it("partially updates an existing node", () => { | ||
| type TestNode = Node<{ | ||
| title?: string; | ||
| subtitle?: string; | ||
| list?: string[]; | ||
| }>; | ||
| const INITIAL_DATA: TestNode[] = [ | ||
| { key: 1, title: "first", list: [] }, | ||
| { key: 2, title: "second", list: ["a", "b"] }, | ||
| ]; | ||
| const NEXT_DATA = mapNodes([ | ||
| { key: 1, subtitle: "changed", list: ["a", "b"] }, | ||
| ]); | ||
| const { result } = renderHook((props) => useNodeMap(props), { | ||
| initialProps: mapNodes(INITIAL_DATA), | ||
| }); | ||
| const { nodes, update } = result.current; | ||
|
|
||
| expect([...nodes.values()]).toStrictEqual(INITIAL_DATA); | ||
|
|
||
| act(() => update(NEXT_DATA)); | ||
|
|
||
| expect([...result.current.nodes.values()]).toStrictEqual([ | ||
| { key: 1, title: "first", subtitle: "changed", list: ["a", "b"] }, | ||
| { key: 2, title: "second", list: ["a", "b"] }, | ||
| ]); | ||
| }); | ||
|
|
||
| it("partially updates an existing node via update function", () => { | ||
| type TestNode = Node<{ | ||
| title?: string; | ||
| subtitle?: string; | ||
| list?: string[]; | ||
| }>; | ||
| const INITIAL_DATA: TestNode[] = [ | ||
| { key: 1, title: "first", list: [] }, | ||
| { key: 2, title: "second", list: ["a", "b"] }, | ||
| ]; | ||
| const NEXT_DATA = mapNodes([ | ||
| { key: 1, subtitle: "changed", list: ["a", "b"] }, | ||
| ]); | ||
| const { result } = renderHook(() => useNodeMap()); | ||
|
|
||
| const { update } = result.current; | ||
|
|
||
| act(() => update(mapNodes(INITIAL_DATA))); | ||
|
|
||
| expect([...result.current.nodes.values()]).toStrictEqual(INITIAL_DATA); | ||
|
|
||
| act(() => result.current.update(NEXT_DATA)); | ||
|
|
||
| expect([...result.current.nodes.values()]).toStrictEqual([ | ||
| { key: 1, title: "first", subtitle: "changed", list: ["a", "b"] }, | ||
| { key: 2, title: "second", list: ["a", "b"] }, | ||
| ]); | ||
| }); | ||
|
|
||
| it("replaces existing node when partial updates are disabled", () => { | ||
| type TestNode = Node<{ title?: string; subtitle?: string }>; | ||
| const INITIAL_DATA: TestNode[] = [ | ||
| { key: 1, title: "first" }, | ||
| { key: 2, title: "second" }, | ||
| ]; | ||
| const NEXT_DATA = mapNodes([{ key: 1, subtitle: "changed" }]); | ||
| const { result } = renderHook((props) => useNodeMap(props, false), { | ||
| initialProps: mapNodes(INITIAL_DATA), | ||
| }); | ||
| const { nodes, update } = result.current; | ||
|
|
||
| expect([...nodes.values()]).toStrictEqual(INITIAL_DATA); | ||
|
|
||
| act(() => update(NEXT_DATA)); | ||
|
|
||
| expect([...result.current.nodes.values()]).toStrictEqual([ | ||
| { key: 1, subtitle: "changed" }, | ||
| { key: 2, title: "second" }, | ||
| ]); | ||
| }); | ||
|
|
||
| it.skip("removes node value when explicitly removed", () => { | ||
| type TestNodes = NodeMap<{ title?: string; subtitle?: string }>; | ||
| const INITIAL_DATA: TestNodes = mapNodes([ | ||
| { key: 1, title: "first", subtitle: "ghost" }, | ||
| { key: 2, title: "second" }, | ||
| ]); | ||
| const NEXT_DATA = mapNodes([{ key: 1, subtitle: undefined }]); | ||
|
|
||
| const { result } = renderHook((props) => useNodeMap(props), { | ||
| initialProps: INITIAL_DATA, | ||
| }); | ||
| const { nodes, update } = result.current; | ||
|
|
||
| expect([...nodes.values()]).toStrictEqual(INITIAL_DATA); | ||
|
|
||
| act(() => update(NEXT_DATA)); | ||
|
|
||
| expect([...result.current.nodes.values()]).toStrictEqual([ | ||
| { key: 1, title: "first" }, | ||
| { key: 2, title: "second" }, | ||
| ]); | ||
| }); | ||
|
|
||
| it("resetting all nodes", () => { | ||
| const INITIAL_DATA = [ | ||
| { key: 1, title: "first" }, | ||
| { key: 2, title: "second" }, | ||
| ]; | ||
| const { result } = renderHook((props) => useNodeMap(props), { | ||
| initialProps: mapNodes(INITIAL_DATA), | ||
| }); | ||
| const { nodes, reset } = result.current; | ||
|
|
||
| expect([...nodes.values()]).toStrictEqual(INITIAL_DATA); | ||
|
|
||
| act(() => reset()); | ||
|
|
||
| expect([...result.current.nodes.values()]).toStrictEqual([]); | ||
| }); | ||
| }); | ||
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,48 @@ | ||
| import _ from "lodash"; | ||
| import { Key, useState } from "react"; | ||
|
|
||
| /** Note: should be adding __type and optional relational types in future iterations */ | ||
| export type Node<T> = T & { | ||
| key: Key; | ||
| }; | ||
|
|
||
| export type NodeMap<N> = Map<Key, Node<N>>; | ||
|
|
||
| export const mapNodes = <Data>(data: Node<Data>[]): NodeMap<Data> => | ||
| new Map(data.map((d) => [d.key, d])); | ||
|
|
||
| const mergeNodes = <Data>(prev: NodeMap<Data>, next: NodeMap<Data>) => | ||
| _([...next.entries()]) | ||
| .map(([key, node]) => | ||
| _.chain(prev.get(key)).defaultTo(node).merge(node).value(), | ||
| ) | ||
| .thru(mapNodes) | ||
| .value(); | ||
|
|
||
| const useNodeMap = <Data>( | ||
| initialData?: NodeMap<Data>, | ||
| partialUpdates = true, | ||
| ) => { | ||
| const [nodes, setNodeMapState] = useState<NodeMap<Data>>( | ||
| initialData ?? new Map(), | ||
| ); | ||
|
|
||
| const update = (next: NodeMap<Data>) => { | ||
| const nextDraft = partialUpdates ? mergeNodes(nodes, next) : next; | ||
| const hasChanges = _([...nextDraft.entries()]).some( | ||
| ([key, node]) => !_.isEqual(nodes.get(key), node), | ||
| ); | ||
|
|
||
| if (hasChanges) { | ||
| setNodeMapState((prev) => new Map([...prev, ...nextDraft])); | ||
| } | ||
| }; | ||
|
|
||
| const reset = () => { | ||
| setNodeMapState(new Map()); | ||
| }; | ||
|
|
||
| return { nodes, update, reset }; | ||
| }; | ||
|
|
||
| export default useNodeMap; |
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.
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.
Uh oh!
There was an error while loading. Please reload this page.