From ba8d184b7753abd7937fbe0db28b7a39f09a4b73 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rub=C3=A9n=20Norte?= Date: Fri, 6 Dec 2024 05:24:31 -0800 Subject: [PATCH] Move render output tests to ReactNativeTester (#48140) Summary: Pull Request resolved: https://github.com/facebook/react-native/pull/48140 Changelog: [internal] These tests test an API that's part of `ReactNativeTester` (will be renamed as `Fantom`) so it makes sense that they're in the same test file as the tests for the rest of the API. Reviewed By: javache Differential Revision: D66874226 fbshipit-source-id: f17e14c83cb5ca95ac619c5398c49ad84a27cfa5 --- .../src/__tests__/ReactNativeTester-itest.js | 211 +++++++++++++++++- 1 file changed, 205 insertions(+), 6 deletions(-) diff --git a/packages/react-native-fantom/src/__tests__/ReactNativeTester-itest.js b/packages/react-native-fantom/src/__tests__/ReactNativeTester-itest.js index adaa90105ae..b9a45a5d978 100644 --- a/packages/react-native-fantom/src/__tests__/ReactNativeTester-itest.js +++ b/packages/react-native-fantom/src/__tests__/ReactNativeTester-itest.js @@ -10,14 +10,16 @@ */ import 'react-native/Libraries/Core/InitializeCore'; -import * as ReactNativeTester from '..'; +import {createRoot, runTask} from '..'; +import * as React from 'react'; +import {Text, View} from 'react-native'; describe('ReactNativeTester', () => { describe('runTask', () => { it('should run a task synchronously', () => { const task = jest.fn(); - ReactNativeTester.runTask(task); + runTask(task); expect(task).toHaveBeenCalledTimes(1); }); @@ -25,7 +27,7 @@ describe('ReactNativeTester', () => { // TODO: fix error handling and make this pass it.skip('should re-throw errors from the task synchronously', () => { expect(() => { - ReactNativeTester.runTask(() => { + runTask(() => { throw new Error('test error'); }); }).toThrow('test error'); @@ -34,7 +36,7 @@ describe('ReactNativeTester', () => { it('should exhaust the microtask queue synchronously', () => { const lastMicrotask = jest.fn(); - ReactNativeTester.runTask(() => { + runTask(() => { queueMicrotask(() => { queueMicrotask(() => { queueMicrotask(() => { @@ -50,7 +52,7 @@ describe('ReactNativeTester', () => { // TODO: fix error handling and make this pass it.skip('should re-throw errors from microtasks synchronously', () => { expect(() => { - ReactNativeTester.runTask(() => { + runTask(() => { queueMicrotask(() => { throw new Error('test error'); }); @@ -61,7 +63,7 @@ describe('ReactNativeTester', () => { it('should run async tasks synchronously', () => { let completed = false; - ReactNativeTester.runTask(async () => { + runTask(async () => { await Promise.resolve(6); completed = true; }); @@ -69,4 +71,201 @@ describe('ReactNativeTester', () => { expect(completed).toBe(true); }); }); + + describe('getRenderedOutput', () => { + describe('toJSX', () => { + it('default config', () => { + const root = createRoot(); + + runTask(() => { + root.render( + , + ); + }); + + expect(root.getRenderedOutput().toJSX()).toEqual( + , + ); + + root.destroy(); + }); + + it('default config, list of children', () => { + const root = createRoot(); + + runTask(() => { + root.render( + <> + + + , + ); + }); + + expect(root.getRenderedOutput().toJSX()).toEqual( + <> + + + , + ); + + root.destroy(); + }); + + it('include root', () => { + const root = createRoot(); + + runTask(() => { + root.render( + , + ); + }); + + expect(root.getRenderedOutput({includeRoot: true}).toJSX()).toEqual( + + + , + ); + + root.destroy(); + }); + + it('include layout metrics', () => { + const root = createRoot(); + + runTask(() => { + root.render( + , + ); + }); + + expect( + root.getRenderedOutput({includeLayoutMetrics: true}).toJSX(), + ).toEqual( + , + ); + + root.destroy(); + }); + + it('take props', () => { + const root = createRoot(); + + runTask(() => { + root.render( + , + ); + }); + + expect( + root + .getRenderedOutput({ + props: ['width'], + }) + .toJSX(), + ).toEqual(); + + root.destroy(); + }); + + it('skip props', () => { + const root = createRoot(); + + runTask(() => { + root.render( + , + ); + }); + + expect( + root + .getRenderedOutput({ + props: ['!width'], + }) + .toJSX(), + ).toEqual(); + + root.destroy(); + }); + + it('filter out all props', () => { + const root = createRoot(); + + runTask(() => { + root.render( + <> + + hello world! + + , + ); + }); + + expect(root.getRenderedOutput({props: []}).toJSX()).toEqual( + <> + + hello world! + + , + ); + + root.destroy(); + }); + }); + + describe('toJSON', () => { + it('nested text', () => { + const root = createRoot(); + + runTask(() => { + root.render( + + Testing native{' '} + + JSX is easy! + + , + ); + }); + + expect( + root.getRenderedOutput({props: ['foreground*']}).toJSON(), + ).toEqual({ + children: [ + 'Testing native ', + { + children: 'JSX is ', + props: { + foregroundColor: 'rgba(255, 0, 0, 255)', + }, + type: 'Text', + }, + { + children: 'easy!', + props: { + foregroundColor: 'rgba(0, 0, 255, 255)', + }, + type: 'Text', + }, + ], + props: { + foregroundColor: 'rgba(255, 255, 255, 127)', + }, + type: 'Paragraph', + }); + + root.destroy(); + }); + }); + }); });