From c22ae09f380aadcc38ca6c79083e3df63a6f885f Mon Sep 17 00:00:00 2001 From: Polo Li <65737086+pololi-stripe@users.noreply.github.com> Date: Wed, 9 Oct 2024 16:14:23 -0700 Subject: [PATCH] Add onLineItemsChange option to EmbeddedCheckoutProvider (#536) --- package.json | 2 +- .../EmbeddedCheckoutProvider.test.tsx | 31 +++++++++++++++++++ src/components/EmbeddedCheckoutProvider.tsx | 12 +++++++ yarn.lock | 8 ++--- 4 files changed, 48 insertions(+), 5 deletions(-) diff --git a/package.json b/package.json index 2adc9fc..2ef44aa 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/components/EmbeddedCheckoutProvider.test.tsx b/src/components/EmbeddedCheckoutProvider.test.tsx index 1001719..ac4de1f 100644 --- a/src/components/EmbeddedCheckoutProvider.test.tsx +++ b/src/components/EmbeddedCheckoutProvider.test.tsx @@ -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( + + ); + await act(() => mockEmbeddedCheckoutPromise); + + rerender( + + ); + 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(
diff --git a/src/components/EmbeddedCheckoutProvider.tsx b/src/components/EmbeddedCheckoutProvider.tsx index c33137b..2a25030 100644 --- a/src/components/EmbeddedCheckoutProvider.tsx +++ b/src/components/EmbeddedCheckoutProvider.tsx @@ -57,6 +57,9 @@ interface EmbeddedCheckoutProviderProps { onShippingDetailsChange?: ( event: stripeJs.StripeEmbeddedCheckoutShippingDetailsChangeEvent ) => Promise; + onLineItemsChange?: ( + event: stripeJs.StripeEmbeddedCheckoutLineItemsChangeEvent + ) => Promise; }; } @@ -220,6 +223,15 @@ export const EmbeddedCheckoutProvider: FunctionComponent