|
| 1 | +import * as React from 'react' |
| 2 | +import { expect, test } from 'vitest' |
| 3 | +import { page } from 'vitest/browser' |
| 4 | +import { render } from 'vitest-browser-react' |
| 5 | +import { useApplyNativeVideoPreferences } from '#app/components/native-video-preferences.ts' |
| 6 | + |
| 7 | +function LegacyHydrationDelayedVideoPreferences() { |
| 8 | + const playbackRate = 1.75 |
| 9 | + const volumeRate = 0.25 |
| 10 | + const shouldUseOfflineVideo = true |
| 11 | + const nativeVideoRef = React.useRef<HTMLVideoElement>(null) |
| 12 | + const [isHydrated, setIsHydrated] = React.useState(false) |
| 13 | + const [snapshot, setSnapshot] = React.useState('legacy:pending') |
| 14 | + |
| 15 | + React.useEffect(() => { |
| 16 | + setIsHydrated(true) |
| 17 | + }, []) |
| 18 | + |
| 19 | + React.useEffect(() => { |
| 20 | + if (!nativeVideoRef.current) return |
| 21 | + if (typeof playbackRate === 'number') { |
| 22 | + nativeVideoRef.current.playbackRate = playbackRate |
| 23 | + } |
| 24 | + if (typeof volumeRate === 'number') { |
| 25 | + nativeVideoRef.current.volume = volumeRate |
| 26 | + } |
| 27 | + }, [playbackRate, volumeRate, shouldUseOfflineVideo]) |
| 28 | + |
| 29 | + React.useEffect(() => { |
| 30 | + if (!isHydrated || !nativeVideoRef.current) return |
| 31 | + setSnapshot( |
| 32 | + `legacy:${nativeVideoRef.current.playbackRate.toFixed(2)}|${nativeVideoRef.current.volume.toFixed(2)}`, |
| 33 | + ) |
| 34 | + }, [isHydrated]) |
| 35 | + |
| 36 | + return ( |
| 37 | + <div> |
| 38 | + {isHydrated ? ( |
| 39 | + <video ref={nativeVideoRef} aria-label="legacy-video" /> |
| 40 | + ) : ( |
| 41 | + <span>legacy-fallback</span> |
| 42 | + )} |
| 43 | + <span>{snapshot}</span> |
| 44 | + </div> |
| 45 | + ) |
| 46 | +} |
| 47 | + |
| 48 | +function FixedHydrationDelayedVideoPreferences() { |
| 49 | + const playerPreferences = { playbackRate: 1.75, volumeRate: 0.25 } |
| 50 | + const shouldUseOfflineVideo = true |
| 51 | + const nativeVideoRef = React.useRef<HTMLVideoElement>(null) |
| 52 | + const [nativeVideoElement, setNativeVideoElement] = |
| 53 | + React.useState<HTMLVideoElement | null>(null) |
| 54 | + const [isHydrated, setIsHydrated] = React.useState(false) |
| 55 | + const [snapshot, setSnapshot] = React.useState('fixed:pending') |
| 56 | + const setNativeVideoRef = React.useCallback( |
| 57 | + (element: HTMLVideoElement | null) => { |
| 58 | + nativeVideoRef.current = element |
| 59 | + setNativeVideoElement(element) |
| 60 | + }, |
| 61 | + [], |
| 62 | + ) |
| 63 | + |
| 64 | + useApplyNativeVideoPreferences({ |
| 65 | + shouldApply: shouldUseOfflineVideo, |
| 66 | + videoElement: nativeVideoElement, |
| 67 | + playerPreferences, |
| 68 | + }) |
| 69 | + |
| 70 | + React.useEffect(() => { |
| 71 | + setIsHydrated(true) |
| 72 | + }, []) |
| 73 | + |
| 74 | + React.useEffect(() => { |
| 75 | + if (!nativeVideoElement || !nativeVideoRef.current) return |
| 76 | + setSnapshot( |
| 77 | + `fixed:${nativeVideoRef.current.playbackRate.toFixed(2)}|${nativeVideoRef.current.volume.toFixed(2)}`, |
| 78 | + ) |
| 79 | + }, [nativeVideoElement]) |
| 80 | + |
| 81 | + return ( |
| 82 | + <div> |
| 83 | + {isHydrated ? ( |
| 84 | + <video ref={setNativeVideoRef} aria-label="fixed-video" /> |
| 85 | + ) : ( |
| 86 | + <span>fixed-fallback</span> |
| 87 | + )} |
| 88 | + <span>{snapshot}</span> |
| 89 | + </div> |
| 90 | + ) |
| 91 | +} |
| 92 | + |
| 93 | +test('legacy ref-only effect misses preferences after hydration (aha)', async () => { |
| 94 | + await render(<LegacyHydrationDelayedVideoPreferences />) |
| 95 | + |
| 96 | + await expect.element(page.getByText('legacy:1.00|1.00')).toBeVisible() |
| 97 | +}) |
| 98 | + |
| 99 | +test('applies preferences when delayed video element mounts', async () => { |
| 100 | + await render(<FixedHydrationDelayedVideoPreferences />) |
| 101 | + |
| 102 | + await expect.element(page.getByText('fixed:1.75|0.25')).toBeVisible() |
| 103 | +}) |
0 commit comments