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

PWA-2985: [bug]: Payment method always reverts to "Check / Money orde… #3969

Merged
merged 4 commits into from
Oct 3, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ Object {
},
],
"currentSelectedPaymentMethod": "currentSelectedPaymentMethod",
"initialSelectedMethod": "availablePaymentMethod",
"handlePaymentMethodSelection": [Function],
"initialSelectedMethod": "braintree",
"isLoading": false,
}
`;
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,21 @@ jest.mock(
}
);

const mockSetPaymentMethodOnCartMutation = jest.fn();

jest.mock('@apollo/client', () => {
return {
useMutation: jest.fn(() => [
mockSetPaymentMethodOnCartMutation,
{ loading: true }
]),
useQuery: jest.fn().mockReturnValue({
data: {
cart: {
available_payment_methods: [
{ code: 'availablePaymentMethod' }
]
],
selected_payment_method: { code: 'braintree' }
}
},
loading: false
Expand All @@ -34,9 +41,18 @@ jest.mock('@apollo/client', () => {
});

jest.mock('../paymentMethods.gql', () => ({
getPaymentMethodsQuery: 'paymentMethodsQuery'
getPaymentMethodsQuery: 'paymentMethodsQuery',
setPaymentMethodOnCartMutation: 'setPaymentMethod'
}));

const getPaymentMethodsQuery = 'getPaymentMethodsQuery';
const setPaymentMethodOnCartMutation = 'setPaymentMethodOnCartMutation';

const operations = {
getPaymentMethodsQuery,
setPaymentMethodOnCartMutation
};

const Component = props => {
const talonProps = usePaymentMethods(props);

Expand All @@ -58,7 +74,9 @@ const getTalonProps = props => {
};

it('returns the correct shape', () => {
const { talonProps } = getTalonProps();
const { talonProps } = getTalonProps({
operations
});

expect(talonProps).toMatchSnapshot();
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,33 @@ export const GET_PAYMENT_METHODS = gql`
code
title
}
selected_payment_method {
code
}
}
}
`;

export const SET_PAYMENT_METHOD_ON_CART = gql`
mutation setPaymentMethodOnCart(
$cartId: String!
$paymentMethod: PaymentMethodInput!
) {
setPaymentMethodOnCart(
input: { cart_id: $cartId, payment_method: $paymentMethod }
) {
cart {
id
selected_payment_method {
code
title
}
}
}
}
`;

export default {
getPaymentMethodsQuery: GET_PAYMENT_METHODS
getPaymentMethodsQuery: GET_PAYMENT_METHODS,
setPaymentMethodOnCartMutation: SET_PAYMENT_METHOD_ON_CART
};
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { useQuery } from '@apollo/client';
import { useCallback } from 'react';
import { useMutation, useQuery } from '@apollo/client';
import useFieldState from '@magento/peregrine/lib/hooks/hook-wrappers/useInformedFieldStateWrapper';
import DEFAULT_OPERATIONS from './paymentMethods.gql';
import mergeOperations from '@magento/peregrine/lib/util/shallowMerge';
Expand All @@ -7,7 +8,12 @@ import { useCartContext } from '../../../context/cart';

export const usePaymentMethods = props => {
const operations = mergeOperations(DEFAULT_OPERATIONS, props.operations);
const { getPaymentMethodsQuery } = operations;
const {
getPaymentMethodsQuery,
setPaymentMethodOnCartMutation
} = operations;

const [setPaymentMethod] = useMutation(setPaymentMethodOnCartMutation);

const [{ cartId }] = useCartContext();

Expand All @@ -24,12 +30,32 @@ export const usePaymentMethods = props => {
(data && data.cart.available_payment_methods) || [];

const initialSelectedMethod =
(availablePaymentMethods.length && availablePaymentMethods[0].code) ||
null;
(data && data.cart.selected_payment_method.code) || null;

const handlePaymentMethodSelection = useCallback(
element => {
const value = element.target.value;

setPaymentMethod({
variables: {
cartId,
paymentMethod: {
code: value,
braintree: {
payment_method_nonce: value,
is_active_payment_token_enabler: false
}
}
}
});
},
[cartId, setPaymentMethod]
);

return {
availablePaymentMethods,
currentSelectedPaymentMethod,
handlePaymentMethodSelection,
initialSelectedMethod,
isLoading: loading
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ const PaymentMethods = props => {
const {
availablePaymentMethods,
currentSelectedPaymentMethod,
handlePaymentMethodSelection,
initialSelectedMethod,
isLoading
} = talonProps;
Expand Down Expand Up @@ -65,6 +66,7 @@ const PaymentMethods = props => {
label: classes.radio_label
}}
checked={isSelected}
onChange={handlePaymentMethodSelection}
/>
{renderedComponent}
</div>
Expand Down