Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -1012,3 +1012,384 @@ test('native scroll event does not step a running timing animation', () => {
mockClock.uninstall();
}
});

describe('static value set after a native-driver animation', () => {
function runFadeThenStatic(staticOpacity: number | void) {
let _fade;
let _animation;
let _setFading;

function MyApp() {
const fade = useAnimatedValue(1);
const [fading, setFading] = useState(false);
_fade = fade;
_setFading = setFading;
return (
<Animated.View
style={[
{width: 100, height: 100},
fading
? {opacity: fade}
: staticOpacity != null
? {opacity: staticOpacity}
: null,
]}
/>
);
}

const root = Fantom.createRoot();

Fantom.runTask(() => {
root.render(<MyApp />);
});

Fantom.runTask(() => {
_setFading(true);
});

Fantom.runTask(() => {
_animation = Animated.timing(_fade, {
toValue: 0.2,
duration: 100,
useNativeDriver: true,
}).start();
});

Fantom.unstable_produceFramesForDuration(100);

// Same as switching to a static style in the start() callback.
Fantom.runTask(() => {
_animation?.stop();
_setFading(false);
});

// Flush the work queued by stopping the animation.
Fantom.runWorkLoop();

return root;
}

test('a new static value replaces the animated value', () => {
const root = runFadeThenStatic(0.6);

expect(root.getRenderedOutput({props: ['opacity']}).toJSX()).toEqual(
<rn-view opacity="0.6" />,
);
});

test('going back to the pre-animation value replaces the animated value', () => {
const root = runFadeThenStatic(1);

expect(root.getRenderedOutput({props: ['opacity']}).toJSX()).toEqual(
<rn-view />,
);
});

test('removing the animated prop resets it to the default', () => {
const root = runFadeThenStatic(undefined);

expect(root.getRenderedOutput({props: ['opacity']}).toJSX()).toEqual(
<rn-view />,
);
});
});

