Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Do not throw button render errors when ref is invalid #88

Merged
merged 2 commits into from
Mar 11, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 26 additions & 16 deletions src/components/PayPalButtons.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
import React, { useState } from "react";
import { render, waitFor, screen, fireEvent } from "@testing-library/react";
import {
render,
waitFor,
screen,
fireEvent,
act,
} from "@testing-library/react";

import { PayPalScriptProvider } from "../ScriptContext";
import PayPalButtons from "./PayPalButtons";
Expand All @@ -11,13 +17,11 @@ jest.mock("@paypal/paypal-js", () => ({
}));

describe("<PayPalButtons />", () => {
let consoleErrorSpy;
beforeEach(() => {
window.paypal = {};
loadScript.mockResolvedValue(window.paypal);

consoleErrorSpy = jest.spyOn(console, "error");
console.error.mockImplementation(() => {});
const consoleErrorSpy = jest.spyOn(console, "error");
consoleErrorSpy.mockImplementation(() => {});
});
afterEach(() => {
jest.clearAllMocks();
Expand Down Expand Up @@ -98,14 +102,14 @@ describe("<PayPalButtons />", () => {
expect(window.paypal.Buttons).toHaveBeenCalled();
});

const onInitCallback = window.paypal.Buttons.mock.calls[0][0].onInit;

const onInitActions = {
enable: jest.fn(),
disable: jest.fn(),
};

onInitCallback({}, onInitActions);
act(() =>
window.paypal.Buttons.mock.calls[0][0].onInit({}, onInitActions)
);

await waitFor(() => {
expect(onInitCallbackMock).toHaveBeenCalled();
Expand Down Expand Up @@ -183,8 +187,10 @@ describe("<PayPalButtons />", () => {

expect(screen.getByTestId("orderID").innerHTML).toBe("1");

// call createOrder() to trigger a state change
window.paypal.Buttons.mock.calls[0][0].createOrder();
act(() =>
// call createOrder() to trigger a state change
window.paypal.Buttons.mock.calls[0][0].createOrder()
);

await waitFor(() =>
expect(screen.getByTestId("orderID").innerHTML).toBe("2")
Expand Down Expand Up @@ -246,17 +252,21 @@ describe("<PayPalButtons />", () => {
};
};

const onError = jest.fn();

const wrapper = ({ children }) => (
<ErrorBoundary onError={onError}>{children}</ErrorBoundary>
);

render(
<PayPalScriptProvider options={{ "client-id": "test" }}>
<PayPalButtons />
</PayPalScriptProvider>
</PayPalScriptProvider>,
{ wrapper }
);

await waitFor(() =>
expect(consoleErrorSpy).toHaveBeenCalledWith(
expect.stringMatching(/Window closed/)
)
);
await waitFor(() => expect(onError).toHaveBeenCalled());
expect(onError.mock.calls[0][0].message).toMatchSnapshot();
});
});

Expand Down
12 changes: 9 additions & 3 deletions src/components/PayPalButtons.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,15 @@ export default function PayPalButtons({
}

buttons.current.render(buttonsContainerRef.current).catch((err) => {
console.error(
`Failed to render <PayPalButtons /> component. ${err}`
);
// component failed to render, possibly because it was closed or destroyed.
if (buttonsContainerRef.current === null) {
// ref is no longer in the DOM, we can safely ignore the error
return;
}
// ref is still in the DOM
setErrorState(() => {
throw new Error(`Failed to render <PayPalButtons /> component. ${err}`);
});
});

return closeButtonsComponent;
Expand Down
2 changes: 2 additions & 0 deletions src/components/__snapshots__/PayPalButtons.test.js.snap
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`<PayPalButtons /> should catch and log zoid render errors 1`] = `"Failed to render <PayPalButtons /> component. Window closed"`;

exports[`<PayPalButtons /> should throw an error when no components are passed to the PayPalScriptProvider 1`] = `"Unable to render <PayPalButtons /> because window.paypal.Buttons is undefined."`;

exports[`<PayPalButtons /> should throw an error when the 'buttons' component is missing from the components list passed to the PayPalScriptProvider 1`] = `
Expand Down