Skip to content

Commit

Permalink
refactor: wishlist tests for new changes
Browse files Browse the repository at this point in the history
  • Loading branch information
Fifciuu committed Dec 23, 2020
1 parent c595d57 commit 6812862
Showing 1 changed file with 62 additions and 0 deletions.
62 changes: 62 additions & 0 deletions packages/core/core/__tests__/factories/useWishlistFactory.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,16 @@ function createComposable() {
setWishlist = factory.setWishlist;
}

const factoryParams = {
addItem: jest.fn(() => null),
removeItem: jest.fn(),
load: jest.fn(),
clear: jest.fn(),
isOnWishlist: jest.fn()
};

const { useWishlist: useWishlistMock } = useWishlistFactory<any, any, any>(factoryParams);

describe('[CORE - factories] useWishlistFactory', () => {
beforeEach(() => {
jest.clearAllMocks();
Expand Down Expand Up @@ -73,6 +83,18 @@ describe('[CORE - factories] useWishlistFactory', () => {
expect(params.load).toHaveBeenCalledWith({ context: null }, { customQuery });
expect(wishlist.value).toEqual({ id: 'mocked_wishlist' });
});

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

await load();

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

describe('addItem', () => {
Expand All @@ -85,6 +107,20 @@ describe('[CORE - factories] useWishlistFactory', () => {
});
expect(wishlist.value).toEqual({ id: 'mocked_added_wishlist' });
});

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

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

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

describe('removeItem', () => {
Expand All @@ -97,6 +133,20 @@ describe('[CORE - factories] useWishlistFactory', () => {
});
expect(wishlist.value).toEqual({ id: 'mocked_removed_wishlist' });
});

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

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

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

describe('clear', () => {
Expand All @@ -106,6 +156,18 @@ describe('[CORE - factories] useWishlistFactory', () => {
expect(params.clear).toHaveBeenCalledWith({ context: null }, { currentWishlist: null });
expect(wishlist.value).toEqual({ id: 'mocked_cleared_wishlist' });
});

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

await clear();

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

0 comments on commit 6812862

Please sign in to comment.