tests for createOrUpdateClient

This commit is contained in:
vchikalkin 2025-01-12 15:49:03 +03:00
parent 8d9ed72b91
commit a8f5f47293

View File

@ -1,7 +1,7 @@
import { beforeEach, describe, expect, it, Mock, vi } from 'vitest';
import * as ApolloClient from '../apollo/client';
import * as GQL from '../types';
import { createOrUpdateUser } from './customer';
import { createOrUpdateClient, createOrUpdateUser } from './customer';
vi.mock('../apollo/client', () => ({
getClientWithToken: vi.fn(),
@ -107,4 +107,99 @@ describe('api/customer', () => {
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);
});
});
});