Skip to content

Commit

Permalink
fix: wrap all dispatches in 'act' so fetches behave
Browse files Browse the repository at this point in the history
  • Loading branch information
ntucker committed Jan 27, 2020
1 parent 379578b commit 1487cdd
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 2 deletions.
24 changes: 24 additions & 0 deletions packages/test/src/ActDispatchManager.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { Manager, MiddlewareAPI, Middleware, Dispatch } from 'rest-hooks';
import { act } from 'react-test-renderer';

export default class ActDispatchManager implements Manager {
protected declare middleware: Middleware;

constructor() {
this.middleware = <R extends React.Reducer<any, any>>({
dispatch,
}: MiddlewareAPI<R>) => {
return (next: Dispatch<R>) => (
action: React.ReducerAction<R>,
): Promise<void> => {
return act(() => next(action));
};
};
}

cleanup() {}

getMiddleware<T extends ActDispatchManager>(this: T) {
return this.middleware;
}
}
5 changes: 3 additions & 2 deletions packages/test/src/makeRenderRestHook.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import React from 'react';
import { renderHook, RenderHookOptions } from '@testing-library/react-hooks';

import {
State,
SubscriptionManager,
PollingSubscription,
Manager,
} from 'rest-hooks';

import ActDispatchManager from './ActDispatchManager';
import { MockNetworkManager } from './managers';
import mockInitialState, { Fixture } from './mockState';

Expand All @@ -19,6 +19,7 @@ export default function makeRenderRestHook(
) {
const manager = new MockNetworkManager();
const subManager = new SubscriptionManager(PollingSubscription);
const actManager = new ActDispatchManager();
function renderRestHook<P, R>(
callback: (props: P) => R,
options?: {
Expand All @@ -29,7 +30,7 @@ export default function makeRenderRestHook(
const initialState =
options && options.results && mockInitialState(options.results);
const Provider: React.ComponentType<any> = makeProvider(
[manager, subManager],
[manager, subManager, actManager],
initialState,
);
const Wrapper = options && options.wrapper;
Expand Down

0 comments on commit 1487cdd

Please sign in to comment.