Skip to content

Commit

Permalink
Extract mockWindowDimensions from TestRunner (#6343)
Browse files Browse the repository at this point in the history
## Summary
Extract `mockWindowDimensions` and `unmockWindowDimensions` from
`TestRunner`
## Test plan
  • Loading branch information
Latropos authored Jul 31, 2024
1 parent a85657a commit 0616843
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 48 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { DescribeDecorator, TestDecorator } from './types';
export { Presets } from './Presets';

const testRunner = new TestRunner();
const windowDimensionsMocker = testRunner.getWindowDimensionsMocker();
const animationRecorder = testRunner.getAnimationUpdatesRecorder();
const valueRegistry = testRunner.getValueRegistry();

Expand Down Expand Up @@ -179,11 +180,11 @@ export async function unmockAnimationTimer() {
}

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

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

export async function recordAnimationUpdates() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,13 @@ import type { BuildFunction, TestCase, TestConfiguration, TestSuite, TestValue,
import { DescribeDecorator, TestDecorator } from '../types';
import { TestComponent } from '../TestComponent';
import { applyMarkdown, formatTestName } from '../utils/stringFormatUtils';
import type {
LayoutAnimationStartFunction,
LayoutAnimationType,
SharedTransitionAnimationsValues,
LayoutAnimation,
} from 'react-native-reanimated';
import { Matchers } from '../matchers/Matchers';
import { assertMockedAnimationTimestamp, assertTestCase, assertTestSuite } from './Asserts';
import { makeMutable, runOnJS } from 'react-native-reanimated';
import { RenderLock, SyncUIRunner } from '../utils/SyncUIRunner';
import { ValueRegistry } from './ValueRegistry';
import { TestSummaryLogger } from './TestSummaryLogger';
import { WindowDimensionsMocker } from './WindowDimensionsMocker';
import { AnimationUpdatesRecorder } from './AnimationUpdatesRecorder';
export { Presets } from '../Presets';

Expand All @@ -42,9 +37,14 @@ export class TestRunner {
private _syncUIRunner: SyncUIRunner = new SyncUIRunner();
private _renderLock: RenderLock = new RenderLock();
private _testSummary: TestSummaryLogger = new TestSummaryLogger();
private _windowDimensionsMocker: WindowDimensionsMocker = new WindowDimensionsMocker();
private _animationRecorder = new AnimationUpdatesRecorder();
private _valueRegistry = new ValueRegistry();

public getWindowDimensionsMocker() {
return this._windowDimensionsMocker;
}

public getAnimationUpdatesRecorder() {
return this._animationRecorder;
}
Expand Down Expand Up @@ -324,46 +324,6 @@ export class TestRunner {
this._currentTestSuite.afterEach = job;
}

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

public async mockWindowDimensions() {
await this._syncUIRunner.runOnUIBlocking(() => {
'worklet';
const originalLayoutAnimationsManager = global.LayoutAnimationsManager;

const startLayoutAnimation: LayoutAnimationStartFunction = (
tag: number,
type: LayoutAnimationType,
_yogaValues: Partial<SharedTransitionAnimationsValues>,
config: (arg: Partial<SharedTransitionAnimationsValues>) => LayoutAnimation,
) => {
originalLayoutAnimationsManager.start(
tag,
type,
{
..._yogaValues,
windowHeight: 852,
windowWidth: 393,
},
config,
);
};

global.originalLayoutAnimationsManager = originalLayoutAnimationsManager;
global.LayoutAnimationsManager = {
start: startLayoutAnimation,
stop: originalLayoutAnimationsManager.stop,
};
});
}

public wait(delay: number) {
return new Promise(resolve => {
setTimeout(resolve, delay);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import { SyncUIRunner } from '../utils/SyncUIRunner';
import type {
LayoutAnimationStartFunction,
LayoutAnimationType,
SharedTransitionAnimationsValues,
LayoutAnimation,
} from 'react-native-reanimated';

export class WindowDimensionsMocker {
public async unmockWindowDimensions() {
await new SyncUIRunner().runOnUIBlocking(() => {
'worklet';
if (global.originalLayoutAnimationsManager) {
global.LayoutAnimationsManager = global.originalLayoutAnimationsManager;
}
});
}

public async mockWindowDimensions() {
await new SyncUIRunner().runOnUIBlocking(() => {
'worklet';
const originalLayoutAnimationsManager = global.LayoutAnimationsManager;

const startLayoutAnimation: LayoutAnimationStartFunction = (
tag: number,
type: LayoutAnimationType,
_yogaValues: Partial<SharedTransitionAnimationsValues>,
config: (arg: Partial<SharedTransitionAnimationsValues>) => LayoutAnimation,
) => {
originalLayoutAnimationsManager.start(
tag,
type,
{
..._yogaValues,
windowHeight: 852,
windowWidth: 393,
},
config,
);
};

global.originalLayoutAnimationsManager = originalLayoutAnimationsManager;
global.LayoutAnimationsManager = {
start: startLayoutAnimation,
stop: originalLayoutAnimationsManager.stop,
};
});
}
}

0 comments on commit 0616843

Please sign in to comment.