-
Notifications
You must be signed in to change notification settings - Fork 3
feat(ui): abstract Bible reader theme settings for RN #227
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
Dustin-Kelley
wants to merge
11
commits into
main
Choose a base branch
from
YPE-2258-react-sdk-abstract-bible-theme-settings-component-view-for-rn-expo-sdk
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
11 commits
Select commit
Hold shift + click to select a range
69560eb
feat(ui): abstract Bible reader theme settings for RN
Dustin-Kelley cb402c5
refactor(ui): rename applySettings to applyThemeSettings in Toolbar f…
Dustin-Kelley 74d1d2f
feat(ui): expose BibleThemeSettingsContent and add onOpenBibleThemeSe…
Dustin-Kelley bd231fb
refactor(ui): rename settingsValuesRef to themesSettingsValuesRef in …
Dustin-Kelley 8f1caa5
fix(ui): add theme prop to BibleThemeSettingsContent for dynamic theming
Dustin-Kelley 7b3def3
refactor(ui): make BibleReader theme settings Expo DOM-safe
Dustin-Kelley ac51da5
refactor(ui): clean up BibleReader component by removing unused comme…
Dustin-Kelley 4bc911a
fix(ui): prevent multiple theme settings hydration in BibleReader com…
Dustin-Kelley 3fff272
fix(ui): disable theme font-size controls at bounds
Dustin-Kelley d72888b
test(ui): cover onFontDecreased handler in BibleReader
Dustin-Kelley 816a716
test(ui): ensure font-size controls are disabled at bounds in BibleRe…
Dustin-Kelley 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| '@youversion/platform-react-ui': minor | ||
| --- | ||
|
|
||
| Expose `BibleThemeSettingsContent` and add optional `onOpenBibleThemeSettings` on `BibleReader.Toolbar` with a **serializable** `BibleThemeSettingsSnapshot` payload for Expo DOM hosts. Export `BIBLE_READER_FONT`, `clampBibleReaderFontSize`, `nextBibleReaderFontSizeUp`, and `nextBibleReaderFontSizeDown` so native can apply edits without closure payloads. Add optional controlled typography on `BibleReader.Root` via `fontSize`/`fontFamily` with `onFontSizeChange`/`onFontFamilyChange` (and `defaultFontSize` / `defaultFontFamily` for defaults). |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,266 @@ | ||
| /** | ||
| * @vitest-environment jsdom | ||
| */ | ||
| // We stub ResizeObserver for jsdom (used by Radix/@floating-ui). The stub methods are intentionally no-ops. | ||
| /* eslint-disable @typescript-eslint/no-empty-function */ | ||
| import { describe, it, expect, vi, beforeEach } from 'vitest'; | ||
| import { render, screen, waitFor } from '@testing-library/react'; | ||
| import userEvent from '@testing-library/user-event'; | ||
| import { useState } from 'react'; | ||
| import type { BibleBook, BibleVersion, Language } from '@youversion/platform-core'; | ||
| import { | ||
| useBooks, | ||
| useFilteredVersions, | ||
| useLanguage, | ||
| useLanguages, | ||
| useTheme, | ||
| useVersion, | ||
| useVersions, | ||
| } from '@youversion/platform-react-hooks'; | ||
| import { | ||
| BibleReader, | ||
| clampBibleReaderFontSize, | ||
| createBibleThemeSettingsContentHandlers, | ||
| nextBibleReaderFontSizeDown, | ||
| nextBibleReaderFontSizeUp, | ||
| type BibleThemeSettingsSnapshot, | ||
| } from './bible-reader'; | ||
| import { INTER_FONT, SOURCE_SERIF_FONT, type FontFamily } from '@/lib/verse-html-utils'; | ||
|
|
||
| class ResizeObserverMock { | ||
| observe() {} | ||
| unobserve() {} | ||
| disconnect() {} | ||
| } | ||
| globalThis.ResizeObserver = ResizeObserverMock as unknown as typeof ResizeObserver; | ||
|
|
||
| vi.mock('@youversion/platform-react-hooks', async () => { | ||
| const actual = await vi.importActual('@youversion/platform-react-hooks'); | ||
| return { | ||
| ...actual, | ||
| useBooks: vi.fn(), | ||
| useFilteredVersions: vi.fn(), | ||
| useLanguage: vi.fn(), | ||
| useLanguages: vi.fn(), | ||
| useTheme: vi.fn(), | ||
| useVersion: vi.fn(), | ||
| useVersions: vi.fn(), | ||
| }; | ||
| }); | ||
|
|
||
| const mockBooks: BibleBook[] = [ | ||
| { | ||
| id: 'JHN', | ||
| title: 'John', | ||
| full_title: 'The Gospel According to John', | ||
| canon: 'new_testament', | ||
| abbreviation: 'John', | ||
| chapters: [ | ||
| { id: '1', title: '1', passage_id: 'JHN.1' }, | ||
| { id: '2', title: '2', passage_id: 'JHN.2' }, | ||
| ], | ||
| }, | ||
| ]; | ||
|
|
||
| const mockVersion = { | ||
| id: 3034, | ||
| localized_abbreviation: 'BSB', | ||
| abbreviation: 'BSB', | ||
| title: 'Berean Standard Bible', | ||
| language_tag: 'en', | ||
| } as BibleVersion; | ||
|
|
||
| function setupDefaultMocks() { | ||
| vi.mocked(useTheme).mockReturnValue('light'); | ||
| vi.mocked(useBooks).mockReturnValue({ | ||
| books: { data: [...mockBooks], next_page_token: null }, | ||
| loading: false, | ||
| error: null, | ||
| refetch: vi.fn(), | ||
| }); | ||
| vi.mocked(useVersion).mockReturnValue({ | ||
| version: mockVersion, | ||
| loading: false, | ||
| error: null, | ||
| refetch: vi.fn(), | ||
| }); | ||
| vi.mocked(useLanguages).mockReturnValue({ | ||
| languages: { data: [] as Language[], next_page_token: null }, | ||
| loading: false, | ||
| error: null, | ||
| refetch: vi.fn(), | ||
| }); | ||
| vi.mocked(useLanguage).mockReturnValue({ | ||
| language: { id: 'en', language: 'English', display_names: { en: 'English' } } as Language, | ||
| loading: false, | ||
| error: null, | ||
| refetch: vi.fn(), | ||
| }); | ||
| vi.mocked(useVersions).mockReturnValue({ | ||
| versions: { data: [], next_page_token: null }, | ||
| loading: false, | ||
| error: null, | ||
| refetch: vi.fn(), | ||
| }); | ||
| vi.mocked(useFilteredVersions).mockReturnValue([]); | ||
| } | ||
|
|
||
| describe('BibleReader font helpers', () => { | ||
| it('clamps font size to reader bounds', () => { | ||
| expect(clampBibleReaderFontSize(8)).toBe(12); | ||
| expect(clampBibleReaderFontSize(30)).toBe(20); | ||
| expect(clampBibleReaderFontSize(16)).toBe(16); | ||
| }); | ||
|
|
||
| it('steps up and down with clamping at bounds', () => { | ||
| expect(nextBibleReaderFontSizeUp(16)).toBe(18); | ||
| expect(nextBibleReaderFontSizeUp(20)).toBe(20); | ||
| expect(nextBibleReaderFontSizeDown(18)).toBe(16); | ||
| expect(nextBibleReaderFontSizeDown(12)).toBe(12); | ||
| }); | ||
| }); | ||
|
|
||
| describe('createBibleThemeSettingsContentHandlers', () => { | ||
| it('updates font size and family via host-owned setters', () => { | ||
| let fontSize = 16; | ||
| let fontFamily: FontFamily = SOURCE_SERIF_FONT; | ||
| const setFontSize = vi.fn((n: number) => { | ||
| fontSize = n; | ||
| }); | ||
| const setFontFamily = vi.fn((f: FontFamily) => { | ||
| fontFamily = f; | ||
| }); | ||
|
|
||
| const handlers = createBibleThemeSettingsContentHandlers({ | ||
| getFontSize: () => fontSize, | ||
|
Dustin-Kelley marked this conversation as resolved.
|
||
| getFontFamily: () => fontFamily, | ||
| setFontSize, | ||
| setFontFamily, | ||
| }); | ||
|
|
||
| handlers.onFontIncreased(); | ||
| expect(setFontSize).toHaveBeenCalledWith(18); | ||
|
|
||
| handlers.onFontDecreased(); | ||
| expect(setFontSize).toHaveBeenLastCalledWith(16); | ||
|
|
||
| handlers.onFontSelected(INTER_FONT); | ||
| expect(setFontFamily).toHaveBeenCalledWith(INTER_FONT); | ||
| }); | ||
| }); | ||
|
|
||
| describe('BibleReader theme settings', () => { | ||
| beforeEach(() => { | ||
| vi.clearAllMocks(); | ||
| localStorage.clear(); | ||
| setupDefaultMocks(); | ||
| }); | ||
|
|
||
| it('opens the default Reader Settings popover and updates font settings', async () => { | ||
| const user = userEvent.setup(); | ||
|
|
||
| render( | ||
| <BibleReader.Root defaultVersionId={3034} defaultBook="JHN" defaultChapter="1"> | ||
| <BibleReader.Toolbar /> | ||
| </BibleReader.Root>, | ||
| ); | ||
|
|
||
| await user.click(screen.getByRole('button', { name: 'Settings' })); | ||
|
|
||
| expect(await screen.findByText('Reader Settings')).toBeInTheDocument(); | ||
|
|
||
| await user.click(screen.getByTestId('increase-font-size')); | ||
| await waitFor(() => { | ||
| expect(localStorage.getItem('youversion-platform:reader:font-size')).toBe('18'); | ||
| }); | ||
|
|
||
| await user.click(screen.getByRole('button', { name: /inter/i })); | ||
| await waitFor(() => { | ||
| expect(localStorage.getItem('youversion-platform:reader:font-family')).toBe(INTER_FONT); | ||
| }); | ||
| }); | ||
|
|
||
| it('calls onOpenBibleThemeSettings with a serializable snapshot and skips popover content', async () => { | ||
| const user = userEvent.setup(); | ||
| const onOpenBibleThemeSettings = vi.fn(); | ||
|
|
||
| render( | ||
| <BibleReader.Root | ||
| defaultVersionId={3034} | ||
| defaultBook="JHN" | ||
| defaultChapter="1" | ||
| fontSize={18} | ||
| fontFamily={INTER_FONT} | ||
| > | ||
| <BibleReader.Toolbar onOpenBibleThemeSettings={onOpenBibleThemeSettings} /> | ||
| </BibleReader.Root>, | ||
| ); | ||
|
|
||
| await user.click(screen.getByRole('button', { name: 'Settings' })); | ||
|
|
||
| expect(onOpenBibleThemeSettings).toHaveBeenCalledTimes(1); | ||
| expect(screen.queryByText('Reader Settings')).not.toBeInTheDocument(); | ||
|
|
||
| const snapshot = onOpenBibleThemeSettings.mock.calls[0]![0] as BibleThemeSettingsSnapshot; | ||
| expect(snapshot).toEqual({ | ||
| fontSize: 18, | ||
| fontFamily: INTER_FONT, | ||
| minFontSize: 12, | ||
| maxFontSize: 20, | ||
| }); | ||
| }); | ||
|
|
||
| it('applies font updates via controlled props using snapshot and exported font math', async () => { | ||
| const user = userEvent.setup(); | ||
| const onOpen = vi.fn(); | ||
|
|
||
| function ControlledHost() { | ||
| const [fontSize, setFontSize] = useState(16); | ||
| const [fontFamily, setFontFamily] = useState<FontFamily>(SOURCE_SERIF_FONT); | ||
|
|
||
| return ( | ||
| <> | ||
| <button | ||
| type="button" | ||
| onClick={() => { | ||
| const snap = onOpen.mock.calls[0]?.[0] as BibleThemeSettingsSnapshot | undefined; | ||
| if (snap) { | ||
| setFontSize(nextBibleReaderFontSizeDown(snap.fontSize)); | ||
| setFontFamily(INTER_FONT); | ||
| } | ||
| }} | ||
| > | ||
| simulate-native-apply | ||
| </button> | ||
| <BibleReader.Root | ||
| defaultVersionId={3034} | ||
| defaultBook="JHN" | ||
| defaultChapter="1" | ||
| fontSize={fontSize} | ||
| fontFamily={fontFamily} | ||
| onFontSizeChange={setFontSize} | ||
| onFontFamilyChange={setFontFamily} | ||
| > | ||
| <BibleReader.Toolbar onOpenBibleThemeSettings={onOpen} /> | ||
| </BibleReader.Root> | ||
| </> | ||
| ); | ||
| } | ||
|
|
||
| render(<ControlledHost />); | ||
|
|
||
| await user.click(screen.getByRole('button', { name: 'Settings' })); | ||
| expect(onOpen).toHaveBeenCalledTimes(1); | ||
|
|
||
| await user.click(screen.getByRole('button', { name: 'simulate-native-apply' })); | ||
| await waitFor(() => { | ||
| expect(localStorage.getItem('youversion-platform:reader:font-size')).toBeNull(); | ||
| expect(localStorage.getItem('youversion-platform:reader:font-family')).toBeNull(); | ||
| }); | ||
|
|
||
| await user.click(screen.getByRole('button', { name: 'Settings' })); | ||
| const nextSnap = onOpen.mock.calls[1]![0] as BibleThemeSettingsSnapshot; | ||
| expect(nextSnap.fontSize).toBe(14); | ||
| expect(nextSnap.fontFamily).toBe(INTER_FONT); | ||
| }); | ||
| }); | ||
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.
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.
since we added 2
disabled={fontSize >= MAX_FONT_SIZE}props on the buttons inBibleSettingsContent, we need to reflect that change here.