UNPKG

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