This repository has been archived by the owner on May 21, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
17 changed files
with
437 additions
and
294 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,8 +6,19 @@ jest.mock('@vue-storefront/commercetools-api', () => ({ | |
customerSignMeUp: jest.fn(), | ||
customerSignMeIn: jest.fn(), | ||
customerSignOut: jest.fn(), | ||
getMe: () => ({ data: { me: { customer: { firstName: 'loaded customer', | ||
lastName: 'loaded customer' } } } }) | ||
getMe: () => ({ | ||
data: { | ||
me: { | ||
customer: { | ||
firstName: 'loaded customer', | ||
lastName: 'loaded customer' | ||
}, | ||
orders: { | ||
results: [] | ||
} | ||
} | ||
} | ||
}) | ||
})); | ||
|
||
describe('[commercetools-composables] useUser', () => { | ||
|
@@ -16,8 +27,12 @@ describe('[commercetools-composables] useUser', () => { | |
}); | ||
|
||
it('registers new customer', async () => { | ||
const user = { customer: { firstName: 'john', | ||
lastName: 'doe' } }; | ||
const user = { | ||
customer: { | ||
firstName: 'john', | ||
lastName: 'doe' | ||
} | ||
}; | ||
(customerSignMeUp as any).mockReturnValue(Promise.resolve({ data: { user } })); | ||
|
||
const wrapper = mountComposable(useUser); | ||
|
@@ -33,16 +48,22 @@ describe('[commercetools-composables] useUser', () => { | |
}); | ||
|
||
it('login customer and log out', async () => { | ||
const user = { customer: { firstName: 'john', | ||
lastName: 'doe' } }; | ||
const user = { | ||
customer: { | ||
firstName: 'john', | ||
lastName: 'doe' | ||
} | ||
}; | ||
(customerSignMeIn as any).mockReturnValue(Promise.resolve({ data: { user } })); | ||
|
||
const wrapper = mountComposable(useUser); | ||
await wrapper.vm.$nextTick(); | ||
await wrapper.vm.$nextTick(); | ||
|
||
wrapper.vm.$data.login({ email: '[email protected]', | ||
password: '123' }); | ||
wrapper.vm.$data.login({ | ||
email: '[email protected]', | ||
password: '123' | ||
}); | ||
|
||
expect(wrapper.vm.$data.loading).toBeTruthy(); | ||
await wrapper.vm.$nextTick(); | ||
|
@@ -71,4 +92,12 @@ describe('[commercetools-composables] useUser', () => { | |
lastName: 'loaded customer' | ||
}); | ||
}); | ||
|
||
it('loads current customer with orders', async () => { | ||
const wrapper = mountComposable(useUser); | ||
await wrapper.vm.$nextTick(); | ||
await wrapper.vm.$nextTick(); | ||
expect(wrapper.vm.$data.orders).toEqual([]); | ||
}); | ||
|
||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import { | ||
AgnosticTotals | ||
} from '@vue-storefront/interfaces'; | ||
import { | ||
Cart, | ||
LineItem | ||
} from './types/GraphQL'; | ||
import { getProductAttributes } from './product'; | ||
|
||
// Cart | ||
|
||
export const getCartProducts = (cart: Cart): LineItem[] => { | ||
if (!cart) { | ||
return []; | ||
} | ||
|
||
return cart.lineItems; | ||
}; | ||
|
||
export const getCartProductName = (product: LineItem): string => product.name; | ||
|
||
export const getCartProductImage = (product: LineItem): string => product.variant.images[0].url; | ||
|
||
export const getCartProductPrice = (product: LineItem): number => product.price.value.centAmount / 100; | ||
|
||
export const getCartProductQty = (product: LineItem): number => product.quantity; | ||
|
||
export const getCartProductAttributes = (product: LineItem, filterByAttributeName?: Array<string>) => | ||
getProductAttributes(product.variant, filterByAttributeName); | ||
|
||
export const getCartProductSku = (product: LineItem): string => product.variant.sku; | ||
|
||
export const getCartTotals = (cart: Cart): AgnosticTotals => { | ||
if (!cart) { | ||
return { | ||
total: 0, | ||
subtotal: 0 | ||
}; | ||
} | ||
|
||
const subtotalPrice = cart.totalPrice.centAmount; | ||
const shipping = cart.shippingInfo ? cart.shippingInfo.price.centAmount : 0; | ||
|
||
return { | ||
total: (shipping + subtotalPrice) / 100, | ||
subtotal: subtotalPrice / 100 | ||
}; | ||
}; | ||
|
||
export const getCartShippingPrice = (cart: Cart): number => cart && cart.shippingInfo ? cart.shippingInfo.price.centAmount / 100 : 0; | ||
|
||
export const getCartTotalItems = (cart: Cart): number => { | ||
if (!cart) { | ||
return 0; | ||
} | ||
|
||
return cart.lineItems.reduce((previous, current) => previous + current.quantity, 0); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import { | ||
UiCategory | ||
} from '@vue-storefront/interfaces'; | ||
import { | ||
ProductVariant, | ||
Category | ||
} from './types/GraphQL'; | ||
|
||
// Category | ||
export const getCategoryProducts = (category: Category, options: any = {}): ProductVariant[] => { | ||
if (!category || !(category as any)._products) { | ||
return []; | ||
} | ||
|
||
const { _products } = category as any; | ||
|
||
if (options.master) { | ||
return _products.filter((v) => (v as any)._master); | ||
} | ||
|
||
return _products; | ||
}; | ||
|
||
export const getCategoryTree = (category: Category): UiCategory | null => { | ||
const getRoot = (category: Category): Category => (category.parent ? getRoot(category.parent) : category); | ||
const buildTree = (rootCategory: Category) => ({ | ||
label: rootCategory.name, | ||
slug: rootCategory.slug, | ||
items: rootCategory.children.map(buildTree) | ||
}); | ||
|
||
if (!category) { | ||
return null; | ||
} | ||
|
||
return buildTree(getRoot(category)); | ||
}; |
Oops, something went wrong.