Skip to content
This repository was archived by the owner on Jul 30, 2020. It is now read-only.

Commit 7d5057a

Browse files
sz-piotrbcarroll22
authored andcommitted
feat: add TypeScript type declarations (#5)
Closes #2
1 parent 4455511 commit 7d5057a

File tree

2 files changed

+321
-1
lines changed

2 files changed

+321
-1
lines changed

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
"version": "0.0.0-semantically-released",
44
"description": "Simple and complete React Native testing utilities that encourage good testing practices.",
55
"main": "dist/index.js",
6+
"types": "dist/index.d.ts",
67
"files": [
78
"dist/**"
89
],
@@ -16,7 +17,7 @@
1617
"readme:toc": "doctoc README.md --maxlevel 3 --title '## Table of Contents'",
1718
"test": "jest",
1819
"pretty-quick": "pretty-quick --staged --pattern '**/*.(js|jsx|ts|tsx)'",
19-
"prepublish": "rm -rf dist; babel src --out-dir dist --ignore 'src/__tests__/*'",
20+
"prepublish": "rm -rf dist; babel src --out-dir dist --ignore 'src/__tests__/*' && cp src/index.d.ts dist/index.d.ts",
2021
"semantic-release": "semantic-release",
2122
"test:coverage": "jest --coverage",
2223
"test:watch": "jest --watch --coverage"

src/index.d.ts

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

0 commit comments

Comments
 (0)