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

Fix failing typecheck #408

Merged
merged 2 commits into from
Nov 27, 2023
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 7 additions & 7 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
},
"homepage": "https://paypal.github.io/react-paypal-js/",
"dependencies": {
"@paypal/paypal-js": "^7.0.0",
"@paypal/paypal-js": "^7.1.1",
"@paypal/sdk-constants": "^1.0.122"
},
"devDependencies": {
Expand Down
5 changes: 2 additions & 3 deletions src/types/payPalHostedFieldTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@ import type { CSSProperties, ReactNode } from "react";
import type { HostedFieldsHandler, Installments } from "@paypal/paypal-js";

export type PayPalHostedFieldsNamespace = {
components: string | undefined;
[DATA_NAMESPACE: string]: string | undefined;
};
components: string | string[] | undefined;
} & { [DATA_NAMESPACE: string]: string | undefined };

export type PayPalHostedFieldsRegistered = {
[key: string]: PayPalHostedFieldOptions;
Expand Down
26 changes: 26 additions & 0 deletions src/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { mock } from "jest-mock-extended";

import { SDK_SETTINGS } from "./constants";
import {
generateErrorMessage,
getPayPalWindowNamespace,
getBraintreeWindowNamespace,
hashStr,
Expand All @@ -10,6 +11,31 @@ import {
import type { PayPalNamespace } from "@paypal/paypal-js";
import type { BraintreeNamespace } from "./types";

describe("generateErrorMessage", () => {
const errorMessage =
"Unable to render <Example /> because window.customNamespace.Example is undefined.\nTo fix the issue, add 'example' to the list of components passed to the parent PayPalScriptProvider:\n`<PayPalScriptProvider options={{ components: 'hosted-fields,example'}}>`.";
test("sdkRequestedComponents as an array", () => {
expect(
generateErrorMessage({
reactComponentName: "Example",
sdkComponentKey: "example",
sdkRequestedComponents: ["hosted-fields"],
sdkDataNamespace: "customNamespace",
})
).toBe(errorMessage);
});
test("sdkRequestedComponents as a string", () => {
expect(
generateErrorMessage({
reactComponentName: "Example",
sdkComponentKey: "example",
sdkRequestedComponents: "hosted-fields",
sdkDataNamespace: "customNamespace",
})
).toBe(errorMessage);
});
});

describe("getPayPalWindowNamespace", () => {
const mockPayPalNamespace = mock<PayPalNamespace>();

Expand Down
10 changes: 7 additions & 3 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import type { BraintreeNamespace } from "./types";
type ErrorMessageParams = {
reactComponentName: string;
sdkComponentKey: string;
sdkRequestedComponents?: string;
sdkRequestedComponents?: string | string[];
sdkDataNamespace?: string;
};

Expand Down Expand Up @@ -79,8 +79,12 @@ export function generateErrorMessage({

// The JS SDK only loads the buttons component by default.
// All other components like messages and marks must be requested using the "components" query parameter
if (!sdkRequestedComponents.includes(sdkComponentKey)) {
const expectedComponents = [sdkRequestedComponents, sdkComponentKey]
const requestedComponents =
typeof sdkRequestedComponents === "string"
? sdkRequestedComponents
: sdkRequestedComponents.join(",");
if (!requestedComponents.includes(sdkComponentKey)) {
const expectedComponents = [requestedComponents, sdkComponentKey]
.filter(Boolean)
.join();

Expand Down