From c4930a717161a0314809c522dea04eff16d40644 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sampo=20Kivist=C3=B6?= Date: Sat, 16 Mar 2024 12:02:55 +0200 Subject: [PATCH] improved inferno-test-utils typings --- packages/inferno-test-utils/src/index.ts | 4 ++-- packages/inferno-test-utils/src/jest.ts | 19 ++++++++++++++----- 2 files changed, 16 insertions(+), 7 deletions(-) diff --git a/packages/inferno-test-utils/src/index.ts b/packages/inferno-test-utils/src/index.ts index 86e5d7a94..47e1d8780 100644 --- a/packages/inferno-test-utils/src/index.ts +++ b/packages/inferno-test-utils/src/index.ts @@ -46,7 +46,7 @@ export function isFunctionalVNodeOfType(obj: VNode, type: Function): boolean { export function isClassVNodeOfType( obj: VNode, - type: Component, + type: Inferno.ComponentClass | Inferno.StatelessComponent, ): boolean { return _isClassVNode(obj) && obj.type === type; } @@ -85,7 +85,7 @@ export function isRenderedClassComponent(obj: any): boolean { export function isRenderedClassComponentOfType( obj: any, - type: Component, + type: Inferno.ComponentClass | Inferno.StatelessComponent, ): boolean { return ( isRenderedClassComponent(obj) && diff --git a/packages/inferno-test-utils/src/jest.ts b/packages/inferno-test-utils/src/jest.ts index 308ec539e..6b3b43f99 100644 --- a/packages/inferno-test-utils/src/jest.ts +++ b/packages/inferno-test-utils/src/jest.ts @@ -8,12 +8,21 @@ import { getTagNameOfVNode } from './utils'; // Symbols and algorithm have been reversed from the following file: // https://github.com/facebook/react/blob/v15.4.2/src/renderers/testing/ReactTestRenderer.js#L98 +type InfernoTestRendererNode = InfernoSnapshot | string; + +export interface InfernoSnapshot { + type: string; + props: Record; + children: null | InfernoTestRendererNode[]; + $$typeof?: symbol | string; +} + const symbolValue = typeof Symbol === 'undefined' ? 'react.test.json' : Symbol.for('react.test.json'); -function createSnapshotObject(object: object): object { +function createSnapshotObject(object: InfernoSnapshot): InfernoSnapshot { Object.defineProperty(object, '$$typeof', { value: symbolValue, }); @@ -35,7 +44,7 @@ function removeChildren(item): void { } } -function buildVNodeSnapshot(vNode: VNode): unknown { +function buildVNodeSnapshot(vNode: VNode): InfernoSnapshot { const flags = vNode.flags; const children: any = vNode.children; let childVNode; @@ -87,7 +96,7 @@ function buildVNodeSnapshot(vNode: VNode): unknown { return createSnapshotObject({ children: childVNode, props: snapShotProps, - type: getTagNameOfVNode(vNode), + type: getTagNameOfVNode(vNode) as string, }); } else if (flags & VNodeFlags.Text) { childVNode = (vNode.children as string | number) + ''; @@ -96,11 +105,11 @@ function buildVNodeSnapshot(vNode: VNode): unknown { return childVNode; } -export function vNodeToSnapshot(vNode: VNode): unknown { +export function vNodeToSnapshot(vNode: VNode): InfernoSnapshot { return buildVNodeSnapshot(vNode); } -export function renderToSnapshot(input: VNode): unknown { +export function renderToSnapshot(input: VNode): InfernoSnapshot { render(input, document.createElement('div')); rerender(); // Flush all pending set state calls const snapshot = vNodeToSnapshot(input);