Skip to content

Commit

Permalink
refactor: increase test coverage by restructuring
Browse files Browse the repository at this point in the history
  • Loading branch information
angeloashmore committed Aug 3, 2021
1 parent 5d3cce5 commit 84c5cde
Showing 1 changed file with 27 additions and 10 deletions.
37 changes: 27 additions & 10 deletions src/useStatefulPrismicClientMethod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,23 @@ const reducer = <TData>(
state: StateMachineState<TData>,
action: StateMachineAction<TData>,
): StateMachineState<TData> => {
if (action[0] === "start") {
return { state: PrismicClientHookState.PENDING };
} else if (action[0] === "succeed") {
return { state: PrismicClientHookState.SUCCEEDED, data: action[1] };
} else if (action[0] === "fail") {
return { ...state, state: PrismicClientHookState.FAILED, error: action[1] };
switch (action[0]) {
case "start": {
return { state: PrismicClientHookState.PENDING };
}

case "succeed": {
return { state: PrismicClientHookState.SUCCEEDED, data: action[1] };
}

case "fail": {
return {
...state,
state: PrismicClientHookState.FAILED,
error: action[1],
};
}
}

throw new Error(`Invalid action type: ${action[0]}`);
};

const initialState = { state: PrismicClientHookState.IDLE } as const;
Expand All @@ -53,7 +61,7 @@ export type HookOnlyParameters = {
};

const getParamHookDependencies = (
params: ClientMethodParameters<"get">[0] = {},
params: NonNullable<ClientMethodParameters<"get">[0]>,
) => {
return [
params.ref,
Expand Down Expand Up @@ -144,7 +152,16 @@ export const useStatefulPrismicClientMethod = <
},
// We must disable exhaustive-deps to optimize providing `params` deps.
// eslint-disable-next-line react-hooks/exhaustive-deps
[client, ...args.slice(0, -1), ...getParamHookDependencies(params)],
[
client,
state.state,
skip,
method,
// eslint-disable-next-line react-hooks/exhaustive-deps
...args.slice(0, -1),
// eslint-disable-next-line react-hooks/exhaustive-deps
...getParamHookDependencies(params),
],
);

return React.useMemo(
Expand Down

0 comments on commit 84c5cde

Please sign in to comment.