UNPKG

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