UNPKG

6.9 kBTypeScriptView Raw
1import {
2 AbstractView,
3 CElement,
4 ClassType,
5 Component,
6 ComponentClass,
7 DOMAttributes,
8 DOMElement,
9 ReactElement,
10 ReactHTMLElement,
11 ReactInstance,
12 SFC,
13 SFCElement,
14} from "react";
15
16export = TestUtils;
17
18declare namespace TestUtils {
19 export interface OptionalEventProperties {
20 bubbles?: boolean | undefined;
21 cancelable?: boolean | undefined;
22 currentTarget?: EventTarget | undefined;
23 defaultPrevented?: boolean | undefined;
24 eventPhase?: number | undefined;
25 isTrusted?: boolean | undefined;
26 nativeEvent?: Event | undefined;
27 preventDefault?(): void;
28 stopPropagation?(): void;
29 target?: EventTarget | undefined;
30 timeStamp?: Date | undefined;
31 type?: string | undefined;
32 }
33
34 export interface SyntheticEventData extends OptionalEventProperties {
35 altKey?: boolean | undefined;
36 button?: number | undefined;
37 buttons?: number | undefined;
38 clientX?: number | undefined;
39 clientY?: number | undefined;
40 changedTouches?: TouchList | undefined;
41 charCode?: boolean | undefined;
42 clipboardData?: DataTransfer | undefined;
43 ctrlKey?: boolean | undefined;
44 deltaMode?: number | undefined;
45 deltaX?: number | undefined;
46 deltaY?: number | undefined;
47 deltaZ?: number | undefined;
48 detail?: number | undefined;
49 getModifierState?(key: string): boolean;
50 key?: string | undefined;
51 keyCode?: number | undefined;
52 locale?: string | undefined;
53 location?: number | undefined;
54 metaKey?: boolean | undefined;
55 pageX?: number | undefined;
56 pageY?: number | undefined;
57 relatedTarget?: EventTarget | undefined;
58 repeat?: boolean | undefined;
59 screenX?: number | undefined;
60 screenY?: number | undefined;
61 shiftKey?: boolean | undefined;
62 targetTouches?: TouchList | undefined;
63 touches?: TouchList | undefined;
64 view?: AbstractView | undefined;
65 which?: number | undefined;
66 }
67
68 export interface EventSimulator {
69 (element: Element | Component<any>, eventData?: SyntheticEventData): void;
70 }
71
72 export interface MockedComponentClass {
73 new(): any;
74 }
75
76 export interface ShallowRenderer {
77 getRenderOutput<E extends ReactElement>(): E;
78 render(element: ReactElement, context?: any): void;
79 unmount(): void;
80 }
81
82 export namespace Simulate {
83 export var blur: EventSimulator;
84 export var change: EventSimulator;
85 export var click: EventSimulator;
86 export var contextMenu: EventSimulator;
87 export var copy: EventSimulator;
88 export var cut: EventSimulator;
89 export var doubleClick: EventSimulator;
90 export var drag: EventSimulator;
91 export var dragEnd: EventSimulator;
92 export var dragEnter: EventSimulator;
93 export var dragExit: EventSimulator;
94 export var dragLeave: EventSimulator;
95 export var dragOver: EventSimulator;
96 export var dragStart: EventSimulator;
97 export var drop: EventSimulator;
98 export var error: EventSimulator;
99 export var focus: EventSimulator;
100 export var input: EventSimulator;
101 export var keyDown: EventSimulator;
102 export var keyPress: EventSimulator;
103 export var keyUp: EventSimulator;
104 export var load: EventSimulator;
105 export var mouseDown: EventSimulator;
106 export var mouseEnter: EventSimulator;
107 export var mouseLeave: EventSimulator;
108 export var mouseMove: EventSimulator;
109 export var mouseOut: EventSimulator;
110 export var mouseOver: EventSimulator;
111 export var mouseUp: EventSimulator;
112 export var paste: EventSimulator;
113 export var scroll: EventSimulator;
114 export var submit: EventSimulator;
115 export var touchCancel: EventSimulator;
116 export var touchEnd: EventSimulator;
117 export var touchMove: EventSimulator;
118 export var touchStart: EventSimulator;
119 export var wheel: EventSimulator;
120 }
121
122 export function renderIntoDocument<T extends Element>(
123 element: DOMElement<any, T>,
124 ): T;
125 export function renderIntoDocument(
126 element: SFCElement<any>,
127 ): void;
128 export function renderIntoDocument<T extends Component<any>>(
129 element: CElement<any, T>,
130 ): T;
131 export function renderIntoDocument<P>(
132 element: ReactElement<P>,
133 // eslint-disable-next-line @typescript-eslint/no-invalid-void-type
134 ): Component<P> | Element | void;
135
136 export function mockComponent(
137 mocked: MockedComponentClass,
138 mockTagName?: string,
139 ): typeof TestUtils;
140
141 export function isElementOfType<T extends HTMLElement>(
142 element: ReactElement,
143 type: string,
144 ): element is ReactHTMLElement<T>;
145 export function isElementOfType<P extends DOMAttributes<{}>, T extends Element>(
146 element: ReactElement,
147 type: string,
148 ): element is DOMElement<P, T>;
149 export function isElementOfType<P>(
150 element: ReactElement,
151 type: SFC<P>,
152 ): element is SFCElement<P>;
153 export function isElementOfType<P, T extends Component<P>, C extends ComponentClass<P>>(
154 element: ReactElement,
155 type: ClassType<P, T, C>,
156 ): element is CElement<P, T>;
157
158 export function isDOMComponent(instance: ReactInstance): instance is Element;
159 export function isCompositeComponent(instance: ReactInstance): instance is Component<any>;
160 export function isCompositeComponentWithType<T extends Component<any>, C extends ComponentClass<any>>(
161 instance: ReactInstance,
162 type: ClassType<any, T, C>,
163 ): T;
164
165 export function findAllInRenderedTree(
166 root: Component<any>,
167 fn: (i: ReactInstance) => boolean,
168 ): ReactInstance[];
169
170 export function scryRenderedDOMComponentsWithClass(
171 root: Component<any>,
172 className: string,
173 ): Element[];
174 export function findRenderedDOMComponentWithClass(
175 root: Component<any>,
176 className: string,
177 ): Element;
178
179 export function scryRenderedDOMComponentsWithTag(
180 root: Component<any>,
181 tagName: string,
182 ): Element[];
183 export function findRenderedDOMComponentWithTag(
184 root: Component<any>,
185 tagName: string,
186 ): Element;
187
188 export function scryRenderedComponentsWithType<T extends Component<any>, C extends ComponentClass<any>>(
189 root: Component<any>,
190 type: ClassType<any, T, C>,
191 ): T[];
192
193 export function findRenderedComponentWithType<T extends Component<any>, C extends ComponentClass<any>>(
194 root: Component<any>,
195 type: ClassType<any, T, C>,
196 ): T;
197
198 export function createRenderer(): ShallowRenderer;
199}