Skip to content

Commit 2e3db25

Browse files
sammy-SCfacebook-github-bot
authored andcommitted
Add Fantom integration test for createAnimatedPropsHook
Summary: Converts the ref-callback stability tests to Fantom, replacing react-test-renderer create/update with Fantom.createRoot() + Fantom.runTask(). Tests verify same-reference and new-reference behavior for ref callbacks across re-renders. Changelog: [Internal] Differential Revision: D94887732
1 parent 09c0f6c commit 2e3db25

2 files changed

Lines changed: 122 additions & 84 deletions

File tree

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
/**
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*
7+
* @flow strict-local
8+
* @format
9+
*/
10+
11+
import '@react-native/fantom/src/setUpDefaultReactNativeEnvironment';
12+
13+
import {AnimatedEvent} from '../../../../Libraries/Animated/AnimatedEvent';
14+
import createAnimatedPropsHook from '../createAnimatedPropsHook';
15+
import * as Fantom from '@react-native/fantom';
16+
import * as React from 'react';
17+
import {useLayoutEffect} from 'react';
18+
19+
describe('useAnimatedProps', () => {
20+
it('returns the same ref callback when `props` changes', () => {
21+
const useAnimatedProps = createAnimatedPropsHook(null);
22+
23+
const refs: Array<mixed> = [];
24+
function Sentinel(props: {[string]: mixed}): React.Node {
25+
// $FlowFixMe[underconstrained-implicit-instantiation]
26+
const [, ref] = useAnimatedProps(props);
27+
useLayoutEffect(() => {
28+
refs.push(ref);
29+
}, [ref]);
30+
return null;
31+
}
32+
33+
const root = Fantom.createRoot();
34+
35+
Fantom.runTask(() => {
36+
root.render(<Sentinel foo={1} />);
37+
});
38+
expect(refs.length).toBe(1);
39+
expect(typeof refs[0]).toBe('function');
40+
41+
Fantom.runTask(() => {
42+
root.render(<Sentinel foo={2} />);
43+
});
44+
expect(refs.length).toBe(1);
45+
46+
Fantom.runTask(() => {
47+
root.destroy();
48+
});
49+
Fantom.runWorkLoop();
50+
});
51+
52+
it('returns the same ref callback when `AnimatedEvent` is the same', () => {
53+
const useAnimatedProps = createAnimatedPropsHook(null);
54+
55+
const refs: Array<mixed> = [];
56+
function Sentinel(props: {[string]: mixed}): React.Node {
57+
// $FlowFixMe[underconstrained-implicit-instantiation]
58+
const [, ref] = useAnimatedProps(props);
59+
useLayoutEffect(() => {
60+
refs.push(ref);
61+
}, [ref]);
62+
return null;
63+
}
64+
65+
const event = new AnimatedEvent([{}], {useNativeDriver: true});
66+
67+
const root = Fantom.createRoot();
68+
69+
Fantom.runTask(() => {
70+
root.render(<Sentinel foo={event} />);
71+
});
72+
expect(refs.length).toBe(1);
73+
expect(typeof refs[0]).toBe('function');
74+
75+
Fantom.runTask(() => {
76+
root.render(<Sentinel foo={event} />);
77+
});
78+
expect(refs.length).toBe(1);
79+
80+
Fantom.runTask(() => {
81+
root.destroy();
82+
});
83+
Fantom.runWorkLoop();
84+
});
85+
86+
it('returns a new ref callback when `AnimatedEvent` changes', () => {
87+
const useAnimatedProps = createAnimatedPropsHook(null);
88+
89+
const refs: Array<mixed> = [];
90+
function Sentinel(props: {[string]: mixed}): React.Node {
91+
// $FlowFixMe[underconstrained-implicit-instantiation]
92+
const [, ref] = useAnimatedProps(props);
93+
useLayoutEffect(() => {
94+
refs.push(ref);
95+
}, [ref]);
96+
return null;
97+
}
98+
99+
const eventA = new AnimatedEvent([{}], {useNativeDriver: true});
100+
const eventB = new AnimatedEvent([{}], {useNativeDriver: true});
101+
102+
const root = Fantom.createRoot();
103+
104+
Fantom.runTask(() => {
105+
root.render(<Sentinel foo={eventA} />);
106+
});
107+
expect(refs.length).toBe(1);
108+
expect(typeof refs[0]).toBe('function');
109+
110+
Fantom.runTask(() => {
111+
root.render(<Sentinel foo={eventB} />);
112+
});
113+
expect(refs.length).toBe(2);
114+
expect(typeof refs[1]).toBe('function');
115+
expect(refs[0]).not.toBe(refs[1]);
116+
117+
Fantom.runTask(() => {
118+
root.destroy();
119+
});
120+
Fantom.runWorkLoop();
121+
});
122+
});

packages/react-native/src/private/animated/__tests__/createAnimatedPropsHook-test.js

Lines changed: 0 additions & 84 deletions
This file was deleted.

0 commit comments

Comments
 (0)