-
-
Notifications
You must be signed in to change notification settings - Fork 127
Mode-switching workaround for keyboard controller issue 1453 #443
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
trcoffman
wants to merge
2
commits into
LegendApp:main
Choose a base branch
from
trcoffman:workaround-1453
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
2 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
114 changes: 114 additions & 0 deletions
114
__tests__/components/ListComponent.renderScrollComponent.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,114 @@ | ||
| import * as React from "react"; | ||
| import { Text, View } from "react-native"; | ||
|
|
||
| import { describe, expect, it } from "bun:test"; | ||
| import { ListComponent } from "../../src/components/ListComponent"; | ||
| import { StateProvider, useStateContext } from "../../src/state/state"; | ||
| import { createMockState } from "../__mocks__/createMockState"; | ||
| import TestRenderer, { act } from "../helpers/testRenderer"; | ||
| import "../setup"; | ||
|
|
||
| function collectTextFromTree(node: any, values: string[] = []) { | ||
| if (node == null) { | ||
| return values; | ||
| } | ||
|
|
||
| if (typeof node === "string") { | ||
| values.push(node); | ||
| return values; | ||
| } | ||
|
|
||
| if (Array.isArray(node)) { | ||
| for (const child of node) { | ||
| collectTextFromTree(child, values); | ||
| } | ||
| return values; | ||
| } | ||
|
|
||
| if (node.children) { | ||
| collectTextFromTree(node.children, values); | ||
| } | ||
|
|
||
| return values; | ||
| } | ||
|
|
||
| function Header({ events }: { events: string[] }) { | ||
| React.useEffect(() => { | ||
| events.push("mount:header"); | ||
| return () => { | ||
| events.push("unmount:header"); | ||
| }; | ||
| }, [events]); | ||
|
|
||
| return <Text>Header</Text>; | ||
| } | ||
|
|
||
| function ListComponentHarness({ events, label }: { events: string[]; label: string }) { | ||
| const ctx = useStateContext(); | ||
| const state = React.useMemo(() => createMockState(), []); | ||
| ctx.state = state; | ||
|
|
||
| return ( | ||
| <ListComponent | ||
| canRender={false} | ||
| drawDistance={0} | ||
| estimatedItemSize={100} | ||
| getRenderedItem={() => null} | ||
| horizontal={false} | ||
| initialContentOffset={undefined} | ||
| ListHeaderComponent={<Header events={events} />} | ||
| onLayout={() => {}} | ||
| onScroll={() => {}} | ||
| recycleItems={false} | ||
| refScrollView={{ current: null }} | ||
| renderScrollComponent={(scrollProps) => { | ||
| const { children, ...rest } = scrollProps as any; | ||
| return ( | ||
| <View {...rest}> | ||
| <Text>{label}</Text> | ||
| {children} | ||
| </View> | ||
| ); | ||
| }} | ||
| scrollAdjustHandler={state.scrollAdjustHandler} | ||
| scrollEventThrottle={0} | ||
| snapToIndices={undefined} | ||
| stickyHeaderIndices={undefined} | ||
| style={{}} | ||
| updateItemSize={() => {}} | ||
| /> | ||
| ); | ||
| } | ||
|
|
||
| describe("ListComponent renderScrollComponent", () => { | ||
| it("keeps the scroll subtree mounted when the render callback identity changes", () => { | ||
| const events: string[] = []; | ||
| let renderer!: TestRenderer.ReactTestRenderer; | ||
|
|
||
| act(() => { | ||
| renderer = TestRenderer.create( | ||
| <StateProvider> | ||
| <ListComponentHarness events={events} label="first" /> | ||
| </StateProvider>, | ||
| ); | ||
| }); | ||
|
|
||
| expect(collectTextFromTree(renderer.toJSON())).toContain("first"); | ||
| expect(events).toEqual(["mount:header"]); | ||
|
|
||
| act(() => { | ||
| renderer.update( | ||
| <StateProvider> | ||
| <ListComponentHarness events={events} label="second" /> | ||
| </StateProvider>, | ||
| ); | ||
| }); | ||
|
|
||
| expect(collectTextFromTree(renderer.toJSON())).toContain("second"); | ||
| expect(events).toEqual(["mount:header"]); | ||
|
|
||
| act(() => { | ||
| renderer.unmount(); | ||
| }); | ||
| }); | ||
| }); |
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,56 @@ | ||
| import * as React from "react"; | ||
|
|
||
| import { describe, expect, it } from "bun:test"; | ||
| import { useLatestRef } from "../../src/hooks/useLatestRef"; | ||
| import TestRenderer, { act } from "../helpers/testRenderer"; | ||
| import "../setup"; | ||
|
|
||
| function Probe({ onRef, value }: { onRef: (ref: React.RefObject<string>) => void; value: string }) { | ||
| const ref = useLatestRef(value); | ||
|
|
||
| React.useEffect(() => { | ||
| onRef(ref); | ||
| }); | ||
|
|
||
| return null; | ||
| } | ||
|
|
||
| describe("useLatestRef", () => { | ||
| it("keeps a stable ref updated with the latest value", () => { | ||
| const refs: React.RefObject<string>[] = []; | ||
| let renderer!: TestRenderer.ReactTestRenderer; | ||
|
|
||
| act(() => { | ||
| renderer = TestRenderer.create( | ||
| <Probe | ||
| onRef={(ref) => { | ||
| refs.push(ref); | ||
| }} | ||
| value="first" | ||
| />, | ||
| ); | ||
| }); | ||
|
|
||
| expect(refs).toHaveLength(1); | ||
| expect(refs[0].current).toBe("first"); | ||
|
|
||
| act(() => { | ||
| renderer.update( | ||
| <Probe | ||
| onRef={(ref) => { | ||
| refs.push(ref); | ||
| }} | ||
| value="second" | ||
| />, | ||
| ); | ||
| }); | ||
|
|
||
| expect(refs).toHaveLength(2); | ||
| expect(refs[1]).toBe(refs[0]); | ||
| expect(refs[0].current).toBe("second"); | ||
|
|
||
| act(() => { | ||
| renderer.unmount(); | ||
| }); | ||
| }); | ||
| }); |
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.
On Android/new-architecture mounts where the list ref is available before the final layout size is known, this captures the one-time
getState()snapshot and keeps using its initialscrollLengthfor every latertotalSizeupdate. SincescrollLengthis copied into the returned state object rather than kept live, the first content measurement can satisfy the check with a stale 0/old viewport and enableanchoredEndSpacewhile the content is still shorter than the actual viewport plus composer inset, reintroducing the KeyboardChatScrollView short-content path this guard is meant to avoid. Fetch the latest state inside the listener, or otherwise wait for the measured scroll length before flipping the flag.Useful? React with 👍 / 👎.