Skip to content

Commit

Permalink
feat: add loadingStatusErrorMessage to state (#366)
Browse files Browse the repository at this point in the history
  • Loading branch information
dtjones404 committed Jul 3, 2023
1 parent eac53c8 commit e1b9068
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 2 deletions.
7 changes: 6 additions & 1 deletion src/components/PayPalScriptProvider.test.tsx
Expand Up @@ -56,13 +56,14 @@ describe("<PayPalScriptProvider />", () => {
await waitFor(() => expect(state.isResolved).toBeTruthy());
expect(state.isPending).toBeFalsy();
expect(state.isRejected).toBeFalsy();
expect(state.loadingStatusErrorMessage).toBeFalsy();
});

test('should set "isRejected" state to "true" after failing to load the script', async () => {
const spyConsoleError = jest
.spyOn(console, "error")
.mockImplementation();
(loadScript as jest.Mock).mockRejectedValue(new Error());
(loadScript as jest.Mock).mockRejectedValue(new Error("test error"));
const { state, TestComponent } = setupTestComponent();
render(
<PayPalScriptProvider options={{ clientId: "test" }}>
Expand All @@ -79,6 +80,7 @@ describe("<PayPalScriptProvider />", () => {
// verify initial loading state
expect(state.isPending).toBeTruthy();
await waitFor(() => expect(state.isRejected).toBeTruthy());
expect(state.loadingStatusErrorMessage).toBe("Error: test error");
expect(state.isPending).toBeFalsy();
expect(state.isResolved).toBeFalsy();
spyConsoleError.mockRestore();
Expand All @@ -99,6 +101,7 @@ describe("<PayPalScriptProvider />", () => {
unmount();

await waitFor(() => expect(loadScript).toBeCalled());
expect(state.loadingStatusErrorMessage).toBeFalsy();
// verify initial loading state
expect(state.isInitial).toBeFalsy();
expect(state.isPending).toBeTruthy();
Expand Down Expand Up @@ -142,6 +145,7 @@ describe("<PayPalScriptProvider />", () => {

expect(state.isPending).toBe(true);
await waitFor(() => expect(state.isResolved).toBe(true));
expect(state.loadingStatusErrorMessage).toBeFalsy();
});

test("should remount without reloading the sdk script when the options have not changed", async () => {
Expand Down Expand Up @@ -251,6 +255,7 @@ describe("usePayPalScriptReducer", () => {

function setupTestComponent() {
const state = {
loadingStatusErrorMessage: "",
options: { "data-react-paypal-script-id": "" },
isInitial: true,
isPending: false,
Expand Down
5 changes: 4 additions & 1 deletion src/components/PayPalScriptProvider.tsx
Expand Up @@ -66,7 +66,10 @@ export const PayPalScriptProvider: FC<ScriptProviderProps> = ({
if (isSubscribed) {
dispatch({
type: DISPATCH_ACTION.LOADING_STATUS,
value: SCRIPT_LOADING_STATE.REJECTED,
value: {
state: SCRIPT_LOADING_STATE.REJECTED,
message: String(err),
},
});
}
});
Expand Down
8 changes: 8 additions & 0 deletions src/context/scriptProviderContext.ts
Expand Up @@ -51,6 +51,14 @@ export function scriptReducer(
): ScriptContextState {
switch (action.type) {
case DISPATCH_ACTION.LOADING_STATUS:
if (typeof action.value === "object") {
return {
...state,
loadingStatus: action.value.state as SCRIPT_LOADING_STATE,
loadingStatusErrorMessage: action.value.message,
};
}

return {
...state,
loadingStatus: action.value as SCRIPT_LOADING_STATE,
Expand Down
5 changes: 5 additions & 0 deletions src/types/scriptProviderTypes.ts
Expand Up @@ -13,6 +13,10 @@ export type ScriptReducerAction =
type: `${DISPATCH_ACTION.LOADING_STATUS}`;
value: SCRIPT_LOADING_STATE;
}
| {
type: `${DISPATCH_ACTION.LOADING_STATUS}`;
value: { state: SCRIPT_LOADING_STATE; message: string };
}
| {
type: `${DISPATCH_ACTION.RESET_OPTIONS}`;
value: ReactPayPalScriptOptions;
Expand All @@ -30,6 +34,7 @@ export type InitialState = {
export interface ScriptContextState {
options: ReactPayPalScriptOptions;
loadingStatus: SCRIPT_LOADING_STATE;
loadingStatusErrorMessage?: string;
braintreePayPalCheckoutInstance?: BraintreePayPalCheckout;
dispatch?: Dispatch<ScriptReducerAction>;
}
Expand Down

0 comments on commit e1b9068

Please sign in to comment.