Skip to content

Commit

Permalink
Merge pull request #5346 from vuestorefront/error-handling
Browse files Browse the repository at this point in the history
Error handling
  • Loading branch information
filrak committed Jan 7, 2021
2 parents b7cf712 + b012400 commit a287cee
Show file tree
Hide file tree
Showing 26 changed files with 766 additions and 249 deletions.
98 changes: 98 additions & 0 deletions packages/core/core/__tests__/factories/useCartFactory.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,19 @@ function createComposable() {
useCart = useCartFactory<any, any, any, any>(params);
}

const factoryParams = {
addItem: jest.fn(() => null),
removeItem: jest.fn(),
updateItemQty: jest.fn(),
load: jest.fn(),
clear: jest.fn(),
applyCoupon: jest.fn(),
removeCoupon: jest.fn(),
isOnCart: jest.fn()
};

const useCartMock = useCartFactory(factoryParams);

describe('[CORE - factories] useCartFactory', () => {
beforeEach(() => {
jest.clearAllMocks();
Expand Down Expand Up @@ -80,6 +93,18 @@ describe('[CORE - factories] useCartFactory', () => {
expect(params.load).toHaveBeenCalled();
expect(cart.value).toEqual({ id: 'mocked_cart' });
});

it('should set error if factory method throwed', async () => {
const err = new Error('zxczxcx');
factoryParams.load.mockImplementationOnce(() => {
throw err;
});
const { load, error } = useCartMock();

await load();

expect(error.value.load).toBe(err);
});
});

describe('addItem', () => {
Expand All @@ -93,6 +118,18 @@ describe('[CORE - factories] useCartFactory', () => {
});
expect(cart.value).toEqual({ id: 'mocked_added_cart' });
});

it('should set error if factory method throwed', async () => {
const err = new Error('zxczxcx');
factoryParams.addItem.mockImplementationOnce(() => {
throw err;
});
const { addItem, error } = useCartMock();

await addItem({ product: { id: 'productId' }, quantity: 1 });

expect(error.value.addItem).toBe(err);
});
});

describe('removeItem', () => {
Expand All @@ -105,6 +142,18 @@ describe('[CORE - factories] useCartFactory', () => {
});
expect(cart.value).toEqual({ id: 'mocked_removed_cart' });
});

it('should set error if factory method throwed', async () => {
const err = new Error('zxczxcx');
factoryParams.removeItem.mockImplementationOnce(() => {
throw err;
});
const { removeItem, error } = useCartMock();

await removeItem({ product: { id: 'productId' } });

expect(error.value.removeItem).toBe(err);
});
});

describe('updateItemQty', () => {
Expand All @@ -130,6 +179,18 @@ describe('[CORE - factories] useCartFactory', () => {
});
expect(cart.value).toEqual({ id: 'mocked_updated_quantity_cart' });
});

it('should set error if factory method throwed', async () => {
const err = new Error('zxczxcx');
factoryParams.updateItemQty.mockImplementationOnce(() => {
throw err;
});
const { updateItemQty, error } = useCartMock();

await updateItemQty({ product: { id: 'productId' }, quantity: 1 });

expect(error.value.updateItemQty).toBe(err);
});
});

describe('clear', () => {
Expand All @@ -139,6 +200,18 @@ describe('[CORE - factories] useCartFactory', () => {
expect(params.clear).toHaveBeenCalledWith({ context: null }, { currentCart: null });
expect(cart.value).toEqual({ id: 'mocked_cleared_cart' });
});

it('should set error if factory method throwed', async () => {
const err = new Error('zxczxcx');
factoryParams.clear.mockImplementationOnce(() => {
throw err;
});
const { clear, error } = useCartMock();

await clear();

expect(error.value.clear).toBe(err);
});
});

describe('applyCoupon', () => {
Expand All @@ -151,6 +224,18 @@ describe('[CORE - factories] useCartFactory', () => {
});
expect(cart.value).toEqual({ id: 'mocked_apply_coupon_cart' });
});

it('should set error if factory method throwed', async () => {
const err = new Error('zxczxcx');
factoryParams.applyCoupon.mockImplementationOnce(() => {
throw err;
});
const { applyCoupon, error } = useCartMock();

await applyCoupon({ couponCode: 'qwerty' });

expect(error.value.applyCoupon).toBe(err);
});
});

