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 jest mock file #565

Merged
merged 4 commits into from
Sep 17, 2021
Merged
Show file tree
Hide file tree
Changes from 3 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
22 changes: 22 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,28 @@ Certain payment methods require a [webhook listener](https://stripe.com/docs/pay
- Run `stripe listen --forward-to localhost:4242/webhook`
- The CLI will print a webhook secret (such as, `whsec_***`) to the console. Set STRIPE_WEBHOOK_SECRET to this value in your `example/.env` file.

## Testing

This library includes a built in mock file for Jest.
In order to use it, add the following code to the Jest setup file:

```tsx
import mock from '@stripe/stripe-react-native/jest/stripe-react-native-mock.js';
arekkubaczkowski marked this conversation as resolved.
Show resolved Hide resolved

jest.mock('@stripe/stripe-react-native', () => mock);
```

To have a more control over the mocks, you can extend and override particular methods e.g.:

```tsx
const presentApplePayMock = jest.fn();

jest.mock('@stripe/stripe-react-native', () => ({
...mock,
presentApplePay: presentApplePayMock,
}));
```

## Troubleshooting

### `Undefined symbols for architecture x86_64` on iOS
Expand Down
128 changes: 128 additions & 0 deletions jest/stripe-react-native-mock.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
/* eslint-disable no-undef */

const mockFunctions = {
createPaymentMethod: jest.fn(async () => ({
paymentMethod: {},
error: null,
})),
createToken: jest.fn(async () => ({
token: {},
error: null,
})),
retrievePaymentIntent: jest.fn(async () => ({
paymentIntent: {},
error: null,
})),
retrieveSetupIntent: jest.fn(async () => ({
setupIntent: {},
error: null,
})),
confirmPayment: jest.fn(async () => ({
paymentMethod: {},
error: null,
})),
isApplePaySupported: jest.fn(async () => true),
presentApplePay: jest.fn(async () => ({
error: null,
})),
updateApplePaySummaryItems: jest.fn(async () => ({})),
confirmApplePayPayment: jest.fn(async () => ({})),
handleCardAction: jest.fn(async () => ({
paymentIntent: {},
error: null,
})),
confirmSetupIntent: jest.fn(async () => ({
setupIntent: {},
error: null,
})),
createTokenForCVCUpdate: jest.fn(async () => ({
tokenId: '123',
error: null,
})),
handleURLCallback: jest.fn(async () => true),
presentPaymentSheet: jest.fn(async () => ({
paymentOption: {},
error: null,
})),
confirmPaymentSheetPayment: jest.fn(async () => ({
error: null,
})),
initGooglePay: jest.fn(async () => ({
error: null,
})),
presentGooglePay: jest.fn(async () => ({
error: null,
})),
createGooglePayPayment: jest.fn(async () => ({
paymentMethod: {},
error: null,
})),
openApplePaySetup: jest.fn(async () => ({
error: null,
})),
initPaymentSheet: jest.fn(async () => ({
paymentOption: {},
error: null,
})),
};

const mockHooks = {
useConfirmPayment: jest.fn(() => ({
confirmPayment: jest.fn(() => ({
...mockFunctions.confirmPayment(),
})),
})),
useConfirmSetupIntent: jest.fn(() => ({
confirmSetupIntent: jest.fn(() => ({
...mockFunctions.confirmSetupIntent(),
})),
})),
useGooglePay: jest.fn(() => ({
loading: false,
initGooglePay: jest.fn(async () => ({
...mockFunctions.initGooglePay(),
})),
presentGooglePay: jest.fn(async () => ({
...mockFunctions.presentGooglePay(),
})),
createGooglePayPaymentMethod: jest.fn(async () => ({
...mockFunctions.createGooglePayPayment(),
})),
})),
useApplePay: jest.fn(() => ({
loading: false,
isApplePaySupported: true,
presentApplePay: jest.fn(async () => ({
...mockFunctions.presentApplePay(),
})),
confirmApplePayPayment: jest.fn(async () => ({
...mockFunctions.confirmApplePayPayment(),
})),
openApplePaySetup: jest.fn(async () => ({
...mockFunctions.openApplePaySetup(),
})),
})),
usePaymentSheet: jest.fn(() => ({
loading: false,
initPaymentSheet: jest.fn(async () => ({
...mockFunctions.initPaymentSheet(),
})),
presentPaymentSheet: jest.fn(async () => ({
...mockFunctions.presentPaymentSheet(),
})),
confirmPaymentSheetPayment: jest.fn(async () => ({
...mockFunctions.confirmPaymentSheetPayment(),
})),
})),
};

module.exports = {
...mockFunctions,
...mockHooks,
StripeProvider: jest.fn(({ children }) => children),
CardField: jest.fn(() => null),
ApplePayButton: jest.fn(() => null),
AuBECSDebitForm: jest.fn(() => null),
GooglePayButton: jest.fn(() => null),
arekkubaczkowski marked this conversation as resolved.
Show resolved Hide resolved
useStripe: jest.fn(() => mockHooks),
};
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
"ios",
"cpp",
"app.plugin.js",
"stripe-react-native.podspec"
"stripe-react-native.podspec",
"jest"
],
"scripts": {
"test": "jest",
Expand Down
10 changes: 5 additions & 5 deletions src/hooks/useConfirmSetupIntent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,17 @@ import { useStripe } from './useStripe';
*/
export function useConfirmSetupIntent() {
const [loading, setLoading] = useState(false);
const { confirmSetupIntent: confirmSetupIntentNative } = useStripe();
const { confirmSetupIntent: confirmSetupIntent } = useStripe();
arekkubaczkowski marked this conversation as resolved.
Show resolved Hide resolved

const confirmSetupIntent = useCallback(
const _confirmSetupIntent = useCallback(
async (
paymentIntentClientSecret: string,
data: ConfirmSetupIntent.Params,
options: ConfirmSetupIntent.Options = {}
) => {
setLoading(true);

const result = await confirmSetupIntentNative(
const result = await confirmSetupIntent(
paymentIntentClientSecret,
data,
options
Expand All @@ -27,11 +27,11 @@ export function useConfirmSetupIntent() {

return result;
},
[confirmSetupIntentNative]
[confirmSetupIntent]
);

return {
confirmSetupIntent,
confirmSetupIntent: _confirmSetupIntent,
loading,
};
}