206 lines
5.2 KiB
TypeScript
206 lines
5.2 KiB
TypeScript
import { beforeEach, describe, expect, it, Mock, vi } from 'vitest';
|
|
import * as ApolloClient from '../apollo/client';
|
|
import * as GQL from '../types';
|
|
import { createOrUpdateClient, 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);
|
|
});
|
|
});
|
|
|
|
describe('createOrUpdateClient', () => {
|
|
beforeEach(() => {
|
|
vi.clearAllMocks();
|
|
(ApolloClient.getClientWithToken as Mock).mockResolvedValue({
|
|
query: mockQuery,
|
|
mutate: mockMutate,
|
|
});
|
|
});
|
|
|
|
const variables: GQL.CreateClientMutationVariables = {
|
|
name: 'John Doe',
|
|
phone: '1234567890',
|
|
masterId: '123',
|
|
};
|
|
|
|
const client: GQL.CustomerProfileFragment = {
|
|
__typename: 'Customer',
|
|
documentId: '123',
|
|
name: 'John Doe',
|
|
phone: '1234567890',
|
|
telegramId: '1234567890',
|
|
role: GQL.Enum_Customer_Role.Client,
|
|
masters: [],
|
|
};
|
|
|
|
it('create new client if it does not exist', async () => {
|
|
mockQuery.mockResolvedValueOnce({
|
|
data: {
|
|
customers: [],
|
|
},
|
|
});
|
|
|
|
const mockCreateResponse: ApolloResponse<GQL.CreateUserMutation> = {
|
|
data: { createCustomer: client },
|
|
};
|
|
mockMutate.mockResolvedValueOnce(mockCreateResponse);
|
|
|
|
const result = await createOrUpdateClient(variables);
|
|
|
|
expect(mockQuery).toHaveBeenCalledWith({
|
|
query: GQL.GetUserByPhoneDocument,
|
|
variables,
|
|
});
|
|
|
|
expect(mockMutate).toHaveBeenCalledWith({
|
|
mutation: GQL.CreateClientDocument,
|
|
variables,
|
|
});
|
|
|
|
expect(mockMutate).not.toHaveBeenCalledWith({
|
|
mutation: GQL.UpdateCustomerProfileDocument,
|
|
variables,
|
|
});
|
|
|
|
expect(result).toEqual(mockCreateResponse);
|
|
});
|
|
|
|
it('update client if it exists', async () => {
|
|
mockQuery.mockResolvedValueOnce({
|
|
data: {
|
|
customers: [client],
|
|
},
|
|
});
|
|
|
|
const mockUpdateResponse: ApolloResponse<GQL.UpdateCustomerProfileMutation> = {
|
|
data: { updateCustomer: client },
|
|
};
|
|
mockMutate.mockResolvedValueOnce(mockUpdateResponse);
|
|
|
|
const result = await createOrUpdateClient(variables);
|
|
|
|
expect(mockQuery).toHaveBeenCalledWith({
|
|
query: GQL.GetUserByPhoneDocument,
|
|
variables,
|
|
});
|
|
|
|
expect(mockMutate).toHaveBeenCalledWith({
|
|
mutation: GQL.UpdateCustomerProfileDocument,
|
|
variables: {
|
|
documentId: client.documentId,
|
|
data: {
|
|
masters: [variables.masterId],
|
|
},
|
|
},
|
|
});
|
|
|
|
expect(mockMutate).not.toHaveBeenCalledWith({
|
|
mutation: GQL.CreateClientDocument,
|
|
variables,
|
|
});
|
|
|
|
expect(result).toEqual(mockUpdateResponse);
|
|
});
|
|
});
|
|
});
|