|
| 1 | +import { |
| 2 | + describe, |
| 3 | + it, |
| 4 | + expect, |
| 5 | + render, |
| 6 | + waitFor, |
| 7 | + cleanup, |
| 8 | +} from 'react-native-harness'; |
| 9 | +import { useEffect } from 'react'; |
| 10 | +import { ScreenContainer, Screen } from 'react-native-screens'; |
| 11 | +import { |
| 12 | + RiveView, |
| 13 | + RiveFileFactory, |
| 14 | + Fit, |
| 15 | + type RiveFile, |
| 16 | + type RiveViewRef, |
| 17 | +} from '@rive-app/react-native'; |
| 18 | +import type { ViewModelInstance } from '@rive-app/react-native'; |
| 19 | + |
| 20 | +const QUICK_START = require('../assets/rive/quick_start.riv'); |
| 21 | + |
| 22 | +const SCREEN_INACTIVE = 0 as const; |
| 23 | +const SCREEN_ACTIVE = 2 as const; |
| 24 | + |
| 25 | +function expectDefined<T>(value: T): asserts value is NonNullable<T> { |
| 26 | + expect(value).toBeDefined(); |
| 27 | +} |
| 28 | + |
| 29 | +type TestContext = { |
| 30 | + ref: RiveViewRef | null; |
| 31 | + error: string | null; |
| 32 | +}; |
| 33 | + |
| 34 | +function ScreenWithRive({ |
| 35 | + file, |
| 36 | + instance, |
| 37 | + context, |
| 38 | + activityState, |
| 39 | +}: { |
| 40 | + file: RiveFile; |
| 41 | + instance: ViewModelInstance; |
| 42 | + context: TestContext; |
| 43 | + activityState: typeof SCREEN_INACTIVE | typeof SCREEN_ACTIVE; |
| 44 | +}) { |
| 45 | + useEffect(() => { |
| 46 | + return () => { |
| 47 | + context.ref = null; |
| 48 | + }; |
| 49 | + }, [context]); |
| 50 | + |
| 51 | + return ( |
| 52 | + <ScreenContainer style={{ width: 200, height: 200 }}> |
| 53 | + <Screen activityState={activityState} style={{ flex: 1 }}> |
| 54 | + <RiveView |
| 55 | + hybridRef={{ |
| 56 | + f: (ref: RiveViewRef | null) => { |
| 57 | + context.ref = ref; |
| 58 | + }, |
| 59 | + }} |
| 60 | + style={{ flex: 1 }} |
| 61 | + file={file} |
| 62 | + autoPlay={false} |
| 63 | + dataBind={instance} |
| 64 | + fit={Fit.Contain} |
| 65 | + stateMachineName="State Machine 1" |
| 66 | + onError={(e) => { |
| 67 | + context.error = e.message; |
| 68 | + }} |
| 69 | + /> |
| 70 | + </Screen> |
| 71 | + </ScreenContainer> |
| 72 | + ); |
| 73 | +} |
| 74 | + |
| 75 | +describe('navigation lifecycle (PR #46)', () => { |
| 76 | + it('RiveView preserves state after screen detach/reattach', async () => { |
| 77 | + const file = await RiveFileFactory.fromSource(QUICK_START, undefined); |
| 78 | + const vm = file.defaultArtboardViewModel(); |
| 79 | + expectDefined(vm); |
| 80 | + const instance = vm.createDefaultInstance(); |
| 81 | + expectDefined(instance); |
| 82 | + |
| 83 | + const context: TestContext = { ref: null, error: null }; |
| 84 | + |
| 85 | + const { rerender } = await render( |
| 86 | + <ScreenWithRive |
| 87 | + file={file} |
| 88 | + instance={instance} |
| 89 | + context={context} |
| 90 | + activityState={SCREEN_ACTIVE} |
| 91 | + /> |
| 92 | + ); |
| 93 | + |
| 94 | + await waitFor( |
| 95 | + () => { |
| 96 | + expect(context.ref).not.toBeNull(); |
| 97 | + }, |
| 98 | + { timeout: 5000 } |
| 99 | + ); |
| 100 | + await context.ref!.awaitViewReady(); |
| 101 | + |
| 102 | + // Set health to 75 via the native view's VMI |
| 103 | + const boundVmi = context.ref!.getViewModelInstance(); |
| 104 | + expectDefined(boundVmi); |
| 105 | + const health = boundVmi.numberProperty('health'); |
| 106 | + expectDefined(health); |
| 107 | + health.value = 75; |
| 108 | + |
| 109 | + // Detach screen (simulates navigating away — triggers onDetachedFromWindow) |
| 110 | + await rerender( |
| 111 | + <ScreenWithRive |
| 112 | + file={file} |
| 113 | + instance={instance} |
| 114 | + context={context} |
| 115 | + activityState={SCREEN_INACTIVE} |
| 116 | + /> |
| 117 | + ); |
| 118 | + await new Promise((r) => setTimeout(r, 300)); |
| 119 | + |
| 120 | + // Reattach screen (simulates navigating back) |
| 121 | + await rerender( |
| 122 | + <ScreenWithRive |
| 123 | + file={file} |
| 124 | + instance={instance} |
| 125 | + context={context} |
| 126 | + activityState={SCREEN_ACTIVE} |
| 127 | + /> |
| 128 | + ); |
| 129 | + await new Promise((r) => setTimeout(r, 300)); |
| 130 | + |
| 131 | + // Query the VMI from the native view after reattach |
| 132 | + expect(context.error).toBeNull(); |
| 133 | + const reattachedVmi = context.ref!.getViewModelInstance(); |
| 134 | + expectDefined(reattachedVmi); |
| 135 | + const reattachedHealth = reattachedVmi.numberProperty('health'); |
| 136 | + expectDefined(reattachedHealth); |
| 137 | + expect(reattachedHealth.value).toBe(75); |
| 138 | + |
| 139 | + cleanup(); |
| 140 | + }); |
| 141 | +}); |
0 commit comments