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

Add onLineItemsChange option to EmbeddedCheckoutProvider #536

Merged
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
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@
"@rollup/plugin-replace": "^5.0.5",
"@rollup/plugin-terser": "^0.4.4",
"@storybook/react": "^6.5.0-beta.8",
"@stripe/stripe-js": "^4.3.0",
"@stripe/stripe-js": "^4.8.0",
"@testing-library/jest-dom": "^5.16.4",
"@testing-library/react": "^13.1.1",
"@testing-library/react-hooks": "^8.0.0",
Expand Down
31 changes: 31 additions & 0 deletions src/components/EmbeddedCheckoutProvider.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -414,6 +414,37 @@ describe('EmbeddedCheckoutProvider', () => {
);
});

it('does not allow changes to onLineItemsChange option', async () => {
const optionsProp1 = {
fetchClientSecret,
onLineItemsChange: () => Promise.resolve({type: 'accept' as const}),
};
const optionsProp2 = {
fetchClientSecret,
onLineItemsChange: () => Promise.resolve({type: 'reject' as const}),
};

// Silence console output so test output is less noisy
consoleWarn.mockImplementation(() => {});
const {rerender} = render(
<EmbeddedCheckoutProvider
stripe={mockStripe}
options={optionsProp1}
></EmbeddedCheckoutProvider>
);
await act(() => mockEmbeddedCheckoutPromise);

rerender(
<EmbeddedCheckoutProvider
stripe={mockStripe}
options={optionsProp2}
></EmbeddedCheckoutProvider>
);
expect(consoleWarn).toHaveBeenCalledWith(
'Unsupported prop change on EmbeddedCheckoutProvider: You cannot change the onLineItemsChange option after setting it.'
);
});

it('destroys Embedded Checkout when the component unmounts', async () => {
const {rerender} = render(
<div>
Expand Down
12 changes: 12 additions & 0 deletions src/components/EmbeddedCheckoutProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@ interface EmbeddedCheckoutProviderProps {
onShippingDetailsChange?: (
event: stripeJs.StripeEmbeddedCheckoutShippingDetailsChangeEvent
) => Promise<stripeJs.ResultAction>;
onLineItemsChange?: (
event: stripeJs.StripeEmbeddedCheckoutLineItemsChangeEvent
) => Promise<stripeJs.ResultAction>;
};
}

Expand Down Expand Up @@ -220,6 +223,15 @@ export const EmbeddedCheckoutProvider: FunctionComponent<PropsWithChildren<
'Unsupported prop change on EmbeddedCheckoutProvider: You cannot change the onShippingDetailsChange option after setting it.'
);
}

if (
prevOptions.onLineItemsChange != null &&
options.onLineItemsChange !== prevOptions.onLineItemsChange
) {
console.warn(
'Unsupported prop change on EmbeddedCheckoutProvider: You cannot change the onLineItemsChange option after setting it.'
);
}
}, [prevOptions, options]);

return (
Expand Down
8 changes: 4 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2350,10 +2350,10 @@
regenerator-runtime "^0.13.7"
resolve-from "^5.0.0"

"@stripe/stripe-js@^4.3.0":
version "4.3.0"
resolved "https://registry.yarnpkg.com/@stripe/stripe-js/-/stripe-js-4.3.0.tgz#5e8fe1e654413698f33cbc7008fef1e86e16b030"
integrity sha512-bf8MxzzgD3dybtyIJUQSDMqxjEkJfsmj9IdRqDv609Zw08R41O7eoIy0f8KY41u8MbaFOYsn+XGJZtg1xwR2wQ==
"@stripe/stripe-js@^4.8.0":
version "4.8.0"
resolved "https://registry.yarnpkg.com/@stripe/stripe-js/-/stripe-js-4.8.0.tgz#ba78f775347cb30e93e13aa408b345de15e3ff80"
integrity sha512-+4Cb0bVHlV4BJXxkJ3cCLSLuWxm3pXKtgcRacox146EuugjCzRRII5T5gUMgL4HpzrBLVwVxjKaZqntNWAXawQ==

"@testing-library/dom@^8.5.0":
version "8.13.0"
Expand Down
Loading