Skip to content
This repository has been archived by the owner on May 21, 2020. It is now read-only.

Commit

Permalink
add orders to useUser composable
Browse files Browse the repository at this point in the history
  • Loading branch information
Qrzy committed Feb 24, 2020
1 parent 7e5ff8b commit 59eca6a
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 14 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import gql from 'graphql-tag';
import { CartFragment, CustomerFragment } from './../../fragments';
import { CartFragment, CustomerFragment, OrderFragment } from './../../fragments';

const basicProfile = gql`
${CartFragment}
Expand All @@ -16,6 +16,7 @@ const basicProfile = gql`
const fullProfile = gql`
${CartFragment}
${CustomerFragment}
${OrderFragment}
query getMe($locale: Locale!) {
me {
Expand All @@ -25,6 +26,11 @@ const fullProfile = gql`
customer {
...DefaultCustomer
}
orders {
results {
...DefaultOrder
}
}
}
}
`;
Expand Down
13 changes: 8 additions & 5 deletions packages/commercetools/composables/src/useUser/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { ref, Ref, watch, computed } from '@vue/composition-api';
import { UseUser, AgnosticUserLogin, AgnosticUserRegister } from '@vue-storefront/interfaces';
import { Customer, CustomerSignMeUpDraft, CustomerSignMeInDraft } from '@vue-storefront/commercetools-api/lib/src/types/GraphQL';
import { Customer, CustomerSignMeUpDraft, CustomerSignMeInDraft, Order } from '@vue-storefront/commercetools-api/lib/src/types/GraphQL';
import {
customerSignMeUp,
customerSignMeIn,
Expand All @@ -9,13 +9,13 @@ import {
} from '@vue-storefront/commercetools-api';
import { cart } from './../useCart';

type UserRef = Ref<Customer>
type RegisterFn = (userData: AgnosticUserRegister) => Promise<void>
type LoginFn = (userData: AgnosticUserLogin) => Promise<void>
type LogoutFn = () => Promise<void>
type UserData = CustomerSignMeUpDraft | CustomerSignMeInDraft

const user: UserRef = ref({});
const user: Ref<Customer> = ref({});
const orders: Ref<Order[]> = ref([]);
const loading = ref(false);
const error = ref(null);
const isAuthenticated = computed(() => user.value && Object.keys(user.value).length > 0);
Expand All @@ -33,7 +33,7 @@ const authenticate = async (userData: UserData, fn) => {
loading.value = false;
};

export default function useUser(): UseUser<UserRef, RegisterFn, LoginFn, LogoutFn> {
export default function useUser(): UseUser<Ref<Customer>, RegisterFn, LoginFn, LogoutFn, Ref<Order[]>> {
watch(user, async () => {
if (isAuthenticated.value) {
return;
Expand All @@ -44,7 +44,9 @@ export default function useUser(): UseUser<UserRef, RegisterFn, LoginFn, LogoutF
try {
const profile = await getMe({ customer: true });
user.value = profile.data.me.customer;
} catch (err) {} // eslint-disable-line
orders.value = profile.data.me.orders.results;
// eslint-disable-next-line no-empty
} catch (err) {}

loading.value = false;
});
Expand All @@ -67,6 +69,7 @@ export default function useUser(): UseUser<UserRef, RegisterFn, LoginFn, LogoutF

return {
user,
orders,
register,
login,
logout,
Expand Down
45 changes: 37 additions & 8 deletions packages/commercetools/composables/tests/useUser/useUser.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand All @@ -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);
Expand All @@ -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();
Expand Down Expand Up @@ -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([]);
});

});
2 changes: 2 additions & 0 deletions packages/core/interfaces/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,10 @@ export interface UseUser
REGISTER,
LOGIN,
LOGOUT,
ORDERS
> {
user: USER;
orders: ORDERS;
register: REGISTER;
login: LOGIN;
logout: LOGOUT;
Expand Down

0 comments on commit 59eca6a

Please sign in to comment.