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

Update EmbeddedCheckoutProvider prop types #525

Merged
merged 1 commit into from
Aug 14, 2024
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.0.0",
"@stripe/stripe-js": "^4.3.0",
"@testing-library/jest-dom": "^5.16.4",
"@testing-library/react": "^13.1.1",
"@testing-library/react-hooks": "^8.0.0",
Expand Down
32 changes: 32 additions & 0 deletions src/components/EmbeddedCheckoutProvider.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -382,6 +382,38 @@ describe('EmbeddedCheckoutProvider', () => {
);
});

it('does not allow changes to onShippingDetailsChange option', async () => {
const optionsProp1 = {
fetchClientSecret,
onShippingDetailsChange: () => Promise.resolve({type: 'accept' as const}),
};
const optionsProp2 = {
fetchClientSecret,
onShippingDetailsChange: () => 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 onShippingDetailsChange 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 @@ -54,6 +54,9 @@ interface EmbeddedCheckoutProviderProps {
clientSecret?: string | null;
fetchClientSecret?: (() => Promise<string>) | null;
onComplete?: () => void;
onShippingDetailsChange?: (
event: stripeJs.StripeEmbeddedCheckoutShippingDetailsChangeEvent
) => Promise<stripeJs.ResultAction>;
};
}

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

if (
prevOptions.onShippingDetailsChange != null &&
options.onShippingDetailsChange !== prevOptions.onShippingDetailsChange
) {
console.warn(
'Unsupported prop change on EmbeddedCheckoutProvider: You cannot change the onShippingDetailsChange 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 @@ -2335,10 +2335,10 @@
regenerator-runtime "^0.13.7"
resolve-from "^5.0.0"

"@stripe/stripe-js@^4.0.0":
version "4.0.0"
resolved "https://registry.yarnpkg.com/@stripe/stripe-js/-/stripe-js-4.0.0.tgz#57f258cdd595bba0b8a4f5dc94fc58bfa0a324a6"
integrity sha512-R5zewuzTVPGn4dXkavbgDk8vSILkT5hRlzga10p6JzngR17qKi1fgc27kl58TmaVvgBZGngTRNH2j9kYdvfPGA==
"@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==

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