describe('switching from a native-driver animation to static styles', () => {
// $FlowFixMe[unclear-type] Any style a test switches between.
type Style = any;
type StyleFor = (value: Animated.Value, animating: boolean) => Style;

// The expected output: the same style without an animation.
function renderStatic(style: Style, props: Array<string>) {
const root = Fantom.createRoot();
Fantom.runTask(() => {
root.render(
<View collapsable={false} style={[{width: 100, height: 100}, style]} />,
);
});
return JSON.stringify(root.getRenderedOutput({props}).toJSON());
}

function setUp(styleFor: StyleFor, viewCount: number = 1) {
let _value;
let _setAnimating;
const _setAnimatingByView: Array<(animating: boolean) => void> = [];

function Box({index}: {index: number}) {
const [animating, setAnimating] = useState(false);
_setAnimatingByView[index] = setAnimating;
return (
<Animated.View
collapsable={false}
style={[
{width: 100, height: 100},
styleFor(nullthrows(_value), animating),
]}
/>
);
}

function MyApp() {
_value = useAnimatedValue(0);
const [, rerender] = useState(0);
_setAnimating = rerender;
return (
<>
{Array.from({length: viewCount}, (_, index) => (
<Box key={index} index={index} />
))}
</>
);
}

const root = Fantom.createRoot();
Fantom.runTask(() => {
root.render(<MyApp />);
});

return {
root,
value: () => nullthrows(_value),
setAnimating: (animating: boolean, index: number = 0) =>
_setAnimatingByView[index](animating),
rerender: () => nullthrows(_setAnimating)(n => n + 1),
};
}

function animate(
value: Animated.Value,
toValue: number,
duration: number,
runFor: number = duration,
) {
let animation;
Fantom.runTask(() => {
animation = Animated.timing(value, {
toValue,
duration,
easing: Easing.linear,
useNativeDriver: true,
});
animation.start();
});
Fantom.unstable_produceFramesForDuration(runFor);
// The last frame shows up after the queued work runs.
Fantom.runWorkLoop();
return nullthrows(animation);
}

function fadeThenSwitch(
styleFor: StyleFor,
{runFor = 100}: {runFor?: number} = {},
) {
const app = setUp(styleFor);
Fantom.runTask(() => app.setAnimating(true));
const animation = animate(app.value(), 1, 100, runFor);
Fantom.runTask(() => {
animation.stop();
app.setAnimating(false);
});
Fantom.runWorkLoop();
return app;
}

const cases: Array<[string, StyleFor, Style, Array<string>]> = [
[
'transform to a static transform',
(v, a) => ({
transform: [
{
translateX: a
? v.interpolate({inputRange: [0, 1], outputRange: [0, 50]})
: 10,
},
],
}),
{transform: [{translateX: 10}]},
['transform'],
],
[
'transform removed',
(v, a) =>
a
? {
transform: [
{
translateX: v.interpolate({
inputRange: [0, 1],
outputRange: [0, 50],
}),
},
],
}
: {},
{},
['transform'],
],
[
'interpolated color to a static color',
(v, a) => ({
backgroundColor: a
? v.interpolate({inputRange: [0, 1], outputRange: ['red', 'blue']})
: 'green',
}),
{backgroundColor: 'green'},
['backgroundColor'],
],
[
'interpolated color removed',
(v, a) =>
a
? {
backgroundColor: v.interpolate({
inputRange: [0, 1],
outputRange: ['red', 'blue'],
}),
}
: {},
{},
['backgroundColor'],
],
[
'layout prop to a static value',
(v, a) => ({
marginLeft: a
? v.interpolate({inputRange: [0, 1], outputRange: [0, 50]})
: 5,
}),
{marginLeft: 5},
['marginLeft'],
],
[
'border width to a static value',
(v, a) => ({
borderWidth: a
? v.interpolate({inputRange: [0, 1], outputRange: [0, 20]})
: 4,
}),
{borderWidth: 4},
['borderWidth'],
],
[
'layout prop removed',
(v, a) =>
a
? {rowGap: v.interpolate({inputRange: [0, 1], outputRange: [0, 20]})}
: {},
{},
['rowGap'],
],
[
'interpolated opacity to a static value',
(v, a) => ({
opacity: a
? v.interpolate({inputRange: [0, 1], outputRange: [1, 0.2]})
: 0.6,
}),
{opacity: 0.6},
['opacity'],
],
];

for (const [name, styleFor, staticStyle, props] of cases) {
test(name, () => {
const {root} = fadeThenSwitch(styleFor);
expect(JSON.stringify(root.getRenderedOutput({props}).toJSON())).toEqual(
renderStatic(staticStyle, props),
);
});
}

test('switching halfway through the animation', () => {
const {root} = fadeThenSwitch((v, a) => ({opacity: a ? v : 0.6}), {
runFor: 50,
});
expect(root.getRenderedOutput({props: ['opacity']}).toJSX()).toEqual(
<rn-view opacity="0.6" />,
);
});

test('a prop that stays animated keeps animating', () => {
const app = fadeThenSwitch((v, a) => ({
opacity: a ? v : 0.6,
transform: [
{
translateX: v.interpolate({
inputRange: [0, 2],
outputRange: [0, 100],
}),
},
],
}));
animate(app.value(), 2, 100);
expect(
app.root.getRenderedOutput({props: ['opacity', 'transform']}).toJSON(),
).toEqual(
JSON.parse(
renderStatic({opacity: 0.6, transform: [{translateX: 100}]}, [
'opacity',
'transform',
]),
),
);
});

test('another view driven by the same value keeps animating', () => {
const app = setUp((v, a) => ({opacity: a ? v : 0.6}), 2);
Fantom.runTask(() => {
app.setAnimating(true, 0);
app.setAnimating(true, 1);
});
const animation = animate(app.value(), 0.2, 100);
Fantom.runTask(() => {
animation.stop();
app.setAnimating(false, 0);
});
Fantom.runWorkLoop();
expect(
JSON.stringify(app.root.getRenderedOutput({props: ['opacity']}).toJSON()),
).toEqual(
'[{"type":"View","props":{"opacity":"0.6"},"children":[]},{"type":"View","props":{"opacity":"0.2"},"children":[]}]',
);

animate(app.value(), 0.8, 100);
expect(
JSON.stringify(app.root.getRenderedOutput({props: ['opacity']}).toJSON()),
).toEqual(
'[{"type":"View","props":{"opacity":"0.6"},"children":[]},{"type":"View","props":{"opacity":"0.8"},"children":[]}]',
);
});

test('re-rendering while animating keeps the animated value', () => {
const app = setUp((v, a) => ({opacity: a ? v : 0.6}));
Fantom.runTask(() => app.setAnimating(true));
animate(app.value(), 0.2, 100, 50);
Fantom.runTask(() => app.rerender());
Fantom.unstable_produceFramesForDuration(50);
Fantom.runWorkLoop();
expect(app.root.getRenderedOutput({props: ['opacity']}).toJSX()).toEqual(
<rn-view opacity="0.2" />,
);
});

test('animating and switching back twice', () => {
const app = fadeThenSwitch((v, a) => ({opacity: a ? v : 0.6}));
Fantom.runTask(() => {
app.value().setValue(0);
app.setAnimating(true);
});
const animation = animate(app.value(), 0.3, 100);
expect(app.root.getRenderedOutput({props: ['opacity']}).toJSX()).toEqual(
<rn-view opacity="0.3" />,
);
Fantom.runTask(() => {
animation.stop();
app.setAnimating(false);
});
Fantom.runWorkLoop();
expect(app.root.getRenderedOutput({props: ['opacity']}).toJSX()).toEqual(
<rn-view opacity="0.6" />,
);
});
});
Loading