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

Extract valueRegistry from TestRunner #6317

Merged
merged 3 commits into from
Jul 30, 2024
Merged
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 @@ -8,6 +8,7 @@ import { DescribeDecorator, TestDecorator } from './types';
export { Presets } from './Presets';

const testRunner = new TestRunner();
const valueRegistry = testRunner.getValueRegistry();

type DescribeFunction = (name: string, buildSuite: BuildFunction) => void;
type TestFunction = (name: string, buildTest: BuildFunction) => void;
Expand Down Expand Up @@ -126,11 +127,11 @@ export function getTrackerCallCount(name: string) {
}

export function registerValue(name: string, value: SharedValue) {
return testRunner.registerValue(name, value);
return valueRegistry.registerValue(name, value);
}

export async function getRegisteredValue(name: string) {
return await testRunner.getRegisteredValue(name);
return await valueRegistry.getRegisteredValue(name);
}

export function getTestComponent(name: string): TestComponent {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import type {
BuildFunction,
NullableTestValue,
Operation,
SharedValueSnapshot,
TestCase,
TestConfiguration,
TestSuite,
Expand All @@ -15,7 +14,6 @@ import { DescribeDecorator, TestDecorator } from '../types';
import { TestComponent } from '../TestComponent';
import { applyMarkdown, formatString } from '../utils/stringFormatUtils';
import type {
SharedValue,
LayoutAnimationStartFunction,
LayoutAnimationType,
SharedTransitionAnimationsValues,
Expand All @@ -26,6 +24,7 @@ import { assertMockedAnimationTimestamp, assertTestCase, assertTestSuite } from
import { createUpdatesContainer } from './UpdatesContainer';
import { makeMutable, runOnJS } from 'react-native-reanimated';
import { RenderLock, SyncUIRunner } from '../utils/SyncUIRunner';
import { ValueRegistry } from './ValueRegistry';
import { TestSummaryLogger } from './TestSummaryLogger';
export { Presets } from '../Presets';

Expand All @@ -48,11 +47,15 @@ export class TestRunner {
private _currentTestSuite: TestSuite | null = null;
private _currentTestCase: TestCase | null = null;
private _renderHook: (component: ReactElement<Component> | null) => void = () => {};
private _valueRegistry: Record<string, SharedValue> = {};
private _includesOnly: boolean = false;
private _syncUIRunner: SyncUIRunner = new SyncUIRunner();
private _renderLock: RenderLock = new RenderLock();
private _testSummary: TestSummaryLogger = new TestSummaryLogger();
private _valueRegistry = new ValueRegistry();
private _testSummary = new TestSummaryLogger();

public getValueRegistry() {
return this._valueRegistry;
}

public notify(name: string) {
'worklet';
Expand Down Expand Up @@ -196,27 +199,6 @@ export class TestRunner {
}
}

public registerValue(name: string, value: SharedValue) {
'worklet';
this._valueRegistry[name] = value;
}

public async getRegisteredValue(name: string): Promise<SharedValueSnapshot> {
const jsValue = this._valueRegistry[name].value;
const sharedValue = this._valueRegistry[name];
const valueContainer = makeMutable<unknown>(null);
await this._syncUIRunner.runOnUIBlocking(() => {
'worklet';
valueContainer.value = sharedValue.value;
}, 1000);
const uiValue = valueContainer.value;
return {
name,
onJS: jsValue as TestValue,
onUI: uiValue as TestValue,
};
}

public getTrackerCallCount(name: string): TrackerCallCount {
return {
name,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { makeMutable } from 'react-native-reanimated';
import type { SharedValue } from 'react-native-reanimated';
import type { SharedValueSnapshot, TestValue } from '../types';
import { SyncUIRunner } from '../utils/SyncUIRunner';

export class ValueRegistry {
private _valueRegistry: Record<string, SharedValue> = {};
private _syncUIRunner = new SyncUIRunner();

public registerValue(name: string, value: SharedValue) {
'worklet';
this._valueRegistry[name] = value;
}

public async getRegisteredValue(name: string): Promise<SharedValueSnapshot> {
const jsValue = this._valueRegistry[name].value;
const sharedValue = this._valueRegistry[name];
const valueContainer = makeMutable<unknown>(null);
await this._syncUIRunner.runOnUIBlocking(() => {
'worklet';
valueContainer.value = sharedValue.value;
}, 1000);
const uiValue = valueContainer.value;
return {
name,
onJS: jsValue as TestValue,
onUI: uiValue as TestValue,
};
}
}