diff --git a/.changeset/reader-native-share-selection.md b/.changeset/reader-native-share-selection.md new file mode 100644 index 000000000..6a8261103 --- /dev/null +++ b/.changeset/reader-native-share-selection.md @@ -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. diff --git a/packages/ui/src/components/bible-reader-controlled.test.tsx b/packages/ui/src/components/bible-reader-controlled.test.tsx index 9b25e793e..9e3251e9c 100644 --- a/packages/ui/src/components/bible-reader-controlled.test.tsx +++ b/packages/ui/src/components/bible-reader-controlled.test.tsx @@ -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, @@ -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((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); + 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(); + } +}); diff --git a/packages/ui/src/components/bible-reader.tsx b/packages/ui/src/components/bible-reader.tsx index bea912f61..ea394fc77 100644 --- a/packages/ui/src/components/bible-reader.tsx +++ b/packages/ui/src/components/bible-reader.tsx @@ -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). });