From 115ed66c064a6845ff3817857cdc1bef299d4a86 Mon Sep 17 00:00:00 2001 From: Michael Peyper Date: Thu, 3 Oct 2019 06:53:38 +1000 Subject: [PATCH] Add types for @testing-library/react-hooks (#38715) --- types/testing-library__react-hooks/index.d.ts | 27 +++++++ .../testing-library__react-hooks-tests.ts | 71 +++++++++++++++++++ .../tsconfig.json | 20 ++++++ .../testing-library__react-hooks/tslint.json | 3 + 4 files changed, 121 insertions(+) create mode 100644 types/testing-library__react-hooks/index.d.ts create mode 100644 types/testing-library__react-hooks/testing-library__react-hooks-tests.ts create mode 100644 types/testing-library__react-hooks/tsconfig.json create mode 100644 types/testing-library__react-hooks/tslint.json diff --git a/types/testing-library__react-hooks/index.d.ts b/types/testing-library__react-hooks/index.d.ts new file mode 100644 index 0000000000..092dd12951 --- /dev/null +++ b/types/testing-library__react-hooks/index.d.ts @@ -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 +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped +// TypeScript Version: 3.0 + +import { ComponentType } from 'react'; +export { act } from 'react-test-renderer'; + +export interface RenderHookOptions

{ + initialProps?: P; + wrapper?: React.ComponentType; +} + +export interface HookResult { + readonly current: R; + readonly error: Error; +} + +export interface RenderHookResult { + readonly result: HookResult; + readonly waitForNextUpdate: () => Promise; + readonly unmount: () => boolean; + readonly rerender: (newProps?: P) => void; +} + +export function renderHook(callback: (props: P) => R, options?: RenderHookOptions

): RenderHookResult; diff --git a/types/testing-library__react-hooks/testing-library__react-hooks-tests.ts b/types/testing-library__react-hooks/testing-library__react-hooks-tests.ts new file mode 100644 index 0000000000..3e990ad04a --- /dev/null +++ b/types/testing-library__react-hooks/testing-library__react-hooks-tests.ts @@ -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 = waitForNextUpdate; +} + +function checkTypesWithUndefinedResult() { + act(() => undefined); +} + +function checkTypesWithVoidResult() { + act(() => {}); +} + +function checkTypesWithPromiseResult() { + act(() => Promise.resolve()); +} diff --git a/types/testing-library__react-hooks/tsconfig.json b/types/testing-library__react-hooks/tsconfig.json new file mode 100644 index 0000000000..7242e54432 --- /dev/null +++ b/types/testing-library__react-hooks/tsconfig.json @@ -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"] +} diff --git a/types/testing-library__react-hooks/tslint.json b/types/testing-library__react-hooks/tslint.json new file mode 100644 index 0000000000..f93cf8562a --- /dev/null +++ b/types/testing-library__react-hooks/tslint.json @@ -0,0 +1,3 @@ +{ + "extends": "dtslint/dt.json" +}