Skip to content

Commit

Permalink
fix: failing typecheck (#408)
Browse files Browse the repository at this point in the history
  • Loading branch information
jshawl committed Nov 27, 2023
1 parent 21bf270 commit df8f63b
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 14 deletions.
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
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
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
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
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

0 comments on commit df8f63b

Please sign in to comment.