Add types for @testing-library/react-hooks (#38715)

This commit is contained in:
Michael Peyper 2019-10-03 06:53:38 +10:00 committed by Ryan Cavanaugh
parent 44b2e588df
commit 115ed66c06
4 changed files with 121 additions and 0 deletions

View File

@ -0,0 +1,27 @@
// Type definitions for @testing-library/react-hooks 2.0
// Project: https://github.com/testing-library/react-hooks-testing-library
// Definitions by: Michael Peyper <https://github.com/mpeyper>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
// TypeScript Version: 3.0
import { ComponentType } from 'react';
export { act } from 'react-test-renderer';
export interface RenderHookOptions<P> {
initialProps?: P;
wrapper?: React.ComponentType;
}
export interface HookResult<R> {
readonly current: R;
readonly error: Error;
}
export interface RenderHookResult<P, R> {
readonly result: HookResult<R>;
readonly waitForNextUpdate: () => Promise<void>;
readonly unmount: () => boolean;
readonly rerender: (newProps?: P) => void;
}
export function renderHook<P, R>(callback: (props: P) => R, options?: RenderHookOptions<P>): RenderHookResult<P, R>;

View File

@ -0,0 +1,71 @@
import { useState } from 'react';
import { renderHook, act } from '@testing-library/react-hooks';
const useHook = (initialValue: number) => {
const [value, setValue] = useState(initialValue);
return { value, setValue };
};
function checkTypesWithNoInitialProps() {
const { result, unmount, rerender } = renderHook(() => useHook(0));
// check types
const _result: {
current: {
value: number;
setValue: (_: number) => void;
};
} = result;
const _unmount: () => boolean = unmount;
const _rerender: () => void = rerender;
}
function checkTypesWithInitialProps() {
const { result, unmount, rerender } = renderHook(({ value }) => useHook(value), {
initialProps: { value: 10 },
});
// check types
const _result: {
current: {
value: number;
setValue: (_: number) => void;
};
} = result;
const _unmount: () => boolean = unmount;
const _rerender: (_?: { value: number }) => void = rerender;
}
function checkTypesWhenHookReturnsUndefined() {
renderHook(() => {});
}
function checkTypesWithError() {
const { result } = renderHook(() => useHook(0));
// check types
const _result: {
error: Error;
} = result;
}
async function checkTypesForWaitForNextUpdate() {
const { waitForNextUpdate } = renderHook(() => {});
await waitForNextUpdate();
// check type
const _waitForNextUpdate: () => Promise<void> = waitForNextUpdate;
}
function checkTypesWithUndefinedResult() {
act(() => undefined);
}
function checkTypesWithVoidResult() {
act(() => {});
}
function checkTypesWithPromiseResult() {
act(() => Promise.resolve());
}

View File

@ -0,0 +1,20 @@
{
"compilerOptions": {
"module": "commonjs",
"lib": ["es6", "dom"],
"noImplicitAny": true,
"noImplicitThis": true,
"strictNullChecks": true,
"strictFunctionTypes": true,
"baseUrl": "../",
"typeRoots": ["../"],
"target": "es6",
"types": [],
"noEmit": true,
"forceConsistentCasingInFileNames": true,
"paths": {
"@testing-library/react-hooks": ["testing-library__react-hooks"]
}
},
"files": ["index.d.ts", "testing-library__react-hooks-tests.ts"]
}

View File

@ -0,0 +1,3 @@
{
"extends": "dtslint/dt.json"
}