Skip to content

Commit

Permalink
react: Update types for changes declared in the CHANGELOG
Browse files Browse the repository at this point in the history
  • Loading branch information
Jessidhia committed Aug 7, 2019
1 parent c15bcd2 commit b7ba850
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 17 deletions.
23 changes: 20 additions & 3 deletions types/react-test-renderer/index.d.ts
@@ -1,4 +1,4 @@
// Type definitions for react-test-renderer 16.8
// Type definitions for react-test-renderer 16.9
// Project: https://facebook.github.io/react/
// Definitions by: Arvitaly <https://github.com/arvitaly>
// Lochbrunner <https://github.com/lochbrunner>
Expand Down Expand Up @@ -53,6 +53,20 @@ export interface TestRendererOptions {
}
export function create(nextElement: ReactElement, options?: TestRendererOptions): ReactTestRenderer;

/**
* Wrap any code rendering and triggering updates to your components into `act()` calls.
*
* Ensures that the behavior in your tests matches what happens in the browser
* more closely by executing pending `useEffect`s before returning. This also
* reduces the amount of re-renders done.
*
* @param callback An asynchronous, void callback that will execute as a single, complete React commit.
*
* @see https://reactjs.org/blog/2019/02/06/react-v16.8.0.html#testing-hooks
*/
// the "void | undefined" is here to forbid any sneaky return values
// tslint:disable-next-line: void-return
export function act(callback: () => Promise<void | undefined>): Promise<undefined>;
/**
* Wrap any code rendering and triggering updates to your components into `act()` calls.
*
Expand All @@ -71,6 +85,9 @@ export function act(callback: () => void | undefined): DebugPromiseLike;
// Intentionally doesn't extend PromiseLike<never>.
// Ideally this should be as hard to accidentally use as possible.
export interface DebugPromiseLike {
// the actual then() in here is 0-ary, but that doesn't count as a PromiseLike.
then(onfulfilled: (value: never) => never, onrejected: (reason: never) => never): never;
// the actual then() in here is 1-ary, but that doesn't count as a PromiseLike.
then(
onfulfilled: (value: never) => never,
onrejected: (reason: never) => never,
): never;
}
15 changes: 13 additions & 2 deletions types/react-test-renderer/react-test-renderer-tests.ts
Expand Up @@ -70,8 +70,19 @@ shallowRenderer.getMountedInstance();
// Only synchronous, void callbacks are acceptable for act()
act(() => {});
// $ExpectError
act(async () => {});
// $ExpectError
act(() => null);
// $ExpectError
Promise.resolve(act(() => {}));

// async act is now acceptable in React 16.9,
// but the result must be void or undefined
Promise.resolve(act(async () => {}));

void (async () => {
act(() => {});

await act(async () => {});
await act(async () => undefined);
// $ExpectError
await act(async () => null);
})();
12 changes: 3 additions & 9 deletions types/react/index.d.ts
@@ -1,4 +1,4 @@
// Type definitions for React 16.8
// Type definitions for React 16.9
// Project: http://facebook.github.io/react/
// Definitions by: Asana <https://asana.com>
// AssureSign <http://www.assuresign.com>
Expand Down Expand Up @@ -349,13 +349,6 @@ declare namespace React {

/** A fallback react tree to show when a Suspense child (like React.lazy) suspends */
fallback: NonNullable<ReactNode>|null;

// I tried looking at the code but I have no idea what it does.
// https://github.com/facebook/react/issues/13206#issuecomment-432489986
/**
* Not implemented yet, requires unstable_ConcurrentMode
*/
// maxDuration?: number;
}
/**
* This feature is not yet available for server-side rendering.
Expand All @@ -382,7 +375,7 @@ declare namespace React {
onRender: ProfilerOnRenderCallback;
}

const unstable_Profiler: ExoticComponent<ProfilerProps>;
const Profiler: ExoticComponent<ProfilerProps>;

//
// Component API
Expand Down Expand Up @@ -2199,6 +2192,7 @@ declare namespace React {
playsInline?: boolean;
poster?: string;
width?: number | string;
disablePictureInPicture?: boolean;
}

// this list is "complete" in that it contains every SVG attribute
Expand Down
12 changes: 9 additions & 3 deletions types/react/test/tsx.tsx
Expand Up @@ -321,7 +321,7 @@ const ForwardRef3 = React.forwardRef(
<ForwardRef3 ref={divFnRef}/>;
<ForwardRef3 ref={divRef}/>;

const Profiler = React.unstable_Profiler;
const { Profiler } = React;

// 'id' is missing
<Profiler />; // $ExpectError
Expand Down Expand Up @@ -357,8 +357,14 @@ const Profiler = React.unstable_Profiler;
</Profiler>;

type ImgProps = React.ComponentProps<'img'>;
// $ExpectType "async" | "auto" | "sync" | undefined
type ImgPropsDecoding = ImgProps['decoding'];
const imgProps: ImgProps = {};
// the order of the strings in the union seems to vary
// with the typescript version, so test assignment instead
imgProps.decoding = 'async';
imgProps.decoding = 'auto';
imgProps.decoding = 'sync';
// $ExpectError
imgProps.decoding = 'nonsense';
type ImgPropsWithRef = React.ComponentPropsWithRef<'img'>;
// $ExpectType ((instance: HTMLImageElement | null) => void) | RefObject<HTMLImageElement> | null | undefined
type ImgPropsWithRefRef = ImgPropsWithRef['ref'];
Expand Down

0 comments on commit b7ba850

Please sign in to comment.