|
| 1 | +import { ReactElement, ComponentType } from 'react' |
| 2 | +import { ReactTestInstance, ReactTestRenderer, act } from 'react-test-renderer' |
| 3 | + |
| 4 | +// EVENTS |
| 5 | +// ------ |
| 6 | + |
| 7 | +// TODO: type second parameter |
| 8 | +export declare class NativeEvent { |
| 9 | + constructor(typeArg: string, event?: any) |
| 10 | +} |
| 11 | + |
| 12 | +export declare function getEventHandlerName(key: string): string |
| 13 | + |
| 14 | +export interface FireEventFn { |
| 15 | + (element: FiberRoot, event: NativeEvent): any |
| 16 | + focus(element: FiberRoot, init?: any): any |
| 17 | + blur(element: FiberRoot, init?: any): any |
| 18 | + change(element: FiberRoot, init?: any): any |
| 19 | + changeText(element: FiberRoot, value: string): any |
| 20 | + contentSizeChange(element: FiberRoot, init?: any): any |
| 21 | + endEditing(element: FiberRoot, init?: any): any |
| 22 | + keyPress(element: FiberRoot, init?: any): any |
| 23 | + submitEditing(element: FiberRoot, init?: any): any |
| 24 | + layout(element: FiberRoot, init?: any): any |
| 25 | + selectionChange(element: FiberRoot, init?: any): any |
| 26 | + longPress(element: FiberRoot, init?: any): any |
| 27 | + press(element: FiberRoot, init?: any): any |
| 28 | + pressIn(element: FiberRoot, init?: any): any |
| 29 | + pressOut(element: FiberRoot, init?: any): any |
| 30 | + momentumScrollBegin(element: FiberRoot, init?: any): any |
| 31 | + momentumScrollEnd(element: FiberRoot, init?: any): any |
| 32 | + scroll(element: FiberRoot, init?: any): any |
| 33 | + scrollBeginDrag(element: FiberRoot, init?: any): any |
| 34 | + scrollEndDrag(element: FiberRoot, init?: any): any |
| 35 | + load(element: FiberRoot, init?: any): any |
| 36 | + error(element: FiberRoot, init?: any): any |
| 37 | + progress(element: FiberRoot, init?: any): any |
| 38 | +} |
| 39 | +export declare const fireEvent: FireEventFn |
| 40 | + |
| 41 | +// GET NODE TEXT |
| 42 | +// ------------- |
| 43 | + |
| 44 | +export declare function getNodeText(node: FiberRoot): string |
| 45 | + |
| 46 | +// GET QUERIES FOR ELEMENT |
| 47 | +// ----------------------- |
| 48 | + |
| 49 | +export declare function getQueriesForElement<T = Queries>(element: ReactElement, queries?: T): BoundQueries<T> |
| 50 | +export declare function within<T = Queries>(element: ReactElement, queries?: T): BoundQueries<T> |
| 51 | + |
| 52 | +// PREETY PRINT |
| 53 | +// ------------ |
| 54 | + |
| 55 | +// TODO: options are pretty-format options |
| 56 | +export declare function prettyPrint(element: ReactTestRenderer | FiberRoot, maxLength?: number, options?: any): string |
| 57 | + |
| 58 | +// QUERIES |
| 59 | +// ------- |
| 60 | + |
| 61 | +type Omit<T, K extends keyof T> = Pick<T, Exclude<keyof T, K>> |
| 62 | +type Bound<T> = T extends (arg: any, ...rest: infer U) => infer V ? (...args: U) => V : never |
| 63 | +type BoundQueries<T> = { [P in keyof T]: Bound<T[P]> } |
| 64 | + |
| 65 | +export type FiberRoot = Omit<ReactTestInstance, 'find' | 'findAllByProps' | 'findAllByType' | 'findByProps' | 'findByType' | 'instance'> |
| 66 | + |
| 67 | +export type TextMatch = string | RegExp | ((value: string) => boolean) |
| 68 | +export type FilterFn = (value: string, index: number) => boolean |
| 69 | +export type NormalizerFn = (input: string) => string |
| 70 | + |
| 71 | +export interface NormalizerOptions { |
| 72 | + exact?: boolean, |
| 73 | + trim?: boolean, |
| 74 | + collapseWhitespace?: boolean, |
| 75 | + filter?: FilterFn, |
| 76 | + normalizer?: NormalizerFn, |
| 77 | +} |
| 78 | + |
| 79 | +export interface TextNormalizerOptions extends NormalizerOptions { |
| 80 | + types?: string[] |
| 81 | +} |
| 82 | + |
| 83 | +export declare function getByA11yHint(container: FiberRoot, match: TextMatch, options?: NormalizerOptions): FiberRoot |
| 84 | +export declare function getByA11yLabel(container: FiberRoot, match: TextMatch, options?: NormalizerOptions): FiberRoot |
| 85 | +export declare function getByA11yRole(container: FiberRoot, match: TextMatch, options?: NormalizerOptions): FiberRoot |
| 86 | +export declare function getByA11yStates(container: FiberRoot, match: string[], options?: NormalizerOptions): FiberRoot |
| 87 | +export declare function getByA11yTraits(container: FiberRoot, match: string[], options?: NormalizerOptions): FiberRoot |
| 88 | +export declare function getByPlaceholder(container: FiberRoot, match: TextMatch, options?: NormalizerOptions): FiberRoot |
| 89 | +export declare function getByText(container: FiberRoot, match: TextMatch, options?: TextNormalizerOptions): FiberRoot |
| 90 | +export declare function getByValue(container: FiberRoot, match: TextMatch, options?: NormalizerOptions): FiberRoot |
| 91 | +export declare function getByTestId(container: FiberRoot, match: TextMatch, options?: NormalizerOptions): FiberRoot |
| 92 | + |
| 93 | +export declare function getAllByA11yHint(container: FiberRoot, match: TextMatch, options?: NormalizerOptions): FiberRoot[] |
| 94 | +export declare function getAllByA11yLabel(container: FiberRoot, match: TextMatch, options?: NormalizerOptions): FiberRoot[] |
| 95 | +export declare function getAllByA11yRole(container: FiberRoot, match: TextMatch, options?: NormalizerOptions): FiberRoot[] |
| 96 | +export declare function getAllByA11yStates(container: FiberRoot, match: string[], options?: NormalizerOptions): FiberRoot[] |
| 97 | +export declare function getAllByA11yTraits(container: FiberRoot, match: string[], options?: NormalizerOptions): FiberRoot[] |
| 98 | +export declare function getAllByPlaceholder(container: FiberRoot, match: TextMatch, options?: NormalizerOptions): FiberRoot[] |
| 99 | +export declare function getAllByText(container: FiberRoot, match: TextMatch, options?: TextNormalizerOptions): FiberRoot[] |
| 100 | +export declare function getAllByValue(container: FiberRoot, match: TextMatch, options?: NormalizerOptions): FiberRoot[] |
| 101 | +export declare function getAllByTestId(container: FiberRoot, match: TextMatch, options?: NormalizerOptions): FiberRoot[] |
| 102 | + |
| 103 | +export declare function queryByA11yHint(container: FiberRoot, match: TextMatch, options?: NormalizerOptions): FiberRoot | null |
| 104 | +export declare function queryByA11yLabel(container: FiberRoot, match: TextMatch, options?: NormalizerOptions): FiberRoot | null |
| 105 | +export declare function queryByA11yRole(container: FiberRoot, match: TextMatch, options?: NormalizerOptions): FiberRoot | null |
| 106 | +export declare function queryByA11yStates(container: FiberRoot, match: string[], options?: NormalizerOptions): FiberRoot | null |
| 107 | +export declare function queryByA11yTraits(container: FiberRoot, match: string[], options?: NormalizerOptions): FiberRoot | null |
| 108 | +export declare function queryByPlaceholder(container: FiberRoot, match: TextMatch, options?: NormalizerOptions): FiberRoot | null |
| 109 | +export declare function queryByText(container: FiberRoot, match: TextMatch, options?: TextNormalizerOptions): FiberRoot | null |
| 110 | +export declare function queryByValue(container: FiberRoot, match: TextMatch, options?: NormalizerOptions): FiberRoot | null |
| 111 | +export declare function queryByTestId(container: FiberRoot, match: TextMatch, options?: NormalizerOptions): FiberRoot | null |
| 112 | + |
| 113 | +export declare function findByA11yHint(container: FiberRoot, match: TextMatch, options?: NormalizerOptions): Promise<FiberRoot> |
| 114 | +export declare function findByA11yLabel(container: FiberRoot, match: TextMatch, options?: NormalizerOptions): Promise<FiberRoot> |
| 115 | +export declare function findByA11yRole(container: FiberRoot, match: TextMatch, options?: NormalizerOptions): Promise<FiberRoot> |
| 116 | +export declare function findByA11yStates(container: FiberRoot, match: string[], options?: NormalizerOptions): Promise<FiberRoot> |
| 117 | +export declare function findByA11yTraits(container: FiberRoot, match: string[], options?: NormalizerOptions): Promise<FiberRoot> |
| 118 | +export declare function findByPlaceholder(container: FiberRoot, match: TextMatch, options?: NormalizerOptions): Promise<FiberRoot> |
| 119 | +export declare function findByText(container: FiberRoot, match: TextMatch, options?: TextNormalizerOptions): Promise<FiberRoot> |
| 120 | +export declare function findByValue(container: FiberRoot, match: TextMatch, options?: NormalizerOptions): Promise<FiberRoot> |
| 121 | +export declare function findByTestId(container: FiberRoot, match: TextMatch, options?: NormalizerOptions): Promise<FiberRoot> |
| 122 | + |
| 123 | +export declare function findAllByA11yHint(container: FiberRoot, match: TextMatch, options?: NormalizerOptions): Promise<FiberRoot[]> |
| 124 | +export declare function findAllByA11yLabel(container: FiberRoot, match: TextMatch, options?: NormalizerOptions): Promise<FiberRoot[]> |
| 125 | +export declare function findAllByA11yRole(container: FiberRoot, match: TextMatch, options?: NormalizerOptions): Promise<FiberRoot[]> |
| 126 | +export declare function findAllByA11yStates(container: FiberRoot, match: string[], options?: NormalizerOptions): Promise<FiberRoot[]> |
| 127 | +export declare function findAllByA11yTraits(container: FiberRoot, match: string[], options?: NormalizerOptions): Promise<FiberRoot[]> |
| 128 | +export declare function findAllByPlaceholder(container: FiberRoot, match: TextMatch, options?: NormalizerOptions): Promise<FiberRoot[]> |
| 129 | +export declare function findAllByText(container: FiberRoot, match: TextMatch, options?: TextNormalizerOptions): Promise<FiberRoot[]> |
| 130 | +export declare function findAllByValue(container: FiberRoot, match: TextMatch, options?: NormalizerOptions): Promise<FiberRoot[]> |
| 131 | +export declare function findAllByTestId(container: FiberRoot, match: TextMatch, options?: NormalizerOptions): Promise<FiberRoot[]> |
| 132 | + |
| 133 | +export interface Queries { |
| 134 | + getByA11yHint: typeof getByA11yHint |
| 135 | + getByA11yLabel: typeof getByA11yLabel |
| 136 | + getByA11yRole: typeof getByA11yRole |
| 137 | + getByA11yStates: typeof getByA11yStates |
| 138 | + getByA11yTraits: typeof getByA11yTraits |
| 139 | + getByPlaceholder: typeof getByPlaceholder |
| 140 | + getByText: typeof getByText |
| 141 | + getByValue: typeof getByA11yHint |
| 142 | + getByTestId: typeof getByTestId |
| 143 | + |
| 144 | + getAllByA11yHint: typeof getAllByA11yHint |
| 145 | + getAllByA11yLabel: typeof getAllByA11yLabel |
| 146 | + getAllByA11yRole: typeof getAllByA11yRole |
| 147 | + getAllByA11yStates: typeof getAllByA11yStates |
| 148 | + getAllByA11yTraits: typeof getAllByA11yTraits |
| 149 | + getAllByPlaceholder: typeof getAllByPlaceholder |
| 150 | + getAllByText: typeof getAllByText |
| 151 | + getAllByValue: typeof getAllByA11yHint |
| 152 | + getAllByTestId: typeof getAllByTestId |
| 153 | + |
| 154 | + queryByA11yHint: typeof queryByA11yHint |
| 155 | + queryByA11yLabel: typeof queryByA11yLabel |
| 156 | + queryByA11yRole: typeof queryByA11yRole |
| 157 | + queryByA11yStates: typeof queryByA11yStates |
| 158 | + queryByA11yTraits: typeof queryByA11yTraits |
| 159 | + queryByPlaceholder: typeof queryByPlaceholder |
| 160 | + queryByText: typeof queryByText |
| 161 | + queryByValue: typeof queryByA11yHint |
| 162 | + queryByTestId: typeof queryByTestId |
| 163 | + |
| 164 | + findByA11yHint: typeof findByA11yHint |
| 165 | + findByA11yLabel: typeof findByA11yLabel |
| 166 | + findByA11yRole: typeof findByA11yRole |
| 167 | + findByA11yStates: typeof findByA11yStates |
| 168 | + findByA11yTraits: typeof findByA11yTraits |
| 169 | + findByPlaceholder: typeof findByPlaceholder |
| 170 | + findByText: typeof findByText |
| 171 | + findByValue: typeof findByA11yHint |
| 172 | + findByTestId: typeof findByTestId |
| 173 | + |
| 174 | + findAllByA11yHint: typeof findAllByA11yHint |
| 175 | + findAllByA11yLabel: typeof findAllByA11yLabel |
| 176 | + findAllByA11yRole: typeof findAllByA11yRole |
| 177 | + findAllByA11yStates: typeof findAllByA11yStates |
| 178 | + findAllByA11yTraits: typeof findAllByA11yTraits |
| 179 | + findAllByPlaceholder: typeof findAllByPlaceholder |
| 180 | + findAllByText: typeof findAllByText |
| 181 | + findAllByValue: typeof findAllByA11yHint |
| 182 | + findAllByTestId: typeof findAllByTestId |
| 183 | +} |
| 184 | + |
| 185 | +// QUERY HELPERS |
| 186 | +// ------------- |
| 187 | + |
| 188 | +export declare function defaultFilter(node: FiberRoot): boolean |
| 189 | +export declare function getBaseElement(container: ReactTestRenderer | ReactTestInstance): ReactTestInstance |
| 190 | +export declare function getElementError(message: string, container: ReactTestRenderer): Error |
| 191 | +export declare function firstResultOrNull<T extends any[], U>(query: (...args: T) => U[], ...args: T): U | null |
| 192 | +export declare function filterNodeByType(node: FiberRoot, type: string): boolean |
| 193 | +export declare function queryAllByProp( |
| 194 | + attribute: string, |
| 195 | + container: ReactTestRenderer, |
| 196 | + match: TextMatch, |
| 197 | + options?: NormalizerOptions, |
| 198 | +): FiberRoot[] |
| 199 | +export declare function queryByProp( |
| 200 | + attribute: string, |
| 201 | + container: ReactTestRenderer, |
| 202 | + match: TextMatch, |
| 203 | + options?: NormalizerOptions, |
| 204 | +): FiberRoot | null |
| 205 | +export function removeBadProperties(node: ReactTestInstance): FiberRoot |
| 206 | + |
| 207 | +// WAIT |
| 208 | +// ---- |
| 209 | + |
| 210 | +export interface WaitOptions { |
| 211 | + timeout?: number |
| 212 | + interval?: number |
| 213 | +} |
| 214 | +export declare function wait(callback?: () => void, options?: WaitOptions): Promise<void> |
| 215 | + |
| 216 | +// WAIT FOR ELEMENT |
| 217 | +// ---------------- |
| 218 | + |
| 219 | +export interface WaitOptions { |
| 220 | + timeout?: number |
| 221 | + interval?: number |
| 222 | +} |
| 223 | +export declare function waitForElement<T>(callback: () => T, options?: WaitOptions): Promise<T> |
| 224 | + |
| 225 | +// WAIT FOR ELEMENT TO BE REMOVED |
| 226 | +// ------------------------------ |
| 227 | + |
| 228 | +export interface WaitOptions { |
| 229 | + timeout?: number |
| 230 | + interval?: number |
| 231 | +} |
| 232 | +export declare function waitForElementToBeRemoved(callback: () => any, options?: WaitOptions): Promise<null> |
| 233 | + |
| 234 | +// MATCHES |
| 235 | +// ------- |
| 236 | + |
| 237 | +export interface DefaultNormalizerOptions { |
| 238 | + trim?: boolean, |
| 239 | + collapseWhitespace?: boolean, |
| 240 | +} |
| 241 | +export declare function getDefaultNormalizer(options: DefaultNormalizerOptions): NormalizerFn |
| 242 | + |
| 243 | +// INDEX |
| 244 | +// ----- |
| 245 | + |
| 246 | +export interface RenderOptions { |
| 247 | + wrapper?: ComponentType<{ children: ReactElement }> |
| 248 | +} |
| 249 | +export interface RenderOptionsWithQueries<T> extends RenderOptions { |
| 250 | + queries?: T |
| 251 | +} |
| 252 | + |
| 253 | +export declare function render(ui: ReactElement, options?: RenderOptions): RenderResult & BoundQueries<Queries> |
| 254 | +export declare function render<T>(ui: ReactElement, options: RenderOptionsWithQueries<T>): RenderResult & BoundQueries<T> |
| 255 | + |
| 256 | +export interface RenderResult { |
| 257 | + container: ReactTestRenderer |
| 258 | + baseElement: FiberRoot |
| 259 | + debug: () => void |
| 260 | + rerender: (ui: ReactElement) => void |
| 261 | + unmount: () => void |
| 262 | +} |
| 263 | + |
| 264 | +export interface RenderHookOptions<T> extends RenderOptions { |
| 265 | + initialProps?: T |
| 266 | +} |
| 267 | + |
| 268 | +export declare function renderHook<T, U>(callback: (props: T) => U, options?: RenderHookOptions<T>): RenderHookResult<T, U> |
| 269 | + |
| 270 | +export interface RenderHookResult<T, U> { |
| 271 | + result: { |
| 272 | + current: U |
| 273 | + } |
| 274 | + error?: Error |
| 275 | + waitForNextUpdate: () => Promise<void> |
| 276 | + rerender: (newProps?: T) => void |
| 277 | + unmount: () => void |
| 278 | +} |
| 279 | + |
| 280 | +export { act } |
0 commit comments