From f3c0cf9a05a99a47ecbf69396fb6acb880f49425 Mon Sep 17 00:00:00 2001 From: * <*> Date: Thu, 19 Jan 2023 16:56:53 +0800 Subject: [PATCH] Match-id-470122e11f1af7f936f3b0991fd467d8fd693eda --- CHANGELOG.md | 4 ++ libs/horizon/global.d.ts | 1 + libs/horizon/index.ts | 14 +---- libs/horizon/package.json | 2 +- libs/horizon/src/external/JSXElement.ts | 16 +++++- libs/horizon/src/external/TestUtil.ts | 69 +++++++++++++++++++++++ libs/horizon/src/renderer/ErrorHandler.ts | 2 +- 7 files changed, 91 insertions(+), 17 deletions(-) create mode 100644 libs/horizon/src/external/TestUtil.ts diff --git a/CHANGELOG.md b/CHANGELOG.md index d71ca87c..2eb75f8d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,7 @@ +## 0.0.34 (2023-01-11) +- **core**: #95 新增jsx接口 +- **core**: #96 #97 fix testing-library 的UT错误 + ## 0.0.33 (2023-01-11) - **horizonX-devtool**: 修复IE中报错 diff --git a/libs/horizon/global.d.ts b/libs/horizon/global.d.ts index 34c32508..9da3e079 100644 --- a/libs/horizon/global.d.ts +++ b/libs/horizon/global.d.ts @@ -19,3 +19,4 @@ declare var isDev: boolean; declare var isTest: boolean; declare const __VERSION__: string; +declare var setImmediate: Function; diff --git a/libs/horizon/index.ts b/libs/horizon/index.ts index 737861a7..90d62f6e 100644 --- a/libs/horizon/index.ts +++ b/libs/horizon/index.ts @@ -42,9 +42,6 @@ import { useState, useDebugValue, } from './src/renderer/hooks/HookExternal'; -import { asyncUpdates } from './src/renderer/TreeBuilder'; -import { callRenderQueueImmediate } from './src/renderer/taskExecutor/RenderQueue'; -import { runAsyncEffects } from './src/renderer/submit/HookEffectHandler'; import { isContextProvider, isContextConsumer, @@ -59,16 +56,7 @@ import { import { createStore, useStore, clearStore } from './src/horizonx/store/StoreHandler'; import * as reduxAdapter from './src/horizonx/adapters/redux'; import { watch } from './src/horizonx/proxy/watch'; - -// act用于测试,作用是:如果fun触发了刷新(包含了异步刷新),可以保证在act后面的代码是在刷新完成后才执行。 -const act = fun => { - asyncUpdates(fun); - callRenderQueueImmediate(); - runAsyncEffects(); - - // 兼容返回Promise - return Promise.resolve(); -}; +import { act } from './src/external/TestUtil'; import { render, diff --git a/libs/horizon/package.json b/libs/horizon/package.json index 8b7332a0..aa78f321 100644 --- a/libs/horizon/package.json +++ b/libs/horizon/package.json @@ -4,7 +4,7 @@ "keywords": [ "horizon" ], - "version": "0.0.33", + "version": "0.0.34", "homepage": "", "bugs": "", "main": "index.js", diff --git a/libs/horizon/src/external/JSXElement.ts b/libs/horizon/src/external/JSXElement.ts index a4cf50b7..614733be 100644 --- a/libs/horizon/src/external/JSXElement.ts +++ b/libs/horizon/src/external/JSXElement.ts @@ -25,10 +25,10 @@ import { Source } from '../renderer/Types'; * props 其他常规属性 */ export function JSXElement(type, key, ref, vNode, props, source: Source | null) { - return { + const ele = { // 元素标识符 vtype: TYPE_COMMON_ELEMENT, - src: isDev ? source : null, + src: null, // 属于元素的内置属性 type: type, @@ -39,6 +39,18 @@ export function JSXElement(type, key, ref, vNode, props, source: Source | null) // 所属的class组件 belongClassVNode: vNode, }; + + if (isDev) { + // 为了test判断两个JSXElement对象是否相等时忽略src属性,需要设置src的enumerable为false + Object.defineProperty(ele, 'src', { + configurable: false, + enumerable: false, + writable: false, + value: source, + }); + } + + return ele; } function isValidKey(key) { diff --git a/libs/horizon/src/external/TestUtil.ts b/libs/horizon/src/external/TestUtil.ts new file mode 100644 index 00000000..6eccbbe5 --- /dev/null +++ b/libs/horizon/src/external/TestUtil.ts @@ -0,0 +1,69 @@ +/* + * Copyright (c) 2020 Huawei Technologies Co.,Ltd. + * + * openGauss is licensed under Mulan PSL v2. + * You can use this software according to the terms and conditions of the Mulan PSL v2. + * You may obtain a copy of Mulan PSL v2 at: + * + * http://license.coscl.org.cn/MulanPSL2 + * + * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, + * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, + * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. + * See the Mulan PSL v2 for more details. + */ + + +import {asyncUpdates} from '../renderer/TreeBuilder'; +import {callRenderQueueImmediate} from '../renderer/taskExecutor/RenderQueue'; +import {runAsyncEffects} from '../renderer/submit/HookEffectHandler'; +import {isPromise} from '../renderer/ErrorHandler'; + +// act用于测试,作用是:如果fun触发了刷新(包含了异步刷新),可以保证在act后面的代码是在刷新完成后才执行。 +function act(fun) { + const funRet = asyncUpdates(fun); + + callRenderQueueImmediate(); + runAsyncEffects(); + // effects可能产生刷新任务,这里再执行一次 + callRenderQueueImmediate(); + + // 如果fun返回的是Promise + if (isPromise(funRet)) { + // testing-library会返回Promise + return { + then(resolve, reject) { + funRet.then( + () => { + if (typeof setImmediate === 'function') { + // 通过setImmediate回调,用于等待业务的setTimeout完成 + setImmediate(() => { + callRenderQueueImmediate(); + runAsyncEffects(); + resolve(); + }); + } else { + callRenderQueueImmediate(); + runAsyncEffects(); + resolve(); + } + }, + err => { + reject(err); + }, + ); + }, + }; + } else { + return { + then(resolve) { + resolve(); + }, + }; + } + +} + +export { + act +} diff --git a/libs/horizon/src/renderer/ErrorHandler.ts b/libs/horizon/src/renderer/ErrorHandler.ts index af115325..7ba23cec 100644 --- a/libs/horizon/src/renderer/ErrorHandler.ts +++ b/libs/horizon/src/renderer/ErrorHandler.ts @@ -72,7 +72,7 @@ function createClassErrorUpdate(vNode: VNode, error: any): Update { } return update; } -function isPromise(error: any): error is PromiseType { +export function isPromise(error: any): error is PromiseType { return error !== null && typeof error === 'object' && typeof error.then === 'function'; } // 处理capture和bubble阶段抛出的错误