mirror of
https://github.com/gosticks/DefinitelyTyped.git
synced 2025-10-16 12:05:41 +00:00
Add types for @testing-library/react-hooks (#38715)
This commit is contained in:
parent
44b2e588df
commit
115ed66c06
27
types/testing-library__react-hooks/index.d.ts
vendored
Normal file
27
types/testing-library__react-hooks/index.d.ts
vendored
Normal 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>;
|
||||
@ -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());
|
||||
}
|
||||
20
types/testing-library__react-hooks/tsconfig.json
Normal file
20
types/testing-library__react-hooks/tsconfig.json
Normal 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"]
|
||||
}
|
||||
3
types/testing-library__react-hooks/tslint.json
Normal file
3
types/testing-library__react-hooks/tslint.json
Normal file
@ -0,0 +1,3 @@
|
||||
{
|
||||
"extends": "dtslint/dt.json"
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user