Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/reader-native-share-selection.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@youversion/platform-react-ui': patch
---

Preserve newer verse selections when an earlier native Share request completes, avoiding stale selection-cleared events after navigation.
92 changes: 91 additions & 1 deletion packages/ui/src/components/bible-reader-controlled.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,11 @@ import {
vi,
beforeEach,
afterEach,
onTestFinished,
type Mock,
type MockInstance,
} from 'vitest';
import { render, screen, fireEvent, waitFor } from '@testing-library/react';
import { act, render, screen, fireEvent, waitFor } from '@testing-library/react';
import {
DataExchangeClient,
YouVersionAPIUsers,
Expand Down Expand Up @@ -1271,3 +1272,92 @@ it('does not restore the verse menu after an outside click if the permission gra
localStorage.clear();
}
});

it.each([
'original selection',
'new selection',
'reselected same verse',
'new passage',
'new passage without selection',
])('native Share completion only clears its original selection: %s', async (scenario) => {
onTestFinished(() => {
vi.unstubAllGlobals();
});
let finishShare: () => void = () => {
throw new Error('Share promise not initialized');
};
const pendingShare = new Promise<void>((resolve) => {
finishShare = resolve;
});
const share = vi.fn(() => pendingShare);
vi.stubGlobal('navigator', {
userAgent: navigator.userAgent,
platform: navigator.platform,
share,
});
const onVerseSelect = verseSelectSpy();
const overrides: HookOverrides = {
...defaultOverrides(),
usePassage: ({ usfm }) => ({
passage: {
...mockPassage,
id: usfm,
reference: usfm,
content:
usfm === 'JHN.2'
? mockPassage.content.replace('In the beginning was the Word.', 'The second chapter.')
: mockPassage.content,
},
loading: false,
error: null,
refetch: () => undefined,
}),
};
const reader = (chapter: string) =>
wrapReader({ highlights: [], chapter, onVerseSelect }, overrides);
const view = render(reader('1'));
selectVerse(view.container, 1);
fireEvent.click(await screen.findByRole('button', { name: /^share$/i }));
expect(share).toHaveBeenCalledExactlyOnceWith({
text: '“In the beginning was the Word.”\n\nJohn 1:1 NIV',
});
expect(getVerseEl(view.container, 1)).toHaveClass('yv-v-selected');

if (scenario === 'new passage' || scenario === 'new passage without selection') {
view.rerender(reader('2'));
expect(getVerseEl(view.container, 1)).not.toHaveClass('yv-v-selected');
if (scenario === 'new passage') {
selectVerse(view.container, 2);
expect(lastSelection(onVerseSelect)).toMatchObject({ chapter: '2', verses: [2] });
}
} else if (scenario === 'new selection') {
selectVerse(view.container, 1);
selectVerse(view.container, 2);
expect(lastSelection(onVerseSelect)).toMatchObject({ chapter: '1', verses: [2] });
} else if (scenario === 'reselected same verse') {
selectVerse(view.container, 1);
selectVerse(view.container, 1);

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

praise: The same-verse reselection case catches a value-equality guard that would incorrectly clear a newer selection.

For Agents: selection identity regression

Deselecting and reselecting verse 1 produces the same verse numbers but a new selection identity. This case checks that Share completion preserves both the selected verse and its popover without emitting a clear event. Restoring the old handler makes this case fail; the current guard passes.

Written by Code Reviewer bot on behalf of Cam.

expect(lastSelection(onVerseSelect)).toMatchObject({ chapter: '1', verses: [1] });
}
onVerseSelect.mockClear();

await act(async () => {
finishShare();
await pendingShare;
});

if (scenario === 'original selection') {
expect(lastSelection(onVerseSelect)).toEqual(selection([]));
expect(getVerseEl(view.container, 1)).not.toHaveClass('yv-v-selected');
expect(screen.queryByRole('button', { name: /^share$/i })).toBeNull();
} else if (scenario === 'new passage without selection') {
expect(onVerseSelect).not.toHaveBeenCalled();
expect(getVerseEl(view.container, 1)).not.toHaveClass('yv-v-selected');
expect(screen.queryByRole('button', { name: /^share$/i })).toBeNull();
} else {
expect.soft(onVerseSelect).not.toHaveBeenCalled();
const selectedVerse = scenario === 'reselected same verse' ? 1 : 2;
expect.soft(getVerseEl(view.container, selectedVerse)).toHaveClass('yv-v-selected');
expect.soft(screen.queryByRole('button', { name: /^share$/i })).not.toBeNull();
}
});
7 changes: 6 additions & 1 deletion packages/ui/src/components/bible-reader.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -1184,9 +1184,14 @@ function Content() {
}
const text = data?.text ?? '';
if (text && globalThis.navigator?.share instanceof Function) {
// Each selection update replaces this array, even when the same verses are reselected.
const sharedSelection = lastSelectionRef.current;
globalThis.navigator
.share({ text })
.then(() => closeAndClearSelection())
.then(() => {
if (lastSelectionRef.current !== sharedSelection) return;
closeAndClearSelectionRef.current();
})
.catch(() => {
// Cancelled or failed — keep the popover open (AC 4).
});
Expand Down
Loading