Skip to content

Commit

Permalink
PWA-2985: [bug]: Payment method always reverts to “Check / Money orde…
Browse files Browse the repository at this point in the history
…r” when there is an error with the transaction

- Set default/initial payment method
- Add tests
- Update padding
  • Loading branch information
anthoula committed Oct 3, 2022
1 parent 21412f3 commit e9a56e4
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ test('pre-caches wishlist items', async () => {
Object {
"customerWishlistProducts": Array [
"Dress",
"Shirt",
],
}
`);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ Object {
],
"currentSelectedPaymentMethod": "currentSelectedPaymentMethod",
"handlePaymentMethodSelection": [Function],
"initialSelectedMethod": "braintree",
"initialSelectedMethod": "availablePaymentMethod",
"isLoading": false,
}
`;
Original file line number Diff line number Diff line change
Expand Up @@ -89,3 +89,38 @@ it('returns an empty array for availablePaymentMethods when there is no data', (
expect(talonProps.availablePaymentMethods).toEqual([]);
expect(talonProps.initialSelectedMethod).toBeNull();
});

it('default payment is selected when there is one available payment method', () => {
useQuery.mockReturnValueOnce({
data: {
cart: {
available_payment_methods: [{ code: 'availablePaymentMethod' }],
selected_payment_method: { code: null }
}
}
});

const { talonProps } = getTalonProps();

expect(talonProps.availablePaymentMethods.length).toEqual(1);
expect(talonProps.initialSelectedMethod).toEqual('availablePaymentMethod');
});

it('default payment is not selected when there is more than one available payment method', () => {
useQuery.mockReturnValueOnce({
data: {
cart: {
available_payment_methods: [
{ code: 'availablePaymentMethod1' },
{ code: 'availablePaymentMethod2' }
],
selected_payment_method: { code: null }
}
}
});

const { talonProps } = getTalonProps();

expect(talonProps.availablePaymentMethods.length).toEqual(2);
expect(talonProps.initialSelectedMethod).toBeNull();
});
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,19 @@ export const usePaymentMethods = props => {
const availablePaymentMethods =
(data && data.cart.available_payment_methods) || [];

const initialSelectedMethod =
// If there is one payment method, select it by default.
// If more than one, none should be selected by default.
const defaultPaymentCode =
(availablePaymentMethods.length && availablePaymentMethods[0].code) ||
null;
const selectedPaymentCode =
(data && data.cart.selected_payment_method.code) || null;

const initialSelectedMethod =
availablePaymentMethods.length > 1
? selectedPaymentCode
: defaultPaymentCode;

const handlePaymentMethodSelection = useCallback(
element => {
const value = element.target.value;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
.root {
composes: grid from global;
composes: p-md from global;
composes: pb-xs from global;
composes: pb-s from global;
}

.radio_group {
Expand All @@ -14,8 +14,6 @@
composes: border-subtle from global;
composes: pb-xs from global;
composes: pt-xs from global;

composes: last_pt-0 from global;
}

/* TODO @TW: cannot compose */
Expand Down

0 comments on commit e9a56e4

Please sign in to comment.