diff --git a/private/react-native-fantom/src/Benchmark.js b/private/react-native-fantom/src/Benchmark.js index fe352f7fa65..ae7c5fb78e3 100644 --- a/private/react-native-fantom/src/Benchmark.js +++ b/private/react-native-fantom/src/Benchmark.js @@ -35,9 +35,32 @@ type TestOptions = $ReadOnly<{ type SuiteResults = Array<$ReadOnly>; +type TestWithArgName = string | ((testArg: TestArgType) => string); + +type TestWithArgOptions = + | FnOptions + | ((testArg: TestArgType) => FnOptions); + +interface ParameterizedTestFunction { + ( + testArgs: $ReadOnlyArray, + name: TestWithArgName, + fn: (testArg: TestArgType) => void, + options?: TestWithArgOptions, + ): SuiteAPI; + only: ( + testArgs: $ReadOnlyArray, + name: TestWithArgName, + fn: (testArg: TestArgType) => void, + options?: TestWithArgOptions, + ) => SuiteAPI; +} + interface TestFunction { (name: string, fn: () => void, options?: FnOptions): SuiteAPI; only: (name: string, fn: () => void, options?: FnOptions) => SuiteAPI; + // `each` allows to run the same test multiple times with different arguments, provided as an array of values: + each: ParameterizedTestFunction; } interface SuiteAPI { @@ -45,15 +68,17 @@ interface SuiteAPI { verify(fn: (results: SuiteResults) => void): SuiteAPI; } +interface TestTask { + name: string; + fn: () => void; + options: TestOptions | void; +} + export function suite( suiteName: string, suiteOptions?: SuiteOptions = {}, ): SuiteAPI { - const tasks: Array<{ - name: string, - fn: () => void, - options: TestOptions | void, - }> = []; + const tasks: Array = []; const verifyFns = []; global.it(suiteName, () => { @@ -165,6 +190,45 @@ export function suite( return suiteAPI; }; + const testWithArg = ( + testArg: TestArgType, + name: TestWithArgName, + fn: (testArg: TestArgType) => void, + options?: TestWithArgOptions, + only?: boolean = false, + ): TestTask => { + const taskName = + typeof name === 'function' + ? name(testArg) + : `${name} [arg=${String(testArg)}]`; + const taskOptions = + typeof options === 'function' ? options(testArg) : options; + const taskFn = () => fn(testArg); + return {name: taskName, fn: taskFn, options: {...taskOptions, only}}; + }; + + // $FlowIssue[incompatible-type] + const testEach: ParameterizedTestFunction = ( + testArgs: $ReadOnlyArray, + name: TestWithArgName, + fn: (testArg: TestArgType) => void, + options?: TestWithArgOptions, + ): SuiteAPI => { + for (const testArg of testArgs) { + tasks.push(testWithArg(testArg, name, fn, options)); + } + return suiteAPI; + }; + + testEach.only = (testArgs, name, fn, options) => { + for (const testArg of testArgs) { + tasks.push(testWithArg(testArg, name, fn, options, true)); + } + return suiteAPI; + }; + + test.each = testEach; + const suiteAPI = { test, verify(fn: (results: SuiteResults) => void): SuiteAPI {