Skip to content
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

Mock window dimensions #6235

Merged
merged 12 commits into from
Jul 17, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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 @@ -180,6 +180,14 @@ export async function unmockAnimationTimer() {
await testRunner.unmockAnimationTimer();
}

export async function mockWindowDimensions() {
await testRunner.mockWindowDimensions();
}

export async function unmockWindowDimensions() {
await testRunner.unmockWindowDimensions();
}

export async function setAnimationTimestamp(timestamp: number) {
await testRunner.setAnimationTimestamp(timestamp);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,18 @@ import type {
import { ComparisonMode, DescribeDecorator, TestDecorator } from './types';
import { TestComponent } from './TestComponent';
import { EMPTY_LOG_PLACEHOLDER, applyMarkdown, color, formatString, indentNestingLevel } from './stringFormatUtils';
import type { SharedValue } from 'react-native-reanimated';
import type {
SharedValue,
LayoutAnimationStartFunction,
LayoutAnimationType,
SharedTransitionAnimationsValues,
LayoutAnimation,
} from 'react-native-reanimated';
import { makeMutable, runOnUI, runOnJS } from 'react-native-reanimated';
import { Matchers, nullableMatch } from './matchers/Matchers';
import { assertMockedAnimationTimestamp, assertTestCase, assertTestSuite } from './Asserts';
import { createUpdatesContainer } from './UpdatesContainer';
export { Presets } from './Presets';

let callTrackerRegistryJS: Record<string, number> = {};
const callTrackerRegistryUI = makeMutable<Record<string, number>>({});
Expand Down Expand Up @@ -558,6 +565,46 @@ export class TestRunner {
});
}

public async unmockWindowDimensions() {
await this.runOnUIBlocking(() => {
'worklet';
if (global._LayoutAnimationsManager) {
global.LayoutAnimationsManager = global._LayoutAnimationsManager;
}
});
}

public async mockWindowDimensions() {
await this.runOnUIBlocking(() => {
'worklet';
const originalCreateAnimatedComponent = global.LayoutAnimationsManager;
Latropos marked this conversation as resolved.
Show resolved Hide resolved

const createAnimatedComponentOnStart: LayoutAnimationStartFunction = (
Latropos marked this conversation as resolved.
Show resolved Hide resolved
tag: number,
type: LayoutAnimationType,
_yogaValues: Partial<SharedTransitionAnimationsValues>,
config: (arg: Partial<SharedTransitionAnimationsValues>) => LayoutAnimation,
) => {
originalCreateAnimatedComponent.start(
tag,
type,
{
..._yogaValues,
windowHeight: 852,
windowWidth: 393,
},
config,
);
};

global._LayoutAnimationsManager = originalCreateAnimatedComponent;
Latropos marked this conversation as resolved.
Show resolved Hide resolved
global.LayoutAnimationsManager = {
start: createAnimatedComponentOnStart,
Latropos marked this conversation as resolved.
Show resolved Hide resolved
stop: originalCreateAnimatedComponent.stop,
};
});
}

