Skip to content

Commit

Permalink
feat: allows button message amount type to be string (#2421)
Browse files Browse the repository at this point in the history
  • Loading branch information
danzhaaspaypal authored Aug 16, 2024
1 parent 46f5c51 commit f1d9331
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 4 deletions.
10 changes: 7 additions & 3 deletions src/ui/buttons/props.js
Original file line number Diff line number Diff line change
Expand Up @@ -440,7 +440,7 @@ export type ButtonMessage = {|
|};

export type ButtonMessageInputs = {|
amount?: number | void,
amount?: number | string | void,
offer?: $ReadOnlyArray<$Values<typeof MESSAGE_OFFER>> | void,
color?: $Values<typeof MESSAGE_COLOR> | void,
position?: $Values<typeof MESSAGE_POSITION> | void,
Expand Down Expand Up @@ -779,14 +779,18 @@ export function normalizeButtonMessage(
fundingSources: $ReadOnlyArray<$Values<typeof FUNDING>>
): ButtonMessage {
const {
amount,
color = MESSAGE_COLOR.BLACK,
position,
align = MESSAGE_ALIGN.CENTER,
} = message;
let offer = message.offer;
let amount = message.amount;

if (typeof amount !== "undefined") {
if (typeof amount !== "number") {
if (typeof amount === "string") {
amount = Number(amount);
}
if (typeof amount !== "number" || isNaN(amount)) {
throw new TypeError(
`Expected message.amount to be a number, got: ${amount}`
);
Expand Down
20 changes: 20 additions & 0 deletions test/integration/tests/button/message.js
Original file line number Diff line number Diff line change
Expand Up @@ -594,4 +594,24 @@ describe(`paypal button message`, () => {
.render("#testContainer");
});
});

describe("property normalization", () => {
it("should convert string type amount to number", () => {
return wrapPromise(({ expect }) => {
window.paypal
.Buttons({
message: { amount: "100" },
test: {
onRender: expect("onRender", ({ xprops }) => {
const {
message: { amount },
} = xprops;
assert.equal(amount, 100);
}),
},
})
.render("#testContainer");
});
});
});
});
13 changes: 12 additions & 1 deletion test/integration/tests/button/validation.js
Original file line number Diff line number Diff line change
Expand Up @@ -1004,7 +1004,18 @@ const buttonConfigs = [

{
message: {
amount: "100", // invalid: should be num
amount: "100", // valid: is converted to num
offer: ["pay_later_long_term"],
color: "black",
position: "top",
align: "left",
},
valid: true,
},

{
message: {
amount: "one hundred", // invalid: string not convertible to number
offer: ["pay_later_long_term"],
color: "black",
position: "top",
Expand Down

0 comments on commit f1d9331

Please sign in to comment.