UNPKG

12.7 kBTypeScriptView Raw
1import {
2 AbstractView, Component, ComponentClass,
3 ReactElement, ReactInstance, ClassType,
4 DOMElement, FunctionComponentElement, CElement,
5 ReactHTMLElement, DOMAttributes, FC
6} from 'react';
7
8import * as ReactTestUtils from ".";
9
10export {};
11
12export interface OptionalEventProperties {
13 bubbles?: boolean | undefined;
14 cancelable?: boolean | undefined;
15 currentTarget?: EventTarget | undefined;
16 defaultPrevented?: boolean | undefined;
17 eventPhase?: number | undefined;
18 isTrusted?: boolean | undefined;
19 nativeEvent?: Event | undefined;
20 preventDefault?(): void;
21 stopPropagation?(): void;
22 target?: EventTarget | undefined;
23 timeStamp?: Date | undefined;
24 type?: string | undefined;
25}
26
27export type ModifierKey = "Alt" | "AltGraph" | "CapsLock" | "Control" | "Fn" | "FnLock" | "Hyper" | "Meta" | "NumLock" | "ScrollLock" | "Shift" | "Super" | "Symbol" | "SymbolLock";
28
29export interface SyntheticEventData extends OptionalEventProperties {
30 altKey?: boolean | undefined;
31 button?: number | undefined;
32 buttons?: number | undefined;
33 clientX?: number | undefined;
34 clientY?: number | undefined;
35 changedTouches?: TouchList | undefined;
36 charCode?: number | undefined;
37 clipboardData?: DataTransfer | undefined;
38 ctrlKey?: boolean | undefined;
39 deltaMode?: number | undefined;
40 deltaX?: number | undefined;
41 deltaY?: number | undefined;
42 deltaZ?: number | undefined;
43 detail?: number | undefined;
44 getModifierState?(key: ModifierKey): boolean;
45 key?: string | undefined;
46 keyCode?: number | undefined;
47 locale?: string | undefined;
48 location?: number | undefined;
49 metaKey?: boolean | undefined;
50 pageX?: number | undefined;
51 pageY?: number | undefined;
52 relatedTarget?: EventTarget | undefined;
53 repeat?: boolean | undefined;
54 screenX?: number | undefined;
55 screenY?: number | undefined;
56 shiftKey?: boolean | undefined;
57 targetTouches?: TouchList | undefined;
58 touches?: TouchList | undefined;
59 view?: AbstractView | undefined;
60 which?: number | undefined;
61}
62
63export type EventSimulator = (element: Element | Component<any>, eventData?: SyntheticEventData) => void;
64
65export interface MockedComponentClass {
66 new (props: any): any;
67}
68
69export interface ShallowRenderer {
70 /**
71 * After `shallowRenderer.render()` has been called, returns shallowly rendered output.
72 */
73 getRenderOutput<E extends ReactElement>(): E;
74 /**
75 * Similar to `ReactDOM.render` but it doesn't require DOM and only renders a single level deep.
76 */
77 render(element: ReactElement, context?: any): void;
78 unmount(): void;
79}
80
81/**
82 * Simulate an event dispatch on a DOM node with optional `eventData` event data.
83 * `Simulate` has a method for every event that React understands.
84 */
85 export namespace Simulate {
86 const abort: EventSimulator;
87 const animationEnd: EventSimulator;
88 const animationIteration: EventSimulator;
89 const animationStart: EventSimulator;
90 const blur: EventSimulator;
91 const cancel: EventSimulator;
92 const canPlay: EventSimulator;
93 const canPlayThrough: EventSimulator;
94 const change: EventSimulator;
95 const click: EventSimulator;
96 const close: EventSimulator;
97 const compositionEnd: EventSimulator;
98 const compositionStart: EventSimulator;
99 const compositionUpdate: EventSimulator;
100 const contextMenu: EventSimulator;
101 const copy: EventSimulator;
102 const cut: EventSimulator;
103 const auxClick: EventSimulator;
104 const doubleClick: EventSimulator;
105 const drag: EventSimulator;
106 const dragEnd: EventSimulator;
107 const dragEnter: EventSimulator;
108 const dragExit: EventSimulator;
109 const dragLeave: EventSimulator;
110 const dragOver: EventSimulator;
111 const dragStart: EventSimulator;
112 const drop: EventSimulator;
113 const durationChange: EventSimulator;
114 const emptied: EventSimulator;
115 const encrypted: EventSimulator;
116 const ended: EventSimulator;
117 const error: EventSimulator;
118 const focus: EventSimulator;
119 const input: EventSimulator;
120 const invalid: EventSimulator;
121 const keyDown: EventSimulator;
122 const keyPress: EventSimulator;
123 const keyUp: EventSimulator;
124 const load: EventSimulator;
125 const loadStart: EventSimulator;
126 const loadedData: EventSimulator;
127 const loadedMetadata: EventSimulator;
128 const mouseDown: EventSimulator;
129 const mouseEnter: EventSimulator;
130 const mouseLeave: EventSimulator;
131 const mouseMove: EventSimulator;
132 const mouseOut: EventSimulator;
133 const mouseOver: EventSimulator;
134 const mouseUp: EventSimulator;
135 const paste: EventSimulator;
136 const pause: EventSimulator;
137 const play: EventSimulator;
138 const playing: EventSimulator;
139 const progress: EventSimulator;
140 const pointerCancel: EventSimulator;
141 const pointerDown: EventSimulator;
142 const pointerUp: EventSimulator;
143 const pointerMove: EventSimulator;
144 const pointerOut: EventSimulator;
145 const pointerOver: EventSimulator;
146 const pointerEnter: EventSimulator;
147 const pointerLeave: EventSimulator;
148 const gotPointerCapture: EventSimulator;
149 const lostPointerCapture: EventSimulator;
150 const rateChange: EventSimulator;
151 const reset: EventSimulator;
152 const resize: EventSimulator;
153 const scroll: EventSimulator;
154 const toggle: EventSimulator;
155 const seeked: EventSimulator;
156 const seeking: EventSimulator;
157 const select: EventSimulator;
158 const beforeInput: EventSimulator;
159 const stalled: EventSimulator;
160 const submit: EventSimulator;
161 const suspend: EventSimulator;
162 const timeUpdate: EventSimulator;
163 const touchCancel: EventSimulator;
164 const touchEnd: EventSimulator;
165 const touchMove: EventSimulator;
166 const touchStart: EventSimulator;
167 const transitionEnd: EventSimulator;
168 const volumeChange: EventSimulator;
169 const waiting: EventSimulator;
170 const wheel: EventSimulator;
171}
172
173/**
174 * Render a React element into a detached DOM node in the document. __This function requires a DOM__.
175 */
176export function renderIntoDocument<T extends Element>(
177 element: DOMElement<any, T>): T;
178export function renderIntoDocument(
179 element: FunctionComponentElement<any>): void;
180// If we replace `P` with `any` in this overload, then some tests fail because
181// calls to `renderIntoDocument` choose the last overload on the
182// subtype-relation pass and get an undesirably broad return type. Using `P`
183// allows this overload to match on the subtype-relation pass.
184export function renderIntoDocument<P, T extends Component<P>>(
185 element: CElement<P, T>): T;
186export function renderIntoDocument<P>(
187 element: ReactElement<P>): Component<P> | Element | void;
188
189/**
190 * Pass a mocked component module to this method to augment it with useful methods that allow it to
191 * be used as a dummy React component. Instead of rendering as usual, the component will become
192 * a simple `<div>` (or other tag if `mockTagName` is provided) containing any provided children.
193 */
194export function mockComponent(
195 mocked: MockedComponentClass, mockTagName?: string): typeof ReactTestUtils;
196
197/**
198 * Returns `true` if `element` is any React element.
199 */
200export function isElement(element: any): boolean;
201
202/**
203 * Returns `true` if `element` is a React element whose type is of a React `componentClass`.
204 */
205export function isElementOfType<T extends HTMLElement>(
206 element: ReactElement, type: string): element is ReactHTMLElement<T>;
207/**
208 * Returns `true` if `element` is a React element whose type is of a React `componentClass`.
209 */
210export function isElementOfType<P extends DOMAttributes<{}>, T extends Element>(
211 element: ReactElement, type: string): element is DOMElement<P, T>;
212/**
213 * Returns `true` if `element` is a React element whose type is of a React `componentClass`.
214 */
215export function isElementOfType<P>(
216 element: ReactElement, type: FC<P>): element is FunctionComponentElement<P>;
217/**
218 * Returns `true` if `element` is a React element whose type is of a React `componentClass`.
219 */
220export function isElementOfType<P, T extends Component<P>, C extends ComponentClass<P>>(
221 element: ReactElement, type: ClassType<P, T, C>): element is CElement<P, T>;
222
223/**
224 * Returns `true` if `instance` is a DOM component (such as a `<div>` or `<span>`).
225 */
226export function isDOMComponent(instance: ReactInstance): instance is Element;
227/**
228 * Returns `true` if `instance` is a user-defined component, such as a class or a function.
229 */
230export function isCompositeComponent(instance: ReactInstance): instance is Component<any>;
231/**
232 * Returns `true` if `instance` is a component whose type is of a React `componentClass`.
233 */
234export function isCompositeComponentWithType<T extends Component<any>, C extends ComponentClass<any>>(
235 instance: ReactInstance, type: ClassType<any, T, C>): boolean;
236
237/**
238 * Traverse all components in `tree` and accumulate all components where
239 * `test(component)` is `true`. This is not that useful on its own, but it's used
240 * as a primitive for other test utils.
241 */
242export function findAllInRenderedTree(
243 root: Component<any>,
244 fn: (i: ReactInstance) => boolean): ReactInstance[];
245
246/**
247 * Finds all DOM elements of components in the rendered tree that are
248 * DOM components with the class name matching `className`.
249 */
250export function scryRenderedDOMComponentsWithClass(
251 root: Component<any>,
252 className: string): Element[];
253/**
254 * Like `scryRenderedDOMComponentsWithClass()` but expects there to be one result,
255 * and returns that one result, or throws exception if there is any other
256 * number of matches besides one.
257 */
258export function findRenderedDOMComponentWithClass(
259 root: Component<any>,
260 className: string): Element;
261
262/**
263 * Finds all DOM elements of components in the rendered tree that are
264 * DOM components with the tag name matching `tagName`.
265 */
266export function scryRenderedDOMComponentsWithTag(
267 root: Component<any>,
268 tagName: string): Element[];
269/**
270 * Like `scryRenderedDOMComponentsWithTag()` but expects there to be one result,
271 * and returns that one result, or throws exception if there is any other
272 * number of matches besides one.
273 */
274export function findRenderedDOMComponentWithTag(
275 root: Component<any>,
276 tagName: string): Element;
277
278/**
279 * Finds all instances of components with type equal to `componentClass`.
280 */
281export function scryRenderedComponentsWithType<T extends Component<any>, C extends ComponentClass<any>>(
282 root: Component<any>,
283 type: ClassType<any, T, C>): T[];
284
285/**
286 * Same as `scryRenderedComponentsWithType()` but expects there to be one result
287 * and returns that one result, or throws exception if there is any other
288 * number of matches besides one.
289 */
290export function findRenderedComponentWithType<T extends Component<any>, C extends ComponentClass<any>>(
291 root: Component<any>,
292 type: ClassType<any, T, C>): T;
293
294/**
295 * Call this in your tests to create a shallow renderer.
296 */
297export function createRenderer(): ShallowRenderer;
298
299/**
300 * Wrap any code rendering and triggering updates to your components into `act()` calls.
301 *
302 * Ensures that the behavior in your tests matches what happens in the browser
303 * more closely by executing pending `useEffect`s before returning. This also
304 * reduces the amount of re-renders done.
305 *
306 * @param callback A synchronous, void callback that will execute as a single, complete React commit.
307 *
308 * @see https://reactjs.org/blog/2019/02/06/react-v16.8.0.html#testing-hooks
309 */
310// NOTES
311// - the order of these signatures matters - typescript will check the signatures in source order.
312// If the `() => VoidOrUndefinedOnly` signature is first, it'll erroneously match a Promise returning function for users with
313// `strictNullChecks: false`.
314// - VoidOrUndefinedOnly is there to forbid any non-void return values for users with `strictNullChecks: true`
315declare const UNDEFINED_VOID_ONLY: unique symbol;
316// tslint:disable-next-line: void-return
317type VoidOrUndefinedOnly = void | { [UNDEFINED_VOID_ONLY]: never };
318// While act does always return Thenable, if a void function is passed, we pretend the return value is also void to not trigger dangling Promise lint rules.
319export function act(callback: () => VoidOrUndefinedOnly): void;
320export function act<T>(callback: () => T | Promise<T>): Promise<T>;
321
322// Intentionally doesn't extend PromiseLike<never>.
323// Ideally this should be as hard to accidentally use as possible.
324export interface DebugPromiseLike {
325 // the actual then() in here is 0-ary, but that doesn't count as a PromiseLike.
326 then(onfulfilled: (value: never) => never, onrejected: (reason: never) => never): never;
327}