-
-
Notifications
You must be signed in to change notification settings - Fork 98
Expand file tree
/
Copy pathindex.tsx
More file actions
165 lines (156 loc) · 4.81 KB
/
index.tsx
File metadata and controls
165 lines (156 loc) · 4.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
import {
State,
Manager,
SubscriptionManager,
NetworkManager,
PollingSubscription,
Controller,
GenericDispatch,
DataClientDispatch,
} from '@data-client/react';
import React, { memo, Suspense } from 'react';
import {
renderHook,
act,
RenderHookResult,
type RenderHookOptions,
} from './renderHook.cjs';
import { Interceptor, Fixture } from '../fixtureTypes.js';
import { MockController } from '../MockController.js';
import mockInitialState from '../mockState.js';
import { MockProps } from '../mockTypes.js';
/** @see https://dataclient.io/docs/api/makeRenderDataHook */
export default function makeRenderDataHook(
Provider: React.ComponentType<DataProviderProps>,
) {
const renderDataClient: RenderDataHook = (<P, R, T = any>(
callback: (props: P) => R,
{
initialFixtures,
resolverFixtures,
getInitialInterceptorData = () => ({}) as any,
...options
}: {
initialProps?: P;
initialFixtures?: Fixture[];
resolverFixtures?: (Fixture | Interceptor<T>)[];
getInitialInterceptorData?: () => T;
wrapper?: React.ComponentType<React.PropsWithChildren<P>>;
} & Omit<RenderHookOptions<P>, 'initialProps' | 'wrapper'> = {} as any,
): RenderHookResult<R, P> & { controller: Controller } => {
/** Wraps dispatches that are typically called declaratively in act() */
class ActController<
D extends GenericDispatch = DataClientDispatch,
T = {},
> extends MockController(
Controller,
resolverFixtures ?
{
fixtures: resolverFixtures,
getInitialInterceptorData,
}
: {},
)<D> {
constructor(
options: MockProps<T> & ConstructorParameters<typeof Controller<D>>[0],
) {
super(options);
const { setResponse, resolve } = this;
this.setResponse = (...args) => {
let promise: any;
act(() => {
promise = setResponse.call(this, ...args);
});
return promise;
};
this.resolve = (...args) => {
let promise: any;
act(() => {
promise = resolve.call(this, ...args);
});
return promise;
};
}
}
// we want fresh manager state in each instance
const nm = new NetworkManager();
const sm = new SubscriptionManager(PollingSubscription);
const managers = [nm, sm];
// this pattern is dangerous if renderDataClient is shared between tests
// TODO: move to return value
renderDataClient.cleanup = () => {
nm.cleanupDate = Infinity;
nm['fetching'].forEach(({ reject }) => reject());
nm['clearAll']();
managers.forEach(manager => manager.cleanup());
};
renderDataClient.allSettled = () => {
return nm.allSettled();
};
const initialState: State<unknown> = mockInitialState(initialFixtures);
const ProviderWithResolver: React.ComponentType<any> = memo(
function ProviderWithResolver({ children }: React.PropsWithChildren<P>) {
return (
<Provider
initialState={initialState}
Controller={ActController}
managers={managers}
devButton={null}
>
{children}
</Provider>
);
},
);
const Wrapper: React.ComponentType<React.PropsWithChildren<P>> | undefined =
options?.wrapper;
const ProviderWithWrapper =
Wrapper ?
function ProviderWrapped(props: React.PropsWithChildren<P>) {
return (
<ProviderWithResolver>
<Wrapper {...props} />
</ProviderWithResolver>
);
}
: ProviderWithResolver;
const wrapper: React.ComponentType<any> = ({
children,
...props
}: React.PropsWithChildren<P>) => (
<ProviderWithWrapper {...(props as any)}>
<Suspense fallback={null}>{children}</Suspense>
</ProviderWithWrapper>
);
const ret: any = renderHook(callback, {
...options,
wrapper,
});
ret.controller = nm['controller'];
return ret;
}) as any;
renderDataClient.cleanup = () => {};
renderDataClient.allSettled = () => Promise.allSettled([]);
return renderDataClient;
}
export interface DataProviderProps {
children: React.ReactNode;
managers: Manager[];
initialState: State<unknown>;
Controller: typeof Controller<any>;
devButton: any;
}
export type RenderDataHook = (<P, R>(
callback: (props: P) => R,
options?: {
initialProps?: P;
initialFixtures?: readonly Fixture[];
readonly resolverFixtures?: readonly (Fixture | Interceptor)[];
wrapper?: React.ComponentType<React.PropsWithChildren<P>>;
} & Omit<RenderHookOptions<P>, 'initialProps' | 'wrapper'>,
) => RenderHookResult<R, P> & {
controller: Controller;
}) & {
cleanup: () => void;
allSettled: () => Promise<PromiseSettledResult<unknown>[]> | undefined;
};