public wait(delay: number) {
return new Promise(resolve => {
setTimeout(resolve, delay);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { Component, Dispatch, MutableRefObject, ReactNode, SetStateAction } from 'react';
import type { AnimatedStyle, StyleProps } from 'react-native-reanimated';
import type { AnimatedStyle, StyleProps, LayoutAnimationStartFunction } from 'react-native-reanimated';

export type CallTracker = {
UICallsCount: number;
Expand Down Expand Up @@ -137,6 +137,14 @@ declare global {
var _obtainPropPaper: (viewTag: number, propName: string) => string;
var _obtainPropFabric: (shadowNodeWrapper: unknown, propName: string) => string;
var __flushAnimationFrame: (frameTimestamp: number) => void;
var LayoutAnimationsManager: {
start: LayoutAnimationStartFunction;
stop: (tag: number) => void;
};
var _LayoutAnimationsManager: {
start: LayoutAnimationStartFunction;
stop: (tag: number) => void;
};
}
/* eslint-enable no-var */

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ import {
wait,
unmockAnimationTimer,
clearRenderOutput,
mockWindowDimensions,
unmockWindowDimensions,
} from '../../../ReanimatedRuntimeTestsRunner/RuntimeTestsApi';
import {
DurationEnteringSnapshots,
Expand Down Expand Up @@ -101,6 +103,7 @@ const EnteringOnMountComponent = ({ entering }: { entering: any }) => {

async function getSnapshotUpdates(entering: any, waitTime: number, duration: number | undefined, springify = false) {
await mockAnimationTimer();
await mockWindowDimensions();

const updatesContainer = await recordAnimationUpdates();
const springEntering = springify ? entering : entering.springify();
Expand All @@ -109,7 +112,9 @@ async function getSnapshotUpdates(entering: any, waitTime: number, duration: num
await render(<EnteringOnMountComponent entering={componentEntering} />);
await wait(waitTime);
const updates = updatesContainer.getUpdates();

await unmockAnimationTimer();
await unmockWindowDimensions();
await clearRenderOutput();

return updates;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ import {
wait,
unmockAnimationTimer,
clearRenderOutput,
mockWindowDimensions,
unmockWindowDimensions,
} from '../../../ReanimatedRuntimeTestsRunner/RuntimeTestsApi';
import { DurationExitingSnapshots, NoModifierExitingSnapshots, SpringifyExitingSnapshots } from './exiting.snapshot';

Expand Down Expand Up @@ -99,6 +101,7 @@ const ExitingComponent = ({ exiting }: { exiting: any }) => {

async function getSnapshotUpdates(exiting: any, waitTime: number, duration: number | undefined, springify = false) {
await mockAnimationTimer();
await mockWindowDimensions();

const updatesContainer = await recordAnimationUpdates();
const springExiting = springify ? exiting : exiting.springify();
Expand All @@ -108,7 +111,9 @@ async function getSnapshotUpdates(exiting: any, waitTime: number, duration: numb

await wait(waitTime);
const updates = updatesContainer.getUpdates();

await unmockAnimationTimer();
await unmockWindowDimensions();
await clearRenderOutput();

return updates;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ import {
unmockAnimationTimer,
clearRenderOutput,
getTestComponent,
mockWindowDimensions,
unmockWindowDimensions,
} from '../../../ReanimatedRuntimeTestsRunner/RuntimeTestsApi';
import {
CurvedSnapshot,
Expand All @@ -32,6 +34,7 @@ import { Direction, TransitionUpOrDown, TransitionLeftOrRight, TRANSITION_REF }

async function getSnapshotUpdates(layout: any, direction: Direction, waitTime: number) {
await mockAnimationTimer();
await mockWindowDimensions();

const updatesContainer = await recordAnimationUpdates();
if (direction === Direction.UP || direction === Direction.DOWN) {
Expand All @@ -42,7 +45,9 @@ async function getSnapshotUpdates(layout: any, direction: Direction, waitTime: n
await wait(waitTime);
const component = getTestComponent(TRANSITION_REF);
const updates = updatesContainer.getUpdates(component);

await unmockAnimationTimer();
await unmockWindowDimensions();
await clearRenderOutput();

return updates;
Expand Down
3 changes: 3 additions & 0 deletions packages/react-native-reanimated/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,9 @@ export type {
EntryExitAnimationFunction,
LayoutAnimationsValues,
LayoutAnimationFunction,
LayoutAnimationStartFunction,
LayoutAnimationType,
SharedTransitionAnimationsValues,
ILayoutAnimationBuilder,
IEntryExitAnimationBuilder,
} from './layoutReanimation';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ export type LayoutAnimationStartFunction = (
tag: number,
type: LayoutAnimationType,
yogaValues: Partial<SharedTransitionAnimationsValues>,
config: LayoutAnimationFunction
config: (arg: Partial<SharedTransitionAnimationsValues>) => LayoutAnimation
piaskowyk marked this conversation as resolved.
Show resolved Hide resolved
) => void;

export interface ILayoutAnimationBuilder {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,5 @@ export type {
BaseBuilderAnimationConfig,
LayoutAnimationAndConfig,
IEntryExitAnimationBuilder,
SharedTransitionAnimationsValues,
} from './commonTypes';
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { runOnUIImmediately } from '../threads';
import type {
SharedTransitionAnimationsValues,
LayoutAnimation,
LayoutAnimationStartFunction,
} from './animationBuilder/commonTypes';

const TAG_OFFSET = 1e9;
Expand Down Expand Up @@ -34,7 +35,10 @@ function stopObservingProgress(
global._notifyAboutEnd(tag, removeView);
}

function createLayoutAnimationManager() {
function createLayoutAnimationManager(): {
start: LayoutAnimationStartFunction;
stop: (tag: number) => void;
} {
piaskowyk marked this conversation as resolved.
Show resolved Hide resolved
'worklet';
const currentAnimationForTag = new Map();
const mutableValuesForTag = new Map();
Expand Down