Skip to content

Commit

Permalink
Fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
sirugh committed Nov 26, 2019
1 parent 2e17bd9 commit d768d13
Show file tree
Hide file tree
Showing 16 changed files with 787 additions and 997 deletions.
372 changes: 223 additions & 149 deletions packages/peregrine/lib/store/actions/cart/__tests__/asyncActions.spec.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion packages/peregrine/lib/store/actions/cart/asyncActions.js
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ export const removeItemFromCart = payload => {
const { isSignedIn } = user;
let isLastItem = false;

if (cart.details.items_count === 1) {
if (cart.details && cart.details.items_count === 1) {
isLastItem = true;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ const paymentMethod = {
title: 'Check / Money order'
};

const fetchCartId = jest.fn();

beforeAll(() => {
getState.mockImplementation(() => ({
cart: { cartId: 'CART_ID' },
Expand All @@ -76,13 +78,17 @@ describe('beginCheckout', () => {
});

test('beginCheckout thunk returns undefined', async () => {
const result = await beginCheckout()(...thunkArgs);
const result = await beginCheckout({
fetchCartId
})(...thunkArgs);

expect(result).toBeUndefined();
});

test('beginCheckout thunk dispatches actions', async () => {
await beginCheckout()(...thunkArgs);
await beginCheckout({
fetchCartId
})(...thunkArgs);

expect(dispatch).toHaveBeenNthCalledWith(
1,
Expand Down Expand Up @@ -194,11 +200,17 @@ describe('getCountries', () => {

describe('getShippingMethods', () => {
test('getShippingMethods() returns a thunk', () => {
expect(getShippingMethods()).toBeInstanceOf(Function);
expect(
getShippingMethods({
fetchCartId
})
).toBeInstanceOf(Function);
});

test('getShippingMethods thunk returns undefined', async () => {
const result = await getShippingMethods()(...thunkArgs);
const result = await getShippingMethods({
fetchCartId
})(...thunkArgs);

expect(result).toBeUndefined();
});
Expand All @@ -208,7 +220,9 @@ describe('getShippingMethods', () => {
const MOCK_RESPONSE = [];
request.mockResolvedValueOnce(MOCK_RESPONSE);

await getShippingMethods()(...thunkArgs);
await getShippingMethods({
fetchCartId
})(...thunkArgs);

expect(dispatch).toHaveBeenNthCalledWith(
1,
Expand All @@ -226,7 +240,9 @@ describe('getShippingMethods', () => {
const error = new Error('ERROR');
request.mockRejectedValueOnce(error);

await getShippingMethods()(...thunkArgs);
await getShippingMethods({
fetchCartId
})(...thunkArgs);

expect(dispatch).toHaveBeenNthCalledWith(
1,
Expand Down Expand Up @@ -573,6 +589,7 @@ describe('submitOrder', () => {

await submitOrder()(...thunkArgs);

expect(dispatch).toHaveBeenCalledTimes(4);
expect(dispatch).toHaveBeenNthCalledWith(1, actions.order.submit());
expect(dispatch).toHaveBeenNthCalledWith(
2,
Expand All @@ -581,15 +598,14 @@ describe('submitOrder', () => {
billing_address: expect.any(Object)
})
);
expect(dispatch).toHaveBeenNthCalledWith(3, actions.order.accept());
expect(dispatch).toHaveBeenCalledTimes(3);
expect(dispatch).toHaveBeenNthCalledWith(3, expect.any(Function));
expect(dispatch).toHaveBeenNthCalledWith(4, actions.order.accept());

expect(mockRemoveItem).toHaveBeenNthCalledWith(1, 'cartId');
expect(mockRemoveItem).toHaveBeenNthCalledWith(2, 'billing_address');
expect(mockRemoveItem).toHaveBeenNthCalledWith(3, 'paymentMethod');
expect(mockRemoveItem).toHaveBeenNthCalledWith(4, 'shipping_address');
expect(mockRemoveItem).toHaveBeenNthCalledWith(5, 'shippingMethod');
expect(mockRemoveItem).toHaveBeenCalledTimes(5);
expect(mockRemoveItem).toHaveBeenCalledTimes(4);
expect(mockRemoveItem).toHaveBeenNthCalledWith(1, 'billing_address');
expect(mockRemoveItem).toHaveBeenNthCalledWith(2, 'paymentMethod');
expect(mockRemoveItem).toHaveBeenNthCalledWith(3, 'shipping_address');
expect(mockRemoveItem).toHaveBeenNthCalledWith(4, 'shippingMethod');
});

test('submitOrder thunk dispatches actions and clears local storage on success when addresses are different', async () => {
Expand Down Expand Up @@ -619,6 +635,7 @@ describe('submitOrder', () => {

await submitOrder()(...thunkArgs);

expect(dispatch).toHaveBeenCalledTimes(4);
expect(dispatch).toHaveBeenNthCalledWith(1, actions.order.submit());
expect(dispatch).toHaveBeenNthCalledWith(
2,
Expand All @@ -627,15 +644,14 @@ describe('submitOrder', () => {
billing_address: expect.any(Object)
})
);
expect(dispatch).toHaveBeenNthCalledWith(3, actions.order.accept());
expect(dispatch).toHaveBeenCalledTimes(3);

expect(mockRemoveItem).toHaveBeenNthCalledWith(1, 'cartId');
expect(mockRemoveItem).toHaveBeenNthCalledWith(2, 'billing_address');
expect(mockRemoveItem).toHaveBeenNthCalledWith(3, 'paymentMethod');
expect(mockRemoveItem).toHaveBeenNthCalledWith(4, 'shipping_address');
expect(mockRemoveItem).toHaveBeenNthCalledWith(5, 'shippingMethod');
expect(mockRemoveItem).toHaveBeenCalledTimes(5);
expect(dispatch).toHaveBeenNthCalledWith(3, expect.any(Function));
expect(dispatch).toHaveBeenNthCalledWith(4, actions.order.accept());

expect(mockRemoveItem).toHaveBeenCalledTimes(4);
expect(mockRemoveItem).toHaveBeenNthCalledWith(1, 'billing_address');
expect(mockRemoveItem).toHaveBeenNthCalledWith(2, 'paymentMethod');
expect(mockRemoveItem).toHaveBeenNthCalledWith(3, 'shipping_address');
expect(mockRemoveItem).toHaveBeenNthCalledWith(4, 'shippingMethod');
});

test('submitOrder thunk dispatches actions on failure', async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,7 @@ export const submitOrder = () =>
);

// Clear out everything we've saved about this cart from local storage.
await removeCart();
await dispatch(removeCart());
await clearCheckoutDataFromStorage();

dispatch(actions.order.accept());
Expand Down
31 changes: 3 additions & 28 deletions packages/peregrine/lib/store/reducers/__tests__/cart.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ describe('addItem.receive', () => {
describe('updateItem.request', () => {
const actionType = actions.updateItem.request;

test('it sets isUpdatingItem to true and includes all of payload', () => {
test('it sets isUpdatingItem to true', () => {
const action = {
payload: { unit: 'test', other: 'stuff' },
type: actionType
Expand All @@ -116,8 +116,6 @@ describe('updateItem.request', () => {
const result = reducer(state, action);

expect(result).toHaveProperty('isUpdatingItem', true);
expect(result).toHaveProperty('unit', 'test');
expect(result).toHaveProperty('other', 'stuff');
});
});

Expand Down Expand Up @@ -150,30 +148,7 @@ describe('updateItem.receive', () => {
describe('removeItem.receive', () => {
const actionType = actions.removeItem.receive;

test('it includes all of payload on success', () => {
const action = {
payload: { unit: 'test', cartItemCount: 5 },
type: actionType
};

const result = reducer(state, action);

expect(result).toHaveProperty('unit', 'test');
expect(result).toHaveProperty('cartItemCount', 5);
});

test('it returns the initial state if there is only one item', () => {
const action = {
payload: { unit: 'test', cartItemCount: 1 },
type: actionType
};

const result = reducer(state, action);

expect(result).toEqual(initialState);
});

test('it returns the initial state on error', () => {
test('it returns the state with error on error', () => {
const action = {
error: true,
payload: new Error('unit test'),
Expand All @@ -183,7 +158,7 @@ describe('removeItem.receive', () => {
const result = reducer(state, action);

expect(result).toEqual({
...initialState,
...state,
removeItemError: action.payload
});
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export const useCreateAccount = props => {
} = props;

const [isSubmitting, setIsSubmitting] = useState(false);
const [, { removeCart }] = useCartContext();
const [, { getCartDetails, removeCart }] = useCartContext();
const [
{ isGettingDetails, isSignedIn },
{ getUserDetails, setToken }
Expand Down Expand Up @@ -83,10 +83,12 @@ export const useCreateAccount = props => {

// Then remove the old guest cart.
await removeCart();

await getCartDetails({
forceRefresh: true,
fetchCartId
});

// Finally, invoke the post-submission callback.
onSubmit();
} catch (error) {
Expand All @@ -99,6 +101,7 @@ export const useCreateAccount = props => {
[
createAccount,
fetchCartId,
getCartDetails,
getUserDetails,
onSubmit,
removeCart,
Expand Down
2 changes: 1 addition & 1 deletion packages/peregrine/lib/talons/Header/useCartTrigger.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { useCallback, useEffect, useMemo } from 'react';
import { useMutation } from '@apollo/react-hooks';
import { useCartContext } from '@magento/peregrine/lib/context/cart';
import { useAppContext } from '../../context/app';
import { useAppContext } from '@magento/peregrine/lib/context/app';

export const useCartTrigger = props => {
const { createCartMutation } = props;
Expand Down
1 change: 1 addition & 0 deletions packages/peregrine/lib/talons/MiniCart/useProduct.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { useCallback, useState } from 'react';
import { useMutation } from '@apollo/react-hooks';

export const useProduct = props => {
const {
beginEditItem,
Expand Down
Loading

0 comments on commit d768d13

Please sign in to comment.