Skip to content

Commit

Permalink
improved inferno-test-utils typings
Browse files Browse the repository at this point in the history
  • Loading branch information
Havunen committed Mar 16, 2024
1 parent 13f8d24 commit c4930a7
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 7 deletions.
4 changes: 2 additions & 2 deletions packages/inferno-test-utils/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ export function isFunctionalVNodeOfType(obj: VNode, type: Function): boolean {

export function isClassVNodeOfType(
obj: VNode,
type: Component<any, any>,
type: Inferno.ComponentClass<unknown> | Inferno.StatelessComponent<unknown>,
): boolean {
return _isClassVNode(obj) && obj.type === type;
}
Expand Down Expand Up @@ -85,7 +85,7 @@ export function isRenderedClassComponent(obj: any): boolean {

export function isRenderedClassComponentOfType(
obj: any,
type: Component<any, any>,
type: Inferno.ComponentClass<unknown> | Inferno.StatelessComponent<unknown>,
): boolean {
return (
isRenderedClassComponent(obj) &&
Expand Down
19 changes: 14 additions & 5 deletions packages/inferno-test-utils/src/jest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, any>;
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,
});
Expand All @@ -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;
Expand Down Expand Up @@ -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) + '';
Expand All @@ -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);
Expand Down

0 comments on commit c4930a7

Please sign in to comment.