packages/graphql: add api/customer tests
This commit is contained in:
parent
cd027f29e6
commit
8d9ed72b91
110
packages/graphql/api/customer.test.ts
Normal file
110
packages/graphql/api/customer.test.ts
Normal file
@ -0,0 +1,110 @@
|
||||
import { beforeEach, describe, expect, it, Mock, vi } from 'vitest';
|
||||
import * as ApolloClient from '../apollo/client';
|
||||
import * as GQL from '../types';
|
||||
import { createOrUpdateUser } from './customer';
|
||||
|
||||
vi.mock('../apollo/client', () => ({
|
||||
getClientWithToken: vi.fn(),
|
||||
}));
|
||||
|
||||
const mockQuery = vi.fn();
|
||||
const mockMutate = vi.fn();
|
||||
|
||||
type ApolloResponse<TData> = {
|
||||
data: TData;
|
||||
};
|
||||
|
||||
describe('api/customer', () => {
|
||||
describe('createOrUpdateUser', () => {
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks();
|
||||
(ApolloClient.getClientWithToken as Mock).mockResolvedValue({
|
||||
query: mockQuery,
|
||||
mutate: mockMutate,
|
||||
});
|
||||
});
|
||||
|
||||
const variables: GQL.CreateUserMutationVariables = {
|
||||
name: 'John Doe',
|
||||
phone: '1234567890',
|
||||
telegramId: '1234567890',
|
||||
};
|
||||
|
||||
const user: GQL.CustomerProfileFragment = {
|
||||
__typename: 'Customer',
|
||||
documentId: '123',
|
||||
name: 'John Doe',
|
||||
phone: '1234567890',
|
||||
telegramId: '1234567890',
|
||||
role: GQL.Enum_Customer_Role.Client,
|
||||
masters: [],
|
||||
};
|
||||
|
||||
it('create new user if it does not exist', async () => {
|
||||
mockQuery.mockResolvedValueOnce({
|
||||
data: {
|
||||
customers: [],
|
||||
},
|
||||
});
|
||||
|
||||
const mockCreateResponse: ApolloResponse<GQL.CreateUserMutation> = {
|
||||
data: { createCustomer: user },
|
||||
};
|
||||
mockMutate.mockResolvedValueOnce(mockCreateResponse);
|
||||
|
||||
const result = await createOrUpdateUser(variables);
|
||||
|
||||
expect(mockQuery).toHaveBeenCalledWith({
|
||||
query: GQL.GetUserByPhoneDocument,
|
||||
variables,
|
||||
});
|
||||
|
||||
expect(mockMutate).toHaveBeenCalledWith({
|
||||
mutation: GQL.CreateUserDocument,
|
||||
variables,
|
||||
});
|
||||
|
||||
expect(mockMutate).not.toHaveBeenCalledWith({
|
||||
mutation: GQL.UpdateCustomerProfileDocument,
|
||||
variables,
|
||||
});
|
||||
|
||||
expect(result).toEqual(mockCreateResponse);
|
||||
});
|
||||
|
||||
it('update user if it exists', async () => {
|
||||
mockQuery.mockResolvedValueOnce({
|
||||
data: {
|
||||
customers: [user],
|
||||
},
|
||||
});
|
||||
|
||||
const mockUpdateResponse: ApolloResponse<GQL.UpdateCustomerProfileMutation> = {
|
||||
data: { updateCustomer: user },
|
||||
};
|
||||
mockMutate.mockResolvedValueOnce(mockUpdateResponse);
|
||||
|
||||
const result = await createOrUpdateUser(variables);
|
||||
|
||||
expect(mockQuery).toHaveBeenCalledWith({
|
||||
query: GQL.GetUserByPhoneDocument,
|
||||
variables,
|
||||
});
|
||||
|
||||
expect(mockMutate).toHaveBeenCalledWith({
|
||||
mutation: GQL.UpdateCustomerProfileDocument,
|
||||
variables: {
|
||||
documentId: user.documentId,
|
||||
data: variables,
|
||||
},
|
||||
});
|
||||
|
||||
expect(mockMutate).not.toHaveBeenCalledWith({
|
||||
mutation: GQL.CreateUserDocument,
|
||||
variables,
|
||||
});
|
||||
|
||||
expect(result).toEqual(mockUpdateResponse);
|
||||
});
|
||||
});
|
||||
});
|
||||
Loading…
x
Reference in New Issue
Block a user