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.
add orders history to my account (#213)
* Add orders history to my account
- Loading branch information
Showing
28 changed files
with
443 additions
and
66 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
16 changes: 16 additions & 0 deletions
16
packages/commercetools/api-client/src/api/getMyOrders/defaultQuery.ts
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,16 @@ | ||
import gql from 'graphql-tag'; | ||
import { OrderFragment } from './../../fragments'; | ||
|
||
export default gql` | ||
${OrderFragment} | ||
query getMe($where: String, $sort: [String!], $limit: Int, $offset: Int, $locale: Locale!) { | ||
me { | ||
orders(where: $where, sort: $sort, limit: $limit, offset: $offset) { | ||
results { | ||
...DefaultOrder | ||
} | ||
} | ||
} | ||
} | ||
`; |
18 changes: 18 additions & 0 deletions
18
packages/commercetools/api-client/src/api/getMyOrders/index.ts
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,18 @@ | ||
import { apolloClient, locale } from './../../index'; | ||
import defaultQuery from './defaultQuery'; | ||
import { buildOrderWhere } from './../../helpers/search'; | ||
import { OrderSearch, ProfileResponse } from './../../types/Api'; | ||
|
||
export default async (search: OrderSearch): Promise<ProfileResponse> => { | ||
return await apolloClient.query({ | ||
query: defaultQuery, | ||
variables: { | ||
where: buildOrderWhere(search), | ||
sort: search.sort, | ||
limit: search.limit, | ||
offset: search.offset, | ||
locale | ||
}, | ||
fetchPolicy: 'no-cache' | ||
}); | ||
}; |
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 |
---|---|---|
|
@@ -144,6 +144,7 @@ export const OrderFragment = ` | |
orderState | ||
id | ||
version | ||
createdAt | ||
} | ||
`; | ||
|
||
|
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
32 changes: 32 additions & 0 deletions
32
packages/commercetools/api-client/tests/api/getMyOrders/getMyOrders.spec.ts
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,32 @@ | ||
import getMyOrders from '../../../src/api/getMyOrders'; | ||
import { apolloClient } from '../../../src/index'; | ||
import defaultQuery from '../../../src/api/getMyOrders/defaultQuery'; | ||
import { OrderSearch } from '../../../src/types/Api'; | ||
|
||
describe('[commercetools-api-client] getMyOrders', () => { | ||
it('fetches current user data', async () => { | ||
const search: OrderSearch = { | ||
id: 'fvdrt8gaw4r', | ||
limit: 10, | ||
offset: 0 | ||
}; | ||
const givenVariables = { | ||
locale: 'en', | ||
where: 'id="fvdrt8gaw4r"', | ||
limit: 10, | ||
offset: 0, | ||
sort: undefined | ||
}; | ||
|
||
(apolloClient.query as any).mockImplementation(({ variables, query }) => { | ||
expect(variables).toEqual(givenVariables); | ||
expect(query).toEqual(defaultQuery); | ||
|
||
return { data: 'me response' }; | ||
}); | ||
|
||
const { data } = await getMyOrders(search); | ||
|
||
expect(data).toBe('me response'); | ||
}); | ||
}); |
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 |
---|---|---|
@@ -0,0 +1,5 @@ | ||
export type OrderSearchParams = { | ||
id?: string; | ||
page?: number; | ||
perPage?: number; | ||
}; |
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
14 changes: 14 additions & 0 deletions
14
packages/commercetools/composables/src/useUserOrders/index.ts
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,14 @@ | ||
import { useUserOrdersFactory, UseUserOrdersFactoryParams, OrdersSearchResult } from '@vue-storefront/factories'; | ||
import { Order } from '../types/GraphQL'; | ||
import { OrderSearchParams } from '../types'; | ||
import { getMyOrders } from '@vue-storefront/commercetools-api'; | ||
|
||
const params: UseUserOrdersFactoryParams<Order, OrderSearchParams> = { | ||
searchOrders: async (params: OrderSearchParams = {}): Promise<OrdersSearchResult<Order>> => { | ||
const result = await getMyOrders(params); | ||
const { results: data, total } = result.data?.me.orders || { results: [], total: 0 }; | ||
return { data, total }; | ||
} | ||
}; | ||
|
||
export default useUserOrdersFactory<Order, OrderSearchParams>(params); |
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,17 @@ 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' } } } }), | ||
customerChangeMyPassword: jest.fn() | ||
customerChangeMyPassword: jest.fn(), | ||
getMe: () => ({ | ||
data: { | ||
me: { | ||
customer: { | ||
firstName: 'loaded customer', | ||
lastName: 'loaded customer' | ||
} | ||
} | ||
} | ||
}) | ||
})); | ||
|
||
describe.skip('[commercetools-composables] useUser', () => { | ||
|
@@ -22,8 +31,12 @@ describe.skip('[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); | ||
|
@@ -39,15 +52,22 @@ describe.skip('[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(); | ||
|
47 changes: 47 additions & 0 deletions
47
packages/commercetools/composables/tests/useUserOrders/useUserOrders.spec.ts
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,47 @@ | ||
import { useUserOrders } from '../../src'; | ||
import { getMyOrders } from '@vue-storefront/commercetools-api'; | ||
|
||
jest.mock('@vue-storefront/commercetools-api', () => ({ | ||
getMyOrders: jest.fn() | ||
})); | ||
|
||
describe('[commercetools-composables] useUserOrders', () => { | ||
beforeEach(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
it('has proper initial state', () => { | ||
const { orders, loading } = useUserOrders(); | ||
expect(loading.value).toEqual(false); | ||
expect(orders.data.value).toEqual([]); | ||
expect(orders.total.value).toEqual(0); | ||
}); | ||
|
||
describe('search orders', () => { | ||
it('gets no params and resolves to orders list', async () => { | ||
(getMyOrders as jest.Mock).mockResolvedValueOnce({ data: { me: { orders: { results: [{ id: 'first' }, { id: 'second' }], total: 10 } } } }); | ||
const { orders, searchOrders } = useUserOrders(); | ||
await searchOrders(); | ||
expect(orders.data.value).toEqual([{ id: 'first' }, { id: 'second' }]); | ||
expect(orders.total.value).toEqual(10); | ||
expect(getMyOrders).toBeCalledWith({}); | ||
}); | ||
|
||
it('gets order id and passes it to api client', async () => { | ||
(getMyOrders as jest.Mock).mockResolvedValueOnce({ data: { me: { orders: { results: [{ id: 'first' }], total: 1 } } } }); | ||
const { orders, searchOrders } = useUserOrders(); | ||
await searchOrders({ id: 'first' }); | ||
expect(getMyOrders).toBeCalledWith({ id: 'first' }); | ||
expect(orders.data.value).toEqual([{ id: 'first' }]); | ||
expect(orders.total.value).toEqual(1); | ||
}); | ||
|
||
it('gets empty result from api client', async () => { | ||
(getMyOrders as jest.Mock).mockResolvedValueOnce({}); | ||
const { orders, searchOrders } = useUserOrders(); | ||
await searchOrders(); | ||
expect(orders.data.value).toEqual([]); | ||
expect(orders.total.value).toEqual(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
Oops, something went wrong.