describe('removeCoupon', () => {
Expand All @@ -165,6 +250,19 @@ describe('[CORE - factories] useCartFactory', () => {
expect(cart.value).toEqual({ id: 'mocked_removed_coupon_cart' });
});

it('should set error if factory method throwed', async () => {
const err = new Error('zxczxcx');
factoryParams.removeCoupon.mockImplementationOnce(() => {
throw err;
});
const { removeCoupon, error } = useCartMock();
const coupon = 'some-coupon-code-12321231';

await removeCoupon({ coupon });

expect(error.value.removeCoupon).toBe(err);
});

// TODO
// it('should not invoke removeCoupon method if coupon is not applied', async () => {
// });
Expand Down
18 changes: 18 additions & 0 deletions packages/core/core/__tests__/factories/useCategoryFactory.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ import { UseCategory } from '../../src/types';
let useCategory: (cacheId?: string) => UseCategory<any, any>;
let params: UseCategoryFactoryParams<any, any>;

const factoryParams = {
categorySearch: jest.fn()
};

const useCategoryMock = useCategoryFactory(factoryParams);

function createComposable() {
params = {
categorySearch: jest
Expand Down Expand Up @@ -37,6 +43,18 @@ describe('[CORE - factories] useCategoryFactory', () => {
expect(params.categorySearch).toBeCalledWith({ context: null }, { someparam: 'qwerty' });
expect(categories.value).toEqual({ id: 'mocked_removed_cart' });
});

it('should set error if factory method throwed', async () => {
const err = new Error('zxczxcx');
factoryParams.categorySearch.mockImplementationOnce(() => {
throw err;
});
const { search, error } = useCategoryMock('a');

await search({ someparam: 'qwerty' });

expect(error.value.search).toBe(err);
});
});
});
});
20 changes: 19 additions & 1 deletion packages/core/core/__tests__/factories/useContentFactory.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@ describe('[CORE - factories] useContentFactory', () => {
useContent = useContentFactory<any, any>(params);
};

const factoryParams = {
search: jest.fn()
};

const useContentMock = useContentFactory(factoryParams);

beforeEach(() => {
jest.clearAllMocks();
createContentFactoryMock();
Expand All @@ -27,7 +33,7 @@ describe('[CORE - factories] useContentFactory', () => {

expect(content.value).toEqual([]);
expect(loading.value).toEqual(false);
expect(error.value).toEqual(null);
expect(error.value).toEqual({});
});

it('invokes content search', async () => {
Expand All @@ -38,6 +44,18 @@ describe('[CORE - factories] useContentFactory', () => {
expect(params.search).toBeCalledWith({ context: null }, searchParams);
expect(params.search).toBeCalledTimes(1);
});

it('should set error if factory method throwed', async () => {
const err = new Error('zxczxcx');
factoryParams.search.mockImplementationOnce(() => {
throw err;
});
const { search, error } = useContentMock('a');

await search({ someparam: 'qwerty' });

expect(error.value.search).toBe(err);
});
});

describe('[CORE - factories] renderContentFactory', () => {
Expand Down
18 changes: 18 additions & 0 deletions packages/core/core/__tests__/factories/useFacetFactory.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
import { useFacetFactory } from '../../src/factories';

const factoryParams = {
search: jest.fn()
};

const useFacetMock = useFacetFactory(factoryParams);

describe('[CORE - factories] useFacetFactory', () => {
it('creates properties', () => {
const factorySearch = () => jest.fn();
Expand All @@ -21,4 +27,16 @@ describe('[CORE - factories] useFacetFactory', () => {
expect(result.value).toEqual({ data: null, input: { param: 'test' } });
expect(loading.value).toEqual(true);
});

it('should set error if factory method throwed', async () => {
const err = new Error('zxczxcx');
factoryParams.search.mockImplementationOnce(() => {
throw err;
});
const { search, error } = useFacetMock('a');

await search({ someparam: 'qwerty' });

expect(error.value.search).toBe(err);
});
});
18 changes: 18 additions & 0 deletions packages/core/core/__tests__/factories/useProductFactory.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ const useProduct: (cacheId: string) => UseProduct<any, any> = useProductFactory<
productsSearch: (context, searchParams) => Promise.resolve([{ name: 'product ' + searchParams.slug }])
});

const factoryParams = {
productsSearch: jest.fn()
};

const useProductMock = useProductFactory<any, any>(factoryParams);

describe('[CORE - factories] useProductFactory', () => {
it('creates properties', () => {
const { products, loading } = useProduct('test-product');
Expand All @@ -28,4 +34,16 @@ describe('[CORE - factories] useProductFactory', () => {

expect(products.value).toEqual([{name: 'product product-slug' }]);
});

it('should set error if factory method throwed', async () => {
const err = new Error('zxczxcx');
factoryParams.productsSearch.mockImplementationOnce(() => {
throw err;
});
const { search, error } = useProductMock('a');

await search({ someparam: 'qwerty' });

expect(error.value.search).toBe(err);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ describe('[CORE - factories] useReviews', () => {

expect(reviews.value).toEqual([]);
expect(loading.value).toEqual(false);
expect(error.value).toEqual(null);
expect(error.value).toEqual({});
});

it('returns reviews response', async () => {
Expand All @@ -89,7 +89,7 @@ describe('[CORE - factories] useReviews', () => {
await search({});

expect(reviews.value).toEqual(searchReviewResponse);
expect(error.value).toEqual(null);
expect(error.value).toEqual({ search: null });
});

it('can submit new review', async () => {
Expand All @@ -111,7 +111,7 @@ describe('[CORE - factories] useReviews', () => {

expect(reviews.value).toEqual([]);
expect(loading.value).toEqual(false);
expect(error.value).toEqual('Error: Couldn\'t retrieve reviews');
expect(error.value.search.toString()).toEqual('Error: Couldn\'t retrieve reviews');
});

it('returns error when submit fails', async () => {
Expand All @@ -121,6 +121,6 @@ describe('[CORE - factories] useReviews', () => {

expect(reviews.value).toEqual([]);
expect(loading.value).toEqual(false);
expect(error.value).toEqual('Error: Couldn\'t submit review');
expect(error.value.addReview.toString()).toEqual('Error: Couldn\'t submit review');
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,12 @@ describe('[CORE - factories] useUserBillingFactory', () => {
});

it('throws error', async () => {
const err = new Error('zxczxcx');
factoryParams.addAddress.mockImplementationOnce(() => {
throw new Error;
throw err;
});
await expect(useUserBillingMethods.addAddress('' as any)).rejects.toThrow();
await useUserBillingMethods.addAddress('' as any);
expect(useUserBillingMethods.error.value.addAddress).toBe(err);
});

it('finally loading go to false', () => {
Expand All @@ -58,10 +60,12 @@ describe('[CORE - factories] useUserBillingFactory', () => {
});

it('throws error', async () => {
const err = new Error('87878dfdf');
factoryParams.deleteAddress.mockImplementationOnce(() => {
throw new Error();
throw err;
});
await expect(useUserBillingMethods.deleteAddress('' as any)).rejects.toThrow();
await useUserBillingMethods.deleteAddress('' as any);
expect(useUserBillingMethods.error.value.deleteAddress).toBe(err);
});

it('finally loading go to false', () => {
Expand All @@ -78,10 +82,12 @@ describe('[CORE - factories] useUserBillingFactory', () => {
});

it('throws error', async () => {
const err = new Error('23232323');
factoryParams.updateAddress.mockImplementationOnce(() => {
throw new Error();
throw err;
});
await expect(useUserBillingMethods.updateAddress('' as any)).rejects.toThrow();
await useUserBillingMethods.updateAddress('' as any);
expect(useUserBillingMethods.error.value.updateAddress).toBe(err);
});

it('finally loading go to false', () => {
Expand All @@ -98,10 +104,12 @@ describe('[CORE - factories] useUserBillingFactory', () => {
});

it('throws error', async () => {
const err = new Error('cvcvc');
factoryParams.load.mockImplementationOnce(() => {
throw new Error();
throw err;
});
await expect(useUserBillingMethods.load()).rejects.toThrow();
await useUserBillingMethods.load();
expect(useUserBillingMethods.error.value.load).toBe(err);
});

it('finally loading go to false', () => {
Expand All @@ -118,10 +126,12 @@ describe('[CORE - factories] useUserBillingFactory', () => {
});

it('throws error', async () => {
const err = new Error('adsd');
factoryParams.setDefaultAddress.mockImplementationOnce(() => {
throw new Error();
throw err;
});
await expect(useUserBillingMethods.setDefaultAddress('' as any)).rejects.toThrow();
await useUserBillingMethods.setDefaultAddress('' as any);
expect(useUserBillingMethods.error.value.setDefaultAddress).toBe(err);
});

it('finally loading go to false', () => {
Expand Down

0 comments on commit a287cee

Please sign in to comment.