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

Add expectNullable and expectNotNullable to runtime tests #5935

Merged
merged 2 commits into from
Apr 24, 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
@@ -1,6 +1,6 @@
import { getComparator } from './Comparators';
import { appendWhiteSpaceToMatchLength, color } from './stringFormatUtils';
import { ComparisonMode, OperationUpdate, TestCase, TestValue, TrackerCallCount } from './types';
import { ComparisonMode, OperationUpdate, TestCase, TestValue, NullableTestValue, TrackerCallCount } from './types';

type MatcherFunction = (
currentValue: TestValue,
Expand Down Expand Up @@ -210,3 +210,15 @@ export class Matchers {
return `Expected ${expected} snapshots, but received ${received} snapshots\n`;
}
}

export function nullableMatch(currentValue: NullableTestValue, testCase: TestCase, negation: boolean = false) {
const pass = currentValue === null || currentValue === undefined;

const coloredExpected = color('nullable', 'green');
const coloredReceived = color(currentValue, 'red');
const message = `Expected${negation ? ' NOT' : ''} ${coloredExpected} received ${coloredReceived}`;

if ((!pass && !negation) || (pass && negation)) {
testCase.errors.push(message);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import { TestRunner } from './TestRunner';
import { TestComponent } from './TestComponent';
import type { SharedValue } from 'react-native-reanimated';
import { TestConfiguration, TestValue } from './types';
import { TestConfiguration, TestValue, NullableTestValue } from './types';

export { Presets } from './Presets';

Expand Down Expand Up @@ -100,6 +100,14 @@
return testRunner.expect(value);
}

export function expectNullable(currentValue: NullableTestValue) {
return testRunner.expectNullable(currentValue);
}

export function expectNotNullable(currentValue: NullableTestValue) {
return testRunner.expectNotNullable(currentValue);
}

export function configure(config: TestConfiguration) {
return testRunner.configure(config);
}
Expand All @@ -108,8 +116,8 @@
await testRunner.mockAnimationTimer();
}

export async function unmockAnimationTimer() {

Check warning on line 119 in app/src/examples/RuntimeTests/ReanimatedRuntimeTestsRunner/RuntimeTestsApi.ts

View workflow job for this annotation

GitHub Actions / spellcheck

Unknown word (unmock)
await testRunner.unmockAnimationTimer();

Check warning on line 120 in app/src/examples/RuntimeTests/ReanimatedRuntimeTestsRunner/RuntimeTestsApi.ts

View workflow job for this annotation

GitHub Actions / spellcheck

Unknown word (unmock)
}

export async function setAnimationTimestamp(timestamp: number) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Component, MutableRefObject, ReactElement, useRef } from 'react';
import type {
NullableTestValue,
LockObject,
Operation,
SharedValueSnapshot,
Expand All @@ -11,11 +12,11 @@
TrackerCallCount,
} from './types';
import { TestComponent } from './TestComponent';
import { render, stopRecordingAnimationUpdates, unmockAnimationTimer } from './RuntimeTestsApi';

Check warning on line 15 in app/src/examples/RuntimeTests/ReanimatedRuntimeTestsRunner/TestRunner.ts

View workflow job for this annotation

GitHub Actions / spellcheck

Unknown word (unmock)
import { makeMutable, runOnUI, runOnJS, SharedValue } from 'react-native-reanimated';
import { color, formatString, indentNestingLevel } from './stringFormatUtils';
import { createUpdatesContainer } from './UpdatesContainer';
import { Matchers } from './Matchers';
import { Matchers, nullableMatch } from './Matchers';
import { assertMockedAnimationTimestamp, assertTestCase, assertTestSuite } from './Asserts';

let callTrackerRegistryJS: Record<string, number> = {};
Expand Down Expand Up @@ -232,7 +233,7 @@

this._currentTestCase = null;
await render(null);
await unmockAnimationTimer();

Check warning on line 236 in app/src/examples/RuntimeTests/ReanimatedRuntimeTestsRunner/TestRunner.ts

View workflow job for this annotation

GitHub Actions / spellcheck

Unknown word (unmock)
await stopRecordingAnimationUpdates();
}

Expand All @@ -258,6 +259,16 @@
return new Matchers(currentValue, this._currentTestCase);
}

public expectNullable(currentValue: NullableTestValue) {
assertTestCase(this._currentTestCase);
nullableMatch(currentValue, this._currentTestCase);
}

public expectNotNullable(currentValue: NullableTestValue) {
assertTestCase(this._currentTestCase);
nullableMatch(currentValue, this._currentTestCase, true);
}

public beforeAll(job: () => void) {
assertTestSuite(this._currentTestSuite);
this._currentTestSuite.beforeAll = job;
Expand Down Expand Up @@ -402,7 +413,7 @@
});
}

public async unmockAnimationTimer() {

Check warning on line 416 in app/src/examples/RuntimeTests/ReanimatedRuntimeTestsRunner/TestRunner.ts

View workflow job for this annotation

GitHub Actions / spellcheck

Unknown word (unmock)
await this.runOnUIBlocking(() => {
'worklet';
if (global.originalGetAnimationTimestamp) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { TestValue } from './types';
import { NullableTestValue } from './types';

export const RUNTIME_TEST_ERRORS = {
UNDEFINED_TEST_SUITE: 'Undefined test suite context',
Expand All @@ -16,7 +16,7 @@ export function appendWhiteSpaceToMatchLength(message: string | number, length:
return `${messageStr}${' '.repeat(length - messageLen)}`;
}

export function color(value: TestValue, color: 'yellow' | 'cyan' | 'green' | 'red' | 'gray') {
export function color(value: NullableTestValue, color: 'yellow' | 'cyan' | 'green' | 'red' | 'gray') {
const COLOR_CODES = {
red: '\x1b[31m',
green: '\x1b[32m',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ export interface Operation {
}

export type TestValue = TrackerCallCount | string | Array<unknown> | number | bigint | Record<string, unknown>;
export type NullableTestValue = TestValue | null | undefined;

export type TestConfiguration = {
render: Dispatch<SetStateAction<ReactNode | null>>;
Expand Down
Loading