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

Set data-js-sdk-library default value #465

Merged
merged 9 commits into from
Mar 19, 2024
5 changes: 5 additions & 0 deletions .changeset/brown-elephants-behave.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@paypal/paypal-js": patch
---

Set default data-js-sdk-library value
1 change: 1 addition & 0 deletions packages/paypal-js/e2e-tests/load-cached-script.html
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ <h1>Load Cached Script</h1>
const options = {
clientId: "test",
dataPageType: "checkout",
dataJsSdkLibrary: "paypal-js",
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This smells funky, because dataJsSdkLibrary is not a required script option, but if we don't add it here, the "load cached script" tests fail.

};

// initial load and render
Expand Down
14 changes: 14 additions & 0 deletions packages/paypal-js/src/load-script.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,20 @@ describe("loadCustomScript()", () => {
);
});

test("should set data-js-sdk-library to paypal-js when undefined", async () => {
const options = {
url: "https://www.example.com/index.js",
attributes: {},
};

await loadCustomScript(options);
expect(mockedInsertScriptElement).toHaveBeenCalledWith(
expect.objectContaining({
attributes: { "data-js-sdk-library": "paypal-js" },
})
);
});

test("should throw an error from invalid arguments", () => {
// @ts-expect-error ignore invalid arguments error
expect(() => loadCustomScript()).toThrow("Expected an options object.");
Expand Down
4 changes: 4 additions & 0 deletions packages/paypal-js/src/load-script.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,10 @@ export function loadCustomScript(

const { url, attributes } = options;

if (attributes && !attributes["data-js-sdk-library"]) {
Copy link
Contributor Author

@nbierdeman nbierdeman Mar 19, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

loadCustomScript will always have attributes when called by react-paypal-js/paypal-js -> loadScript, but checking for attributes here, since react-paypal-js calls loadCustomScript without attributes in getBraintreeNamespace here.

attributes["data-js-sdk-library"] = "paypal-js";
}

if (typeof url !== "string" || url.length === 0) {
throw new Error("Invalid url.");
}
Expand Down
Loading