Skip to content

Commit

Permalink
feat: #49 (composable) write unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Baroshem committed Aug 22, 2021
1 parent 70d9e29 commit c019eb1
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 1 deletion.
2 changes: 1 addition & 1 deletion packages/composables/__tests__/getters/cartHelpers.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ describe('[vendure-getters] cart getters', () => {
});

it('returns totals', () => {
expect(cartGetters.getTotals(mockedCart)).toEqual({ total: 231094, subtotal: 277312 });
expect(cartGetters.getTotals(mockedCart)).toEqual({ total: 277312, subtotal: 277312 });
});

it('returns total items', () => {
Expand Down
1 change: 1 addition & 0 deletions packages/composables/__tests__/mocks/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from './mockedCollectionList';
export * from './mockedProduct';
export * from './mockedCart';
export * from './mockedShipping';
24 changes: 24 additions & 0 deletions packages/composables/__tests__/mocks/mockedShipping.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
export const mockedShippingMethods = {
eligibleShippingMethods: [
{
id: '1',
price: 500,
priceWithTax: 500,
code: 'standard-shipping',
name: 'Standard Shipping',
description: '',
metadata: null,
__typename: 'ShippingMethodQuote'
},
{
id: '2',
price: 1000,
priceWithTax: 1000,
code: 'express-shipping',
name: 'Express Shipping',
description: '',
metadata: null,
__typename: 'ShippingMethodQuote'
}
]
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { useShippingProvider } from '../../src/composables/useShippingProvider';
import { mockedShippingMethods } from '../mocks';

jest.mock('@vue-storefront/core', () => ({
useShippingProviderFactory: (params) => () => params
}));

const context = {
$vendure: {
api: {
getShippingMethods: jest.fn(() => Promise.resolve({ data: { eligibleShippingMethods: mockedShippingMethods }}))
}
}
};

describe('[vendure-composables] useShippingProvider', () => {
it('loads shipping methods', async () => {
const { load } = useShippingProvider() as any;

const response = await load(context, {});

expect(response).toEqual(mockedShippingMethods);
expect(context.$vendure.api.getShippingMethods).toBeCalledWith(undefined);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const params: UseShippingProviderParams<ShippingProvider, ShippingMethod> = {
return response?.data?.eligibleShippingMethods;
},

// Not used as Vendure provides a mutation that we are triggering from the theme via setShippingMethod API Client function
// eslint-disable-next-line @typescript-eslint/no-unused-vars
save: async (context: Context, { shippingMethod, customQuery }) => {
console.log('Mocked: saveShippingProvider');
Expand Down

0 comments on commit c019eb1

Please sign in to comment.