From a8f5f47293aec9ef4ae1f7d16b45404ec21a8874 Mon Sep 17 00:00:00 2001 From: vchikalkin Date: Sun, 12 Jan 2025 15:49:03 +0300 Subject: [PATCH] tests for createOrUpdateClient --- packages/graphql/api/customer.test.ts | 97 ++++++++++++++++++++++++++- 1 file changed, 96 insertions(+), 1 deletion(-) diff --git a/packages/graphql/api/customer.test.ts b/packages/graphql/api/customer.test.ts index 0b85a49..a511fb5 100644 --- a/packages/graphql/api/customer.test.ts +++ b/packages/graphql/api/customer.test.ts @@ -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 = { + 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 = { + 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); + }); + }); });