UNPKG

3.71 kBTypeScriptView Raw
1import { ElementType, ReactElement } from "react";
2export {};
3
4// extracted from:
5// - https://github.com/facebook/react/blob/v18.0.0/packages/react-test-renderer/index.js
6// - https://reactjs.org/docs/test-renderer.html
7
8export interface ReactTestRendererJSON {
9 type: string;
10 props: { [propName: string]: any };
11 children: null | ReactTestRendererNode[];
12}
13export type ReactTestRendererNode = ReactTestRendererJSON | string;
14export interface ReactTestRendererTree extends ReactTestRendererJSON {
15 nodeType: "component" | "host";
16 instance: any;
17 rendered: null | ReactTestRendererTree | ReactTestRendererTree[];
18}
19export interface ReactTestInstance {
20 instance: any;
21 type: ElementType;
22 props: { [propName: string]: any };
23 parent: null | ReactTestInstance;
24 children: Array<ReactTestInstance | string>;
25
26 find(predicate: (node: ReactTestInstance) => boolean): ReactTestInstance;
27 findByType(type: ElementType): ReactTestInstance;
28 findByProps(props: { [propName: string]: any }): ReactTestInstance;
29
30 findAll(predicate: (node: ReactTestInstance) => boolean, options?: { deep: boolean }): ReactTestInstance[];
31 findAllByType(type: ElementType, options?: { deep: boolean }): ReactTestInstance[];
32 findAllByProps(props: { [propName: string]: any }, options?: { deep: boolean }): ReactTestInstance[];
33}
34export interface ReactTestRenderer {
35 toJSON(): null | ReactTestRendererJSON | ReactTestRendererJSON[];
36 toTree(): null | ReactTestRendererTree;
37 unmount(nextElement?: ReactElement): void;
38 update(nextElement: ReactElement): void;
39 getInstance(): null | ReactTestInstance;
40 root: ReactTestInstance;
41}
42export interface TestRendererOptions {
43 createNodeMock(element: ReactElement): any;
44}
45export function create(nextElement: ReactElement, options?: TestRendererOptions): ReactTestRenderer;
46
47// VoidOrUndefinedOnly is here to forbid any sneaky "Promise" returns.
48// the actual return value is always a "DebugPromiseLike".
49declare const UNDEFINED_VOID_ONLY: unique symbol;
50// eslint-disable-next-line @typescript-eslint/no-invalid-void-type
51type VoidOrUndefinedOnly = void | { [UNDEFINED_VOID_ONLY]: never };
52/**
53 * Wrap any code rendering and triggering updates to your components into `act()` calls.
54 *
55 * Ensures that the behavior in your tests matches what happens in the browser
56 * more closely by executing pending `useEffect`s before returning. This also
57 * reduces the amount of re-renders done.
58 *
59 * @param callback An asynchronous, void callback that will execute as a single, complete React commit.
60 *
61 * @see https://reactjs.org/blog/2019/02/06/react-v16.8.0.html#testing-hooks
62 */
63// VoidOrUndefinedOnly is here to forbid any sneaky return values
64export function act(callback: () => Promise<VoidOrUndefinedOnly>): Promise<undefined>;
65/**
66 * Wrap any code rendering and triggering updates to your components into `act()` calls.
67 *
68 * Ensures that the behavior in your tests matches what happens in the browser
69 * more closely by executing pending `useEffect`s before returning. This also
70 * reduces the amount of re-renders done.
71 *
72 * @param callback A synchronous, void callback that will execute as a single, complete React commit.
73 *
74 * @see https://reactjs.org/blog/2019/02/06/react-v16.8.0.html#testing-hooks
75 */
76export function act(callback: () => VoidOrUndefinedOnly): DebugPromiseLike;
77
78// Intentionally doesn't extend PromiseLike<never>.
79// Ideally this should be as hard to accidentally use as possible.
80export interface DebugPromiseLike {
81 // the actual then() in here is 1-ary, but that doesn't count as a PromiseLike.
82 then(onfulfilled: (value: never) => never, onrejected: (reason: never) => never): never;
83}