Skip to content

Conversation

@AleGastelum
Copy link
Contributor

@AleGastelum AleGastelum commented Dec 5, 2025

Adds the following:

  • Card Fields Provider
  • Card Fields Context
  • useCardFields hook for interacting with the provider's context
  • Test files

The Card Fields Provider creates, manages, and shares session with child components that will need access to it.

@changeset-bot
Copy link

changeset-bot bot commented Dec 5, 2025

🦋 Changeset detected

Latest commit: bdc987d

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@paypal/react-paypal-js Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@AleGastelum AleGastelum marked this pull request as ready for review December 5, 2025 19:26
@AleGastelum AleGastelum requested a review from a team as a code owner December 5, 2025 19:26
@AleGastelum AleGastelum changed the title CardFieldsProvider and context created with tests CardFieldsProvider and context created Dec 5, 2025
Comment on lines 48 to 59
// Early return: Still loading, wait for sdkInstance
if (loadingStatus === INSTANCE_LOADING_STATE.PENDING) {
return;
}

// Error case: Loading finished but no sdkInstance
if (!sdkInstance) {
const errorMsg = toError("no sdk instance available");
setError(errorMsg);
setCardFieldsError(errorMsg);
return;
}
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm wondering if we want to handle the error case in a separate useEffect? The hooks follow this pattern.

The early return AND the error handling together effectively do the same thing as the hook's strategy, this is merely a suggestion.

Copy link
Contributor Author

@AleGastelum AleGastelum Dec 5, 2025

Choose a reason for hiding this comment

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

I think it might be good to follow the pattern other hooks/components are following. Thanks for mentioning it, I will make the update

Copy link
Contributor Author

@AleGastelum AleGastelum Dec 6, 2025

Choose a reason for hiding this comment

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

Took another look at this—the separate useEffect pattern in this file seems to be addressing an infinite looping issue we don't have here. I'm leaning towards keeping it in one useEffect for simplicity and maintainability, but wanted to get your thoughts. Is there another benefit to splitting it that I'm not seeing?

Copy link
Contributor

Choose a reason for hiding this comment

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

I think you're correct in the difference between this use case and the hooks'. The only benefit is having a separate effect that will handle errors unrelated to creating the session, but as you said it may impact simplicity and maintainability.

export { PayPalOneTimePaymentButton } from "./components/PayPalOneTimePaymentButton";
export { PayPalProvider } from "./components/PayPalProvider";
export { usePayPal } from "./hooks/usePayPal";
export { CardFieldsProvider } from "./components/CardFieldsProvider";
Copy link
Contributor

@nityasp nityasp Dec 8, 2025

Choose a reason for hiding this comment

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

Any specific reason why useCardFields hook is not exported? Currently we are exporting all our hooks. Is there any different intended usage pattern that we want to access CardFields session?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good question. useCardFields hook will be internally used by other Card Field components and hooks (that will be added in following PRs) to manage session usage. Those components will then make sure to export anything the merchant needs from the session. That is why only the provider is needed by the merchant and not the hook.

Copy link
Contributor

@EvanReinstein EvanReinstein left a comment

Choose a reason for hiding this comment

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

Lgtm! A couple of small comments but otherwise, glad to see Card Fields cruising along 👍

@AleGastelum AleGastelum merged commit 14441e5 into main Dec 9, 2025
5 checks passed
@AleGastelum AleGastelum deleted the feature/DTPPCPSDK-4084-cardfields-provider branch December 9, 2025 21:02
return { html, cardFieldsSessionState, cardFieldsStatusState };
}

function setupSSRTestComponent() {
Copy link
Contributor

Choose a reason for hiding this comment

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

What makes this "SSR" specifically?


jest.mock("../utils", () => ({
...jest.requireActual("../utils"),
isServer: () => true,
Copy link
Contributor

Choose a reason for hiding this comment

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

Also, I don't see the provider using isServer anywhere, should it be?

Comment on lines +102 to +104
test("should verify isServer mock is working", () => {
expect(isServer()).toBe(true);
});
Copy link
Contributor

Choose a reason for hiding this comment

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

This is a strange test, did this come from AI? I don't think this test suite should be checking this.


test("should not attempt DOM access during server rendering", () => {
// In Node environment, document should be undefined
expect(typeof document).toBe("undefined");
Copy link
Contributor

Choose a reason for hiding this comment

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

IMO, if we're meaning to test SSR, we should use and mock isServer for these tests rather than checking if the document is undefined directly.

Copy link
Contributor

Choose a reason for hiding this comment

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

These tests are weird to me since we don't have SSR logic in the provider. Some of these tests seem incorrect, others are testing functionality I'm not sure we should be testing for SSR specifically (e.g. Context isolation), and some I don't think we're actually specifically coding for (e.g. should maintain state consistency across multiple server renders).

IMO we should add these when we have SSR logic, and we should be more specific / careful about what we're testing.

Copy link
Contributor Author

@AleGastelum AleGastelum Dec 9, 2025

Choose a reason for hiding this comment

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

Great callout! This tests were implemented following the tests PayPalProvider had for SSR. This subject was brought up with the team. The SSR tests files for Card fields and PayPal providers will be removed in a new PR. SSR tests will be added as needed.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Removed here

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants