This repository was archived by the owner on Jul 30, 2020. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 44
Add typescript type declaration #5
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
420e3f6
Add typescript type declaration
sz-piotr d7d3c38
Rename FiberRoot to NativeTestInstance
sz-piotr c153fce
Import OptionsReceived from prettyFormat
sz-piotr a6c2d16
Add types for NativeEvent
sz-piotr 33bbe01
Add validTargets to EventInit
sz-piotr File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,319 @@ | ||
import { ReactElement, ComponentType } from 'react' | ||
import { ReactTestInstance, ReactTestRenderer, act } from 'react-test-renderer' | ||
import { OptionsReceived } from 'pretty-format' | ||
import { | ||
NativeSyntheticEvent, | ||
TextInputFocusEventData, | ||
TextInputChangeEventData, | ||
TextInputContentSizeChangeEventData, | ||
TextInputEndEditingEventData, | ||
TextInputKeyPressEventData, | ||
TextInputSubmitEditingEventData, | ||
LayoutChangeEvent, | ||
TextInputSelectionChangeEventData, | ||
GestureResponderEvent, | ||
ScrollResponderEvent, | ||
ImageLoadEventData, | ||
ImageErrorEventData, | ||
ImageProgressEventDataIOS, | ||
} from 'react-native' | ||
|
||
// EVENTS | ||
// ------ | ||
|
||
type EventInit<T> = Partial<NativeSyntheticEvent<T>> & { validTargets?: string[] } | ||
|
||
export declare class NativeEvent { | ||
constructor(type: 'focus', init?: EventInit<TextInputFocusEventData>) | ||
constructor(type: 'blur', init?: EventInit<TextInputFocusEventData>) | ||
constructor(type: 'change', init?: EventInit<TextInputChangeEventData>) | ||
constructor(type: 'changeText', value: string) | ||
constructor(type: 'contentSizeChange', init?: EventInit<TextInputContentSizeChangeEventData>) | ||
constructor(type: 'endEditing', init?: EventInit<TextInputEndEditingEventData>) | ||
constructor(type: 'keyPress', init?: EventInit<TextInputKeyPressEventData>) | ||
constructor(type: 'submitEditing', init?: EventInit<TextInputSubmitEditingEventData>) | ||
constructor(type: 'layout', init?: EventInit<LayoutChangeEvent['nativeEvent']>) | ||
constructor(type: 'selectionChange', init?: EventInit<TextInputSelectionChangeEventData>) | ||
constructor(type: 'longPress', init?: EventInit<GestureResponderEvent>) | ||
constructor(type: 'press', init?: EventInit<GestureResponderEvent>) | ||
constructor(type: 'pressIn', init?: EventInit<GestureResponderEvent>) | ||
constructor(type: 'pressOut', init?: EventInit<GestureResponderEvent>) | ||
constructor(type: 'momentumScrollBegin', init?: EventInit<ScrollResponderEvent>) | ||
constructor(type: 'momentumScrollEnd', init?: EventInit<ScrollResponderEvent>) | ||
constructor(type: 'scroll', init?: EventInit<ScrollResponderEvent>) | ||
constructor(type: 'scrollBeginDrag', init?: EventInit<ScrollResponderEvent>) | ||
constructor(type: 'scrollEndDrag', init?: EventInit<ScrollResponderEvent>) | ||
constructor(type: 'load', init?: EventInit<ImageLoadEventData>) | ||
constructor(type: 'error', init?: EventInit<ImageErrorEventData>) | ||
constructor(type: 'progress', init?: EventInit<ImageProgressEventDataIOS>) | ||
} | ||
|
||
export declare function getEventHandlerName(key: string): string | ||
|
||
export interface FireEventFn { | ||
(element: NativeTestInstance, event: NativeEvent): any | ||
focus(element: NativeTestInstance, init?: EventInit<TextInputFocusEventData>): any | ||
blur(element: NativeTestInstance, init?: EventInit<TextInputFocusEventData>): any | ||
change(element: NativeTestInstance, init?: EventInit<TextInputChangeEventData>): any | ||
changeText(element: NativeTestInstance, value: string): any | ||
contentSizeChange(element: NativeTestInstance, init?: EventInit<TextInputContentSizeChangeEventData>): any | ||
endEditing(element: NativeTestInstance, init?: EventInit<TextInputEndEditingEventData>): any | ||
keyPress(element: NativeTestInstance, init?: EventInit<TextInputKeyPressEventData>): any | ||
submitEditing(element: NativeTestInstance, init?: EventInit<TextInputSubmitEditingEventData>): any | ||
layout(element: NativeTestInstance, init?: EventInit<LayoutChangeEvent['nativeEvent']>): any | ||
selectionChange(element: NativeTestInstance, init?: EventInit<TextInputSelectionChangeEventData>): any | ||
longPress(element: NativeTestInstance, init?: EventInit<GestureResponderEvent>): any | ||
press(element: NativeTestInstance, init?: EventInit<GestureResponderEvent>): any | ||
pressIn(element: NativeTestInstance, init?: EventInit<GestureResponderEvent>): any | ||
pressOut(element: NativeTestInstance, init?: EventInit<GestureResponderEvent>): any | ||
momentumScrollBegin(element: NativeTestInstance, init?: EventInit<ScrollResponderEvent>): any | ||
momentumScrollEnd(element: NativeTestInstance, init?: EventInit<ScrollResponderEvent>): any | ||
scroll(element: NativeTestInstance, init?: EventInit<ScrollResponderEvent>): any | ||
scrollBeginDrag(element: NativeTestInstance, init?: EventInit<ScrollResponderEvent>): any | ||
scrollEndDrag(element: NativeTestInstance, init?: EventInit<ScrollResponderEvent>): any | ||
load(element: NativeTestInstance, init?: EventInit<ImageLoadEventData>): any | ||
error(element: NativeTestInstance, init?: EventInit<ImageErrorEventData>): any | ||
progress(element: NativeTestInstance, init?: EventInit<ImageProgressEventDataIOS>): any | ||
} | ||
|
||
export declare const fireEvent: FireEventFn | ||
|
||
// GET NODE TEXT | ||
// ------------- | ||
|
||
export declare function getNodeText(node: NativeTestInstance): string | ||
|
||
// GET QUERIES FOR ELEMENT | ||
// ----------------------- | ||
|
||
export declare function getQueriesForElement<T = Queries>(element: ReactElement, queries?: T): BoundQueries<T> | ||
export declare function within<T = Queries>(element: ReactElement, queries?: T): BoundQueries<T> | ||
|
||
// PREETY PRINT | ||
// ------------ | ||
|
||
export declare function prettyPrint(element: ReactTestRenderer | NativeTestInstance | string, maxLength?: number, options?: OptionsReceived): string | ||
|
||
// QUERIES | ||
// ------- | ||
|
||
type Omit<T, K extends keyof T> = Pick<T, Exclude<keyof T, K>> | ||
type Bound<T> = T extends (arg: any, ...rest: infer U) => infer V ? (...args: U) => V : never | ||
type BoundQueries<T> = { [P in keyof T]: Bound<T[P]> } | ||
|
||
export type NativeTestInstance = Omit<ReactTestInstance, 'find' | 'findAllByProps' | 'findAllByType' | 'findByProps' | 'findByType' | 'instance'> | ||
|
||
export type TextMatch = string | RegExp | ((value: string) => boolean) | ||
export type FilterFn = (value: string, index: number) => boolean | ||
export type NormalizerFn = (input: string) => string | ||
|
||
export interface NormalizerOptions { | ||
exact?: boolean, | ||
trim?: boolean, | ||
collapseWhitespace?: boolean, | ||
filter?: FilterFn, | ||
normalizer?: NormalizerFn, | ||
} | ||
|
||
export interface TextNormalizerOptions extends NormalizerOptions { | ||
types?: string[] | ||
} | ||
|
||
export declare function getByA11yHint(container: NativeTestInstance, match: TextMatch, options?: NormalizerOptions): NativeTestInstance | ||
export declare function getByA11yLabel(container: NativeTestInstance, match: TextMatch, options?: NormalizerOptions): NativeTestInstance | ||
export declare function getByA11yRole(container: NativeTestInstance, match: TextMatch, options?: NormalizerOptions): NativeTestInstance | ||
export declare function getByA11yStates(container: NativeTestInstance, match: string[], options?: NormalizerOptions): NativeTestInstance | ||
export declare function getByA11yTraits(container: NativeTestInstance, match: string[], options?: NormalizerOptions): NativeTestInstance | ||
export declare function getByPlaceholder(container: NativeTestInstance, match: TextMatch, options?: NormalizerOptions): NativeTestInstance | ||
export declare function getByText(container: NativeTestInstance, match: TextMatch, options?: TextNormalizerOptions): NativeTestInstance | ||
export declare function getByValue(container: NativeTestInstance, match: TextMatch, options?: NormalizerOptions): NativeTestInstance | ||
export declare function getByTestId(container: NativeTestInstance, match: TextMatch, options?: NormalizerOptions): NativeTestInstance | ||
|
||
export declare function getAllByA11yHint(container: NativeTestInstance, match: TextMatch, options?: NormalizerOptions): NativeTestInstance[] | ||
export declare function getAllByA11yLabel(container: NativeTestInstance, match: TextMatch, options?: NormalizerOptions): NativeTestInstance[] | ||
export declare function getAllByA11yRole(container: NativeTestInstance, match: TextMatch, options?: NormalizerOptions): NativeTestInstance[] | ||
export declare function getAllByA11yStates(container: NativeTestInstance, match: string[], options?: NormalizerOptions): NativeTestInstance[] | ||
export declare function getAllByA11yTraits(container: NativeTestInstance, match: string[], options?: NormalizerOptions): NativeTestInstance[] | ||
export declare function getAllByPlaceholder(container: NativeTestInstance, match: TextMatch, options?: NormalizerOptions): NativeTestInstance[] | ||
export declare function getAllByText(container: NativeTestInstance, match: TextMatch, options?: TextNormalizerOptions): NativeTestInstance[] | ||
export declare function getAllByValue(container: NativeTestInstance, match: TextMatch, options?: NormalizerOptions): NativeTestInstance[] | ||
export declare function getAllByTestId(container: NativeTestInstance, match: TextMatch, options?: NormalizerOptions): NativeTestInstance[] | ||
|
||
export declare function queryByA11yHint(container: NativeTestInstance, match: TextMatch, options?: NormalizerOptions): NativeTestInstance | null | ||
export declare function queryByA11yLabel(container: NativeTestInstance, match: TextMatch, options?: NormalizerOptions): NativeTestInstance | null | ||
export declare function queryByA11yRole(container: NativeTestInstance, match: TextMatch, options?: NormalizerOptions): NativeTestInstance | null | ||
export declare function queryByA11yStates(container: NativeTestInstance, match: string[], options?: NormalizerOptions): NativeTestInstance | null | ||
export declare function queryByA11yTraits(container: NativeTestInstance, match: string[], options?: NormalizerOptions): NativeTestInstance | null | ||
export declare function queryByPlaceholder(container: NativeTestInstance, match: TextMatch, options?: NormalizerOptions): NativeTestInstance | null | ||
export declare function queryByText(container: NativeTestInstance, match: TextMatch, options?: TextNormalizerOptions): NativeTestInstance | null | ||
export declare function queryByValue(container: NativeTestInstance, match: TextMatch, options?: NormalizerOptions): NativeTestInstance | null | ||
export declare function queryByTestId(container: NativeTestInstance, match: TextMatch, options?: NormalizerOptions): NativeTestInstance | null | ||
|
||
export declare function findByA11yHint(container: NativeTestInstance, match: TextMatch, options?: NormalizerOptions): Promise<NativeTestInstance> | ||
export declare function findByA11yLabel(container: NativeTestInstance, match: TextMatch, options?: NormalizerOptions): Promise<NativeTestInstance> | ||
export declare function findByA11yRole(container: NativeTestInstance, match: TextMatch, options?: NormalizerOptions): Promise<NativeTestInstance> | ||
export declare function findByA11yStates(container: NativeTestInstance, match: string[], options?: NormalizerOptions): Promise<NativeTestInstance> | ||
export declare function findByA11yTraits(container: NativeTestInstance, match: string[], options?: NormalizerOptions): Promise<NativeTestInstance> | ||
export declare function findByPlaceholder(container: NativeTestInstance, match: TextMatch, options?: NormalizerOptions): Promise<NativeTestInstance> | ||
export declare function findByText(container: NativeTestInstance, match: TextMatch, options?: TextNormalizerOptions): Promise<NativeTestInstance> | ||
export declare function findByValue(container: NativeTestInstance, match: TextMatch, options?: NormalizerOptions): Promise<NativeTestInstance> | ||
export declare function findByTestId(container: NativeTestInstance, match: TextMatch, options?: NormalizerOptions): Promise<NativeTestInstance> | ||
|
||
export declare function findAllByA11yHint(container: NativeTestInstance, match: TextMatch, options?: NormalizerOptions): Promise<NativeTestInstance[]> | ||
export declare function findAllByA11yLabel(container: NativeTestInstance, match: TextMatch, options?: NormalizerOptions): Promise<NativeTestInstance[]> | ||
export declare function findAllByA11yRole(container: NativeTestInstance, match: TextMatch, options?: NormalizerOptions): Promise<NativeTestInstance[]> | ||
export declare function findAllByA11yStates(container: NativeTestInstance, match: string[], options?: NormalizerOptions): Promise<NativeTestInstance[]> | ||
export declare function findAllByA11yTraits(container: NativeTestInstance, match: string[], options?: NormalizerOptions): Promise<NativeTestInstance[]> | ||
export declare function findAllByPlaceholder(container: NativeTestInstance, match: TextMatch, options?: NormalizerOptions): Promise<NativeTestInstance[]> | ||
export declare function findAllByText(container: NativeTestInstance, match: TextMatch, options?: TextNormalizerOptions): Promise<NativeTestInstance[]> | ||
export declare function findAllByValue(container: NativeTestInstance, match: TextMatch, options?: NormalizerOptions): Promise<NativeTestInstance[]> | ||
export declare function findAllByTestId(container: NativeTestInstance, match: TextMatch, options?: NormalizerOptions): Promise<NativeTestInstance[]> | ||
|
||
export interface Queries { | ||
getByA11yHint: typeof getByA11yHint | ||
getByA11yLabel: typeof getByA11yLabel | ||
getByA11yRole: typeof getByA11yRole | ||
getByA11yStates: typeof getByA11yStates | ||
getByA11yTraits: typeof getByA11yTraits | ||
getByPlaceholder: typeof getByPlaceholder | ||
getByText: typeof getByText | ||
getByValue: typeof getByA11yHint | ||
getByTestId: typeof getByTestId | ||
|
||
getAllByA11yHint: typeof getAllByA11yHint | ||
getAllByA11yLabel: typeof getAllByA11yLabel | ||
getAllByA11yRole: typeof getAllByA11yRole | ||
getAllByA11yStates: typeof getAllByA11yStates | ||
getAllByA11yTraits: typeof getAllByA11yTraits | ||
getAllByPlaceholder: typeof getAllByPlaceholder | ||
getAllByText: typeof getAllByText | ||
getAllByValue: typeof getAllByA11yHint | ||
getAllByTestId: typeof getAllByTestId | ||
|
||
queryByA11yHint: typeof queryByA11yHint | ||
queryByA11yLabel: typeof queryByA11yLabel | ||
queryByA11yRole: typeof queryByA11yRole | ||
queryByA11yStates: typeof queryByA11yStates | ||
queryByA11yTraits: typeof queryByA11yTraits | ||
queryByPlaceholder: typeof queryByPlaceholder | ||
queryByText: typeof queryByText | ||
queryByValue: typeof queryByA11yHint | ||
queryByTestId: typeof queryByTestId | ||
|
||
findByA11yHint: typeof findByA11yHint | ||
findByA11yLabel: typeof findByA11yLabel | ||
findByA11yRole: typeof findByA11yRole | ||
findByA11yStates: typeof findByA11yStates | ||
findByA11yTraits: typeof findByA11yTraits | ||
findByPlaceholder: typeof findByPlaceholder | ||
findByText: typeof findByText | ||
findByValue: typeof findByA11yHint | ||
findByTestId: typeof findByTestId | ||
|
||
findAllByA11yHint: typeof findAllByA11yHint | ||
findAllByA11yLabel: typeof findAllByA11yLabel | ||
findAllByA11yRole: typeof findAllByA11yRole | ||
findAllByA11yStates: typeof findAllByA11yStates | ||
findAllByA11yTraits: typeof findAllByA11yTraits | ||
findAllByPlaceholder: typeof findAllByPlaceholder | ||
findAllByText: typeof findAllByText | ||
findAllByValue: typeof findAllByA11yHint | ||
findAllByTestId: typeof findAllByTestId | ||
} | ||
|
||
// QUERY HELPERS | ||
// ------------- | ||
|
||
export declare function defaultFilter(node: NativeTestInstance): boolean | ||
export declare function getBaseElement(container: ReactTestRenderer | ReactTestInstance): ReactTestInstance | ||
export declare function getElementError(message: string, container: ReactTestRenderer): Error | ||
export declare function firstResultOrNull<T extends any[], U>(query: (...args: T) => U[], ...args: T): U | null | ||
export declare function filterNodeByType(node: NativeTestInstance, type: string): boolean | ||
export declare function queryAllByProp( | ||
attribute: string, | ||
container: ReactTestRenderer, | ||
match: TextMatch, | ||
options?: NormalizerOptions, | ||
): NativeTestInstance[] | ||
export declare function queryByProp( | ||
attribute: string, | ||
container: ReactTestRenderer, | ||
match: TextMatch, | ||
options?: NormalizerOptions, | ||
): NativeTestInstance | null | ||
export function removeBadProperties(node: ReactTestInstance): NativeTestInstance | ||
|
||
// WAIT | ||
// ---- | ||
|
||
export interface WaitOptions { | ||
timeout?: number | ||
interval?: number | ||
} | ||
export declare function wait(callback?: () => void, options?: WaitOptions): Promise<void> | ||
|
||
// WAIT FOR ELEMENT | ||
// ---------------- | ||
|
||
export interface WaitOptions { | ||
timeout?: number | ||
interval?: number | ||
} | ||
export declare function waitForElement<T>(callback: () => T, options?: WaitOptions): Promise<T> | ||
|
||
// WAIT FOR ELEMENT TO BE REMOVED | ||
// ------------------------------ | ||
|
||
export interface WaitOptions { | ||
timeout?: number | ||
interval?: number | ||
} | ||
export declare function waitForElementToBeRemoved(callback: () => any, options?: WaitOptions): Promise<null> | ||
|
||
// MATCHES | ||
// ------- | ||
|
||
export interface DefaultNormalizerOptions { | ||
trim?: boolean, | ||
collapseWhitespace?: boolean, | ||
} | ||
export declare function getDefaultNormalizer(options: DefaultNormalizerOptions): NormalizerFn | ||
|
||
// INDEX | ||
// ----- | ||
|
||
export interface RenderOptions { | ||
wrapper?: ComponentType<{ children: ReactElement }> | ||
} | ||
export interface RenderOptionsWithQueries<T> extends RenderOptions { | ||
queries?: T | ||
} | ||
|
||
export declare function render(ui: ReactElement, options?: RenderOptions): RenderResult & BoundQueries<Queries> | ||
export declare function render<T>(ui: ReactElement, options: RenderOptionsWithQueries<T>): RenderResult & BoundQueries<T> | ||
|
||
export interface RenderResult { | ||
container: ReactTestRenderer | ||
baseElement: NativeTestInstance | ||
debug: () => void | ||
rerender: (ui: ReactElement) => void | ||
unmount: () => void | ||
} | ||
|
||
export interface RenderHookOptions<T> extends RenderOptions { | ||
initialProps?: T | ||
} | ||
|
||
export declare function renderHook<T, U>(callback: (props: T) => U, options?: RenderHookOptions<T>): RenderHookResult<T, U> | ||
|
||
export interface RenderHookResult<T, U> { | ||
result: { | ||
current: U | ||
} | ||
error?: Error | ||
waitForNextUpdate: () => Promise<void> | ||
rerender: (newProps?: T) => void | ||
unmount: () => void | ||
} | ||
|
||
export { act } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
waitForElement
also takecontainer
as an option.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This was actually removed, it no longer takes a container 👍
Unrelated to this PR, but the reason for this was that in
dom-testing-library
the container is used as the root of aMutationObserver
but we don't actually observe for mutation. It was an option in previous versions, but it didn't actually do anything. This is true for ^1.6.0.