From 6e1ee78fd3109bcd79a7578ece201604b4fa8327 Mon Sep 17 00:00:00 2001 From: Cuong Vu Date: Wed, 19 Aug 2020 17:08:58 +0700 Subject: [PATCH] chore: #2362 graphql api layer tests --- packages/graphql-server/jest.config.js | 1 - .../applicants/__tests__/api.test.ts | 184 +++++++++++++++ .../appointments/__tests__/api.test.ts | 105 +++++++++ .../src/resolvers/areas/__tests__/api.test.ts | 100 +++++++++ .../resolvers/companies/__tests__/api.test.ts | 124 +++++++++++ .../configurations/__tests__/api.test.ts | 61 +++++ .../resolvers/contacts/__tests__/api.test.ts | 100 +++++++++ .../src/resolvers/departments/types.graphql | 1 - .../identity-checks/__tests__/api.test.ts | 105 +++++++++ .../src/resolvers/identity-checks/api.ts | 2 +- .../resolvers/landlords/__tests__/api.test.ts | 2 +- .../negotiators/__tests__/api.test.ts | 105 +++++++++ .../resolvers/offers/__tests__/api.test.ts | 100 +++++++++ .../resolvers/offices/__tests__/api.test.ts | 100 +++++++++ .../properties/__tests__/api.test.ts | 100 +++++++++ .../propertyImages/__tests__/api.test.ts | 123 ++++++++++ .../resolvers/tenancies/__tests__/api.test.ts | 210 ++++++++++++++++++ .../__stubs__/create-vendor-relationships.ts | 8 + .../__stubs__/delete-vendor-relationships.ts | 6 + .../vendors/__stubs__/vendor-relationship.ts | 20 ++ .../vendors/__stubs__/vendor-relationships.ts | 28 +++ .../resolvers/vendors/__tests__/api.test.ts | 163 ++++++++++++++ .../src/resolvers/vendors/vendors.d.ts | 36 +-- 23 files changed, 1762 insertions(+), 22 deletions(-) create mode 100644 packages/graphql-server/src/resolvers/applicants/__tests__/api.test.ts create mode 100644 packages/graphql-server/src/resolvers/appointments/__tests__/api.test.ts create mode 100644 packages/graphql-server/src/resolvers/areas/__tests__/api.test.ts create mode 100644 packages/graphql-server/src/resolvers/companies/__tests__/api.test.ts create mode 100644 packages/graphql-server/src/resolvers/configurations/__tests__/api.test.ts create mode 100644 packages/graphql-server/src/resolvers/contacts/__tests__/api.test.ts create mode 100644 packages/graphql-server/src/resolvers/identity-checks/__tests__/api.test.ts create mode 100644 packages/graphql-server/src/resolvers/negotiators/__tests__/api.test.ts create mode 100644 packages/graphql-server/src/resolvers/offers/__tests__/api.test.ts create mode 100644 packages/graphql-server/src/resolvers/offices/__tests__/api.test.ts create mode 100644 packages/graphql-server/src/resolvers/properties/__tests__/api.test.ts create mode 100644 packages/graphql-server/src/resolvers/propertyImages/__tests__/api.test.ts create mode 100644 packages/graphql-server/src/resolvers/tenancies/__tests__/api.test.ts create mode 100644 packages/graphql-server/src/resolvers/vendors/__stubs__/create-vendor-relationships.ts create mode 100644 packages/graphql-server/src/resolvers/vendors/__stubs__/delete-vendor-relationships.ts create mode 100644 packages/graphql-server/src/resolvers/vendors/__stubs__/vendor-relationship.ts create mode 100644 packages/graphql-server/src/resolvers/vendors/__stubs__/vendor-relationships.ts create mode 100644 packages/graphql-server/src/resolvers/vendors/__tests__/api.test.ts diff --git a/packages/graphql-server/jest.config.js b/packages/graphql-server/jest.config.js index d8508d18b2..dfe67bc3f0 100644 --- a/packages/graphql-server/jest.config.js +++ b/packages/graphql-server/jest.config.js @@ -7,7 +7,6 @@ module.exports = { testPathIgnorePatterns: ['/src/tests/'], coveragePathIgnorePatterns: [ '[/\\\\](node_modules|src/types|src/tests|src/scripts|src/__stubs__|dist)[/\\\\]', - 'api.ts', '.d.ts', 'index.ts', ], diff --git a/packages/graphql-server/src/resolvers/applicants/__tests__/api.test.ts b/packages/graphql-server/src/resolvers/applicants/__tests__/api.test.ts new file mode 100644 index 0000000000..6e152dddc3 --- /dev/null +++ b/packages/graphql-server/src/resolvers/applicants/__tests__/api.test.ts @@ -0,0 +1,184 @@ +import { mockContext } from '../../../__stubs__/context' +import { + callGetApplicantsAPI, + callGetApplicantByIdAPI, + callGetApplicantRelationshipsAPI, + callGetApplicantRelationshipByIdAPI, + callCreateApplicantAPI, + callUpdateApplicantAPI, + callCreateApplicantRelationshipAPI, + callDeleteApplicantRelationshipAPI, +} from '../api' +import { createPlatformAxiosInstance } from '../../../utils/axios-instances' +import { applicantMock } from '../__stubs__/applicant' +import { applicantsMock } from '../__stubs__/applicants' +import { relationshipMock } from '../__stubs__/relationship' +import { relationshipsMock } from '../__stubs__/relationships' +import { createApplicantArgsMock } from '../__stubs__/create-applicant' +import { createRelationshipsArgs } from '../__stubs__/create-relationships' +import { updateApplicantArgsMock } from '../__stubs__/update-applicant' +import { deleteRelationshipMockArgs } from '../__stubs__/delete-relatationships' +import { getIdFromCreateHeaders } from '../../../utils/get-id-from-create-headers' + +jest.mock('../../../utils/get-id-from-create-headers', () => ({ + getIdFromCreateHeaders: jest.fn(), +})) + +jest.mock('../../../utils/handle-error', () => ({ + handleError: jest.fn(() => Promise.resolve('caught error')), +})) +jest.mock('../../../logger') +jest.mock('../../../utils/axios-instances', () => ({ + createPlatformAxiosInstance: jest.fn(() => ({ + get: jest.fn(), + post: jest.fn(), + patch: jest.fn(), + put: jest.fn(), + delete: jest.fn(), + })), +})) + +describe('callGetApplicantsAPI', () => { + it('should work correctly', async () => { + ;(createPlatformAxiosInstance as jest.Mocked).mockReturnValueOnce({ + get: jest.fn(() => Promise.resolve({ data: applicantsMock })), + }) + const args = { pageSize: 1 } + const result = await callGetApplicantsAPI(args, mockContext) + expect(result).toEqual(applicantsMock) + }) + it('should catch error correctly', async () => { + ;(createPlatformAxiosInstance as jest.Mocked).mockReturnValueOnce({ + get: jest.fn(() => Promise.reject('error caught')), + }) + const args = { pageSize: 1 } + const result = await callGetApplicantsAPI(args, mockContext) + expect(result).toEqual('caught error') + }) +}) + +describe('callGetApplicantByIdAPI', () => { + it('should work correctly', async () => { + ;(createPlatformAxiosInstance as jest.Mocked).mockReturnValueOnce({ + get: jest.fn(() => Promise.resolve({ data: applicantMock })), + }) + const args = { id: applicantMock.id } + const result = await callGetApplicantByIdAPI(args, mockContext) + expect(result).toEqual(applicantMock) + }) + + it('should catch error correctly', async () => { + ;(createPlatformAxiosInstance as jest.Mocked).mockReturnValueOnce({ + get: jest.fn(() => Promise.reject('error caught')), + }) + const args = { id: applicantMock.id } + const result = await callGetApplicantByIdAPI(args, mockContext) + expect(result).toEqual('caught error') + }) +}) + +describe('callGetApplicantRelationshipByIdAPI', () => { + it('should work correctly', async () => { + ;(createPlatformAxiosInstance as jest.Mocked).mockReturnValueOnce({ + get: jest.fn(() => Promise.resolve({ data: relationshipMock })), + }) + const args = { id: 'id', relationshipId: 'relationshipId' } + const result = await callGetApplicantRelationshipByIdAPI(args, mockContext) + expect(result).toEqual(relationshipMock) + }) + + it('should catch error correctly', async () => { + ;(createPlatformAxiosInstance as jest.Mocked).mockReturnValueOnce({ + get: jest.fn(() => Promise.reject('error caught')), + }) + const args = { id: 'id', relationshipId: 'relationshipId' } + const result = await callGetApplicantRelationshipByIdAPI(args, mockContext) + expect(result).toEqual('caught error') + }) +}) + +describe('callGetApplicantRelationshipsAPI', () => { + it('should work correctly', async () => { + ;(createPlatformAxiosInstance as jest.Mocked).mockReturnValueOnce({ + get: jest.fn(() => Promise.resolve({ data: relationshipsMock })), + }) + const args = { pageSize: 1, id: 'id' } + const result = await callGetApplicantRelationshipsAPI(args, mockContext) + expect(result).toEqual(relationshipsMock) + }) + it('should catch error correctly', async () => { + ;(createPlatformAxiosInstance as jest.Mocked).mockReturnValueOnce({ + get: jest.fn(() => Promise.reject('error caught')), + }) + const args = { pageSize: 1, id: 'id' } + const result = await callGetApplicantRelationshipsAPI(args, mockContext) + expect(result).toEqual('caught error') + }) +}) + +describe('callCreateApplicantAPI', () => { + it('should work correctly', async () => { + ;(createPlatformAxiosInstance as jest.Mocked).mockReturnValueOnce({ + post: jest.fn(() => Promise.resolve({ headers: 'header' })), + get: jest.fn(() => Promise.resolve({ data: applicantMock })), + }) + ;(getIdFromCreateHeaders as jest.Mocked).mockReturnValueOnce(applicantMock.id) + await callCreateApplicantAPI(createApplicantArgsMock, mockContext) + expect(getIdFromCreateHeaders).toHaveBeenCalledWith({ headers: 'header' }) + }) + it('should catch error correctly', async () => { + ;(createPlatformAxiosInstance as jest.Mocked).mockReturnValueOnce({ + post: jest.fn(() => Promise.reject('error caught')), + }) + const result = await callCreateApplicantAPI(createApplicantArgsMock, mockContext) + expect(result).toEqual('caught error') + }) +}) + +describe('callCreateApplicantRelationshipAPI', () => { + it('should work correctly', async () => { + ;(createPlatformAxiosInstance as jest.Mocked).mockReturnValueOnce({ + post: jest.fn(() => Promise.resolve({ headers: 'header' })), + }) + await callCreateApplicantRelationshipAPI(createRelationshipsArgs, mockContext) + }) + it('should catch error correctly', async () => { + ;(createPlatformAxiosInstance as jest.Mocked).mockReturnValueOnce({ + post: jest.fn(() => Promise.reject('error caught')), + }) + const result = await callCreateApplicantRelationshipAPI(createRelationshipsArgs, mockContext) + expect(result).toEqual('caught error') + }) +}) + +describe('callUpdateApplicantAPI', () => { + it('should work correctly', async () => { + ;(createPlatformAxiosInstance as jest.Mocked).mockReturnValueOnce({ + patch: jest.fn(() => Promise.resolve({ headers: 'header' })), + }) + await callUpdateApplicantAPI(updateApplicantArgsMock, mockContext) + }) + it('should catch error correctly', async () => { + ;(createPlatformAxiosInstance as jest.Mocked).mockReturnValueOnce({ + patch: jest.fn(() => Promise.reject('error caught')), + }) + const result = await callUpdateApplicantAPI(updateApplicantArgsMock, mockContext) + expect(result).toEqual('caught error') + }) +}) + +describe('callDeleteApplicantRelationshipAPI', () => { + it('should work correctly', async () => { + ;(createPlatformAxiosInstance as jest.Mocked).mockReturnValueOnce({ + delete: jest.fn(() => Promise.resolve({ headers: 'header' })), + }) + await callDeleteApplicantRelationshipAPI(deleteRelationshipMockArgs, mockContext) + }) + it('should catch error correctly', async () => { + ;(createPlatformAxiosInstance as jest.Mocked).mockReturnValueOnce({ + delete: jest.fn(() => Promise.reject('error caught')), + }) + const result = await callDeleteApplicantRelationshipAPI(deleteRelationshipMockArgs, mockContext) + expect(result).toEqual('caught error') + }) +}) diff --git a/packages/graphql-server/src/resolvers/appointments/__tests__/api.test.ts b/packages/graphql-server/src/resolvers/appointments/__tests__/api.test.ts new file mode 100644 index 0000000000..e04a6d9835 --- /dev/null +++ b/packages/graphql-server/src/resolvers/appointments/__tests__/api.test.ts @@ -0,0 +1,105 @@ +import { mockContext } from '../../../__stubs__/context' +import { + callGetAppointmentsAPI, + callCreateAppointmentAPI, + callUpdateAppointmentAPI, + callGetAppointmentByIdAPI, +} from '../api' +import { createPlatformAxiosInstance } from '../../../utils/axios-instances' +import { appointmentMock } from '../__stubs__/appointment' +import { appointmentsMock } from '../__stubs__/appointments' +import { createAppointmentArgsMock } from '../__stubs__/create-appointment' +import { updateAppointmentArgsMock } from '../__stubs__/update-appointment' +import { getIdFromCreateHeaders } from '../../../utils/get-id-from-create-headers' + +jest.mock('../../../utils/get-id-from-create-headers', () => ({ + getIdFromCreateHeaders: jest.fn(), +})) + +jest.mock('../../../utils/handle-error', () => ({ + handleError: jest.fn(() => Promise.resolve('caught error')), +})) +jest.mock('../../../logger') +jest.mock('../../../utils/axios-instances', () => ({ + createPlatformAxiosInstance: jest.fn(() => ({ + get: jest.fn(), + post: jest.fn(), + patch: jest.fn(), + put: jest.fn(), + delete: jest.fn(), + })), +})) + +describe('callGetAppointmentsAPI', () => { + it('should work correctly', async () => { + ;(createPlatformAxiosInstance as jest.Mocked).mockReturnValueOnce({ + get: jest.fn(() => Promise.resolve({ data: appointmentsMock })), + }) + const args = { pageSize: 1 } + const result = await callGetAppointmentsAPI(args, mockContext) + expect(result).toEqual(appointmentsMock) + }) + it('should catch error correctly', async () => { + ;(createPlatformAxiosInstance as jest.Mocked).mockReturnValueOnce({ + get: jest.fn(() => Promise.reject('error caught')), + }) + const args = { pageSize: 1 } + const result = await callGetAppointmentsAPI(args, mockContext) + expect(result).toEqual('caught error') + }) +}) + +describe('callGetAppointmentByIdAPI', () => { + it('should work correctly', async () => { + ;(createPlatformAxiosInstance as jest.Mocked).mockReturnValueOnce({ + get: jest.fn(() => Promise.resolve({ data: appointmentMock })), + }) + const args = { id: appointmentMock.id } + const result = await callGetAppointmentByIdAPI(args, mockContext) + expect(result).toEqual(appointmentMock) + }) + + it('should catch error correctly', async () => { + ;(createPlatformAxiosInstance as jest.Mocked).mockReturnValueOnce({ + get: jest.fn(() => Promise.reject('error caught')), + }) + const args = { id: appointmentMock.id } + const result = await callGetAppointmentByIdAPI(args, mockContext) + expect(result).toEqual('caught error') + }) +}) + +describe('callCreateAppointmentAPI', () => { + it('should work correctly', async () => { + ;(createPlatformAxiosInstance as jest.Mocked).mockReturnValueOnce({ + post: jest.fn(() => Promise.resolve({ headers: 'header' })), + get: jest.fn(() => Promise.resolve({ data: appointmentMock })), + }) + ;(getIdFromCreateHeaders as jest.Mocked).mockReturnValueOnce(appointmentMock.id) + await callCreateAppointmentAPI(createAppointmentArgsMock, mockContext) + expect(getIdFromCreateHeaders).toHaveBeenCalledWith({ headers: 'header' }) + }) + it('should catch error correctly', async () => { + ;(createPlatformAxiosInstance as jest.Mocked).mockReturnValueOnce({ + post: jest.fn(() => Promise.reject('error caught')), + }) + const result = await callCreateAppointmentAPI(createAppointmentArgsMock, mockContext) + expect(result).toEqual('caught error') + }) +}) + +describe('callUpdateAppointmentAPI', () => { + it('should work correctly', async () => { + ;(createPlatformAxiosInstance as jest.Mocked).mockReturnValueOnce({ + patch: jest.fn(() => Promise.resolve({ headers: 'header' })), + }) + await callUpdateAppointmentAPI(updateAppointmentArgsMock, mockContext) + }) + it('should catch error correctly', async () => { + ;(createPlatformAxiosInstance as jest.Mocked).mockReturnValueOnce({ + patch: jest.fn(() => Promise.reject('error caught')), + }) + const result = await callUpdateAppointmentAPI(updateAppointmentArgsMock, mockContext) + expect(result).toEqual('caught error') + }) +}) diff --git a/packages/graphql-server/src/resolvers/areas/__tests__/api.test.ts b/packages/graphql-server/src/resolvers/areas/__tests__/api.test.ts new file mode 100644 index 0000000000..25b390050c --- /dev/null +++ b/packages/graphql-server/src/resolvers/areas/__tests__/api.test.ts @@ -0,0 +1,100 @@ +import { mockContext } from '../../../__stubs__/context' +import { callGetAreasAPI, callCreateAreaAPI, callUpdateAreaAPI, callGetAreaByIdAPI } from '../api' +import { createPlatformAxiosInstance } from '../../../utils/axios-instances' +import { areaMock } from '../__stubs__/area' +import { areasMock } from '../__stubs__/areas' +import { createAreaArgsMock } from '../__stubs__/create-area' +import { updateAreaArgsMock } from '../__stubs__/update-area' +import { getIdFromCreateHeaders } from '../../../utils/get-id-from-create-headers' + +jest.mock('../../../utils/get-id-from-create-headers', () => ({ + getIdFromCreateHeaders: jest.fn(), +})) + +jest.mock('../../../utils/handle-error', () => ({ + handleError: jest.fn(() => Promise.resolve('caught error')), +})) +jest.mock('../../../logger') +jest.mock('../../../utils/axios-instances', () => ({ + createPlatformAxiosInstance: jest.fn(() => ({ + get: jest.fn(), + post: jest.fn(), + patch: jest.fn(), + put: jest.fn(), + delete: jest.fn(), + })), +})) + +describe('callGetAreasAPI', () => { + it('should work correctly', async () => { + ;(createPlatformAxiosInstance as jest.Mocked).mockReturnValueOnce({ + get: jest.fn(() => Promise.resolve({ data: areasMock })), + }) + const args = { pageSize: 1 } + const result = await callGetAreasAPI(args, mockContext) + expect(result).toEqual(areasMock) + }) + it('should catch error correctly', async () => { + ;(createPlatformAxiosInstance as jest.Mocked).mockReturnValueOnce({ + get: jest.fn(() => Promise.reject('error caught')), + }) + const args = { pageSize: 1 } + const result = await callGetAreasAPI(args, mockContext) + expect(result).toEqual('caught error') + }) +}) + +describe('callGetAreaByIdAPI', () => { + it('should work correctly', async () => { + ;(createPlatformAxiosInstance as jest.Mocked).mockReturnValueOnce({ + get: jest.fn(() => Promise.resolve({ data: areaMock })), + }) + const args = { id: areaMock.id } + const result = await callGetAreaByIdAPI(args, mockContext) + expect(result).toEqual(areaMock) + }) + + it('should catch error correctly', async () => { + ;(createPlatformAxiosInstance as jest.Mocked).mockReturnValueOnce({ + get: jest.fn(() => Promise.reject('error caught')), + }) + const args = { id: areaMock.id } + const result = await callGetAreaByIdAPI(args, mockContext) + expect(result).toEqual('caught error') + }) +}) + +describe('callCreateAreaAPI', () => { + it('should work correctly', async () => { + ;(createPlatformAxiosInstance as jest.Mocked).mockReturnValueOnce({ + post: jest.fn(() => Promise.resolve({ headers: 'header' })), + get: jest.fn(() => Promise.resolve({ data: areaMock })), + }) + ;(getIdFromCreateHeaders as jest.Mocked).mockReturnValueOnce(areaMock.id) + await callCreateAreaAPI(createAreaArgsMock, mockContext) + expect(getIdFromCreateHeaders).toHaveBeenCalledWith({ headers: 'header' }) + }) + it('should catch error correctly', async () => { + ;(createPlatformAxiosInstance as jest.Mocked).mockReturnValueOnce({ + post: jest.fn(() => Promise.reject('error caught')), + }) + const result = await callCreateAreaAPI(createAreaArgsMock, mockContext) + expect(result).toEqual('caught error') + }) +}) + +describe('callUpdateAreaAPI', () => { + it('should work correctly', async () => { + ;(createPlatformAxiosInstance as jest.Mocked).mockReturnValueOnce({ + patch: jest.fn(() => Promise.resolve({ headers: 'header' })), + }) + await callUpdateAreaAPI(updateAreaArgsMock, mockContext) + }) + it('should catch error correctly', async () => { + ;(createPlatformAxiosInstance as jest.Mocked).mockReturnValueOnce({ + patch: jest.fn(() => Promise.reject('error caught')), + }) + const result = await callUpdateAreaAPI(updateAreaArgsMock, mockContext) + expect(result).toEqual('caught error') + }) +}) diff --git a/packages/graphql-server/src/resolvers/companies/__tests__/api.test.ts b/packages/graphql-server/src/resolvers/companies/__tests__/api.test.ts new file mode 100644 index 0000000000..702755724a --- /dev/null +++ b/packages/graphql-server/src/resolvers/companies/__tests__/api.test.ts @@ -0,0 +1,124 @@ +import { mockContext } from '../../../__stubs__/context' +import { + callGetCompaniesAPI, + callCreateCompanyAPI, + callUpdateCompanyAPI, + callGetCompanyByIdAPI, + callGetCompanyRolesAPI, +} from '../api' +import { createPlatformAxiosInstance } from '../../../utils/axios-instances' +import { companyMock } from '../__stubs__/company' +import { companiesMock } from '../__stubs__/companies' +import { companyRolesMock } from '../__stubs__/company-roles' +import { createCompanyArgsMock } from '../__stubs__/create-company' +import { updateCompanyArgsMock } from '../__stubs__/update-company' +import { getIdFromCreateHeaders } from '../../../utils/get-id-from-create-headers' + +jest.mock('../../../utils/get-id-from-create-headers', () => ({ + getIdFromCreateHeaders: jest.fn(), +})) + +jest.mock('../../../utils/handle-error', () => ({ + handleError: jest.fn(() => Promise.resolve('caught error')), +})) +jest.mock('../../../logger') +jest.mock('../../../utils/axios-instances', () => ({ + createPlatformAxiosInstance: jest.fn(() => ({ + get: jest.fn(), + post: jest.fn(), + patch: jest.fn(), + put: jest.fn(), + delete: jest.fn(), + })), +})) + +describe('callGetCompaniesAPI', () => { + it('should work correctly', async () => { + ;(createPlatformAxiosInstance as jest.Mocked).mockReturnValueOnce({ + get: jest.fn(() => Promise.resolve({ data: companiesMock })), + }) + const args = { pageSize: 1 } + const result = await callGetCompaniesAPI(args, mockContext) + expect(result).toEqual(companiesMock) + }) + it('should catch error correctly', async () => { + ;(createPlatformAxiosInstance as jest.Mocked).mockReturnValueOnce({ + get: jest.fn(() => Promise.reject('error caught')), + }) + const args = { pageSize: 1 } + const result = await callGetCompaniesAPI(args, mockContext) + expect(result).toEqual('caught error') + }) +}) + +describe('callGetCompanyByIdAPI', () => { + it('should work correctly', async () => { + ;(createPlatformAxiosInstance as jest.Mocked).mockReturnValueOnce({ + get: jest.fn(() => Promise.resolve({ data: companyMock })), + }) + const args = { id: companyMock.id } + const result = await callGetCompanyByIdAPI(args, mockContext) + expect(result).toEqual(companyMock) + }) + + it('should catch error correctly', async () => { + ;(createPlatformAxiosInstance as jest.Mocked).mockReturnValueOnce({ + get: jest.fn(() => Promise.reject('error caught')), + }) + const args = { id: companyMock.id } + const result = await callGetCompanyByIdAPI(args, mockContext) + expect(result).toEqual('caught error') + }) +}) + +describe('callGetCompanyRolesAPI', () => { + it('should work correctly', async () => { + ;(createPlatformAxiosInstance as jest.Mocked).mockReturnValueOnce({ + get: jest.fn(() => Promise.resolve({ data: companyRolesMock })), + }) + const args = { pageSize: 1, id: ['id'] } + const result = await callGetCompanyRolesAPI(args, mockContext) + expect(result).toEqual(companyRolesMock) + }) + it('should catch error correctly', async () => { + ;(createPlatformAxiosInstance as jest.Mocked).mockReturnValueOnce({ + get: jest.fn(() => Promise.reject('error caught')), + }) + const args = { pageSize: 1, id: ['id'] } + const result = await callGetCompanyRolesAPI(args, mockContext) + expect(result).toEqual('caught error') + }) +}) + +describe('callCreateCompanyAPI', () => { + it('should work correctly', async () => { + ;(createPlatformAxiosInstance as jest.Mocked).mockReturnValueOnce({ + post: jest.fn(() => Promise.resolve({ headers: 'header' })), + }) + await callCreateCompanyAPI(createCompanyArgsMock, mockContext) + expect(getIdFromCreateHeaders).toHaveBeenCalledWith({ headers: 'header' }) + }) + it('should catch error correctly', async () => { + ;(createPlatformAxiosInstance as jest.Mocked).mockReturnValueOnce({ + post: jest.fn(() => Promise.reject('error caught')), + }) + const result = await callCreateCompanyAPI(createCompanyArgsMock, mockContext) + expect(result).toEqual('caught error') + }) +}) + +describe('callUpdateCompanyAPI', () => { + it('should work correctly', async () => { + ;(createPlatformAxiosInstance as jest.Mocked).mockReturnValueOnce({ + patch: jest.fn(() => Promise.resolve({ headers: 'header' })), + }) + await callUpdateCompanyAPI(updateCompanyArgsMock, mockContext) + }) + it('should catch error correctly', async () => { + ;(createPlatformAxiosInstance as jest.Mocked).mockReturnValueOnce({ + patch: jest.fn(() => Promise.reject('error caught')), + }) + const result = await callUpdateCompanyAPI(updateCompanyArgsMock, mockContext) + expect(result).toEqual('caught error') + }) +}) diff --git a/packages/graphql-server/src/resolvers/configurations/__tests__/api.test.ts b/packages/graphql-server/src/resolvers/configurations/__tests__/api.test.ts new file mode 100644 index 0000000000..f062fc4c1f --- /dev/null +++ b/packages/graphql-server/src/resolvers/configurations/__tests__/api.test.ts @@ -0,0 +1,61 @@ +import { mockContext } from '../../../__stubs__/context' +import { callGetConfigurationsByTypeApi, callGetConfigurationsByTypeAndIdApi } from '../api' +import { createPlatformAxiosInstance } from '../../../utils/axios-instances' +import { appointmentTypeMock, appointmentTypesMock } from '../__stubs__/appointmentTypes' +import { ConfigurationType } from '../configurations' + +jest.mock('../../../utils/get-id-from-create-headers', () => ({ + getIdFromCreateHeaders: jest.fn(), +})) + +jest.mock('../../../utils/handle-error', () => ({ + handleError: jest.fn(() => Promise.resolve('caught error')), +})) +jest.mock('../../../logger') +jest.mock('../../../utils/axios-instances', () => ({ + createPlatformAxiosInstance: jest.fn(() => ({ + get: jest.fn(), + post: jest.fn(), + patch: jest.fn(), + put: jest.fn(), + delete: jest.fn(), + })), +})) + +describe('callGetConfigurationsByTypeAndIdApi', () => { + it('should work correctly', async () => { + ;(createPlatformAxiosInstance as jest.Mocked).mockReturnValueOnce({ + get: jest.fn(() => Promise.resolve({ data: appointmentTypeMock })), + }) + const args = { id: 'id1', type: '​​appointmentTypes' as ConfigurationType } + const result = await callGetConfigurationsByTypeAndIdApi(args, mockContext) + expect(result).toEqual(appointmentTypeMock) + }) + it('should catch error correctly', async () => { + ;(createPlatformAxiosInstance as jest.Mocked).mockReturnValueOnce({ + get: jest.fn(() => Promise.reject('error caught')), + }) + const args = { id: 'id1', type: '​​appointmentTypes' as ConfigurationType } + const result = await callGetConfigurationsByTypeAndIdApi(args, mockContext) + expect(result).toEqual('caught error') + }) +}) + +describe('callGetConfigurationsByTypeApi', () => { + it('should work correctly', async () => { + ;(createPlatformAxiosInstance as jest.Mocked).mockReturnValueOnce({ + get: jest.fn(() => Promise.resolve({ data: appointmentTypesMock })), + }) + const args = { type: '​​appointmentTypes' as ConfigurationType } + const result = await callGetConfigurationsByTypeApi(args, mockContext) + expect(result).toEqual(appointmentTypesMock) + }) + it('should catch error correctly', async () => { + ;(createPlatformAxiosInstance as jest.Mocked).mockReturnValueOnce({ + get: jest.fn(() => Promise.reject('error caught')), + }) + const args = { type: '​​appointmentTypes' as ConfigurationType } + const result = await callGetConfigurationsByTypeApi(args, mockContext) + expect(result).toEqual('caught error') + }) +}) diff --git a/packages/graphql-server/src/resolvers/contacts/__tests__/api.test.ts b/packages/graphql-server/src/resolvers/contacts/__tests__/api.test.ts new file mode 100644 index 0000000000..a92b54eab3 --- /dev/null +++ b/packages/graphql-server/src/resolvers/contacts/__tests__/api.test.ts @@ -0,0 +1,100 @@ +import { mockContext } from '../../../__stubs__/context' +import { callGetContactByIdAPI, callUpdateContactAPI, callCreateContactAPI, callGetContactsAPI } from '../api' +import { createPlatformAxiosInstance } from '../../../utils/axios-instances' +import { contactMock } from '../__stubs__/contact' +import { contactsMock } from '../__stubs__/contacts' +import { createContactArgsMock } from '../__stubs__/create-contact' +import { updateContactArgsMock } from '../__stubs__/update-contact' +import { getIdFromCreateHeaders } from '../../../utils/get-id-from-create-headers' + +jest.mock('../../../utils/get-id-from-create-headers', () => ({ + getIdFromCreateHeaders: jest.fn(), +})) + +jest.mock('../../../utils/handle-error', () => ({ + handleError: jest.fn(() => Promise.resolve('caught error')), +})) +jest.mock('../../../logger') +jest.mock('../../../utils/axios-instances', () => ({ + createPlatformAxiosInstance: jest.fn(() => ({ + get: jest.fn(), + post: jest.fn(), + patch: jest.fn(), + put: jest.fn(), + delete: jest.fn(), + })), +})) + +describe('callGetContactsAPI', () => { + it('should work correctly', async () => { + ;(createPlatformAxiosInstance as jest.Mocked).mockReturnValueOnce({ + get: jest.fn(() => Promise.resolve({ data: contactsMock })), + }) + const args = { pageSize: 1 } + const result = await callGetContactsAPI(args, mockContext) + expect(result).toEqual(contactsMock) + }) + it('should catch error correctly', async () => { + ;(createPlatformAxiosInstance as jest.Mocked).mockReturnValueOnce({ + get: jest.fn(() => Promise.reject('error caught')), + }) + const args = { pageSize: 1 } + const result = await callGetContactsAPI(args, mockContext) + expect(result).toEqual('caught error') + }) +}) + +describe('callGetContactByIdAPI', () => { + it('should work correctly', async () => { + ;(createPlatformAxiosInstance as jest.Mocked).mockReturnValueOnce({ + get: jest.fn(() => Promise.resolve({ data: contactMock })), + }) + const args = { id: contactMock.id } + const result = await callGetContactByIdAPI(args, mockContext) + expect(result).toEqual(contactMock) + }) + + it('should catch error correctly', async () => { + ;(createPlatformAxiosInstance as jest.Mocked).mockReturnValueOnce({ + get: jest.fn(() => Promise.reject('error caught')), + }) + const args = { id: contactMock.id } + const result = await callGetContactByIdAPI(args, mockContext) + expect(result).toEqual('caught error') + }) +}) + +describe('callCreateContactAPI', () => { + it('should work correctly', async () => { + ;(createPlatformAxiosInstance as jest.Mocked).mockReturnValueOnce({ + post: jest.fn(() => Promise.resolve({ headers: 'header' })), + get: jest.fn(() => Promise.resolve({ data: contactMock })), + }) + ;(getIdFromCreateHeaders as jest.Mocked).mockReturnValueOnce(contactMock.id) + await callCreateContactAPI(createContactArgsMock, mockContext) + expect(getIdFromCreateHeaders).toHaveBeenCalledWith({ headers: 'header' }) + }) + it('should catch error correctly', async () => { + ;(createPlatformAxiosInstance as jest.Mocked).mockReturnValueOnce({ + post: jest.fn(() => Promise.reject('error caught')), + }) + const result = await callCreateContactAPI(createContactArgsMock, mockContext) + expect(result).toEqual('caught error') + }) +}) + +describe('callUpdateContactAPI', () => { + it('should work correctly', async () => { + ;(createPlatformAxiosInstance as jest.Mocked).mockReturnValueOnce({ + patch: jest.fn(() => Promise.resolve({ headers: 'header' })), + }) + await callUpdateContactAPI(updateContactArgsMock, mockContext) + }) + it('should catch error correctly', async () => { + ;(createPlatformAxiosInstance as jest.Mocked).mockReturnValueOnce({ + patch: jest.fn(() => Promise.reject('error caught')), + }) + const result = await callUpdateContactAPI(updateContactArgsMock, mockContext) + expect(result).toEqual('caught error') + }) +}) diff --git a/packages/graphql-server/src/resolvers/departments/types.graphql b/packages/graphql-server/src/resolvers/departments/types.graphql index c7f16119b1..9c9f96f9df 100644 --- a/packages/graphql-server/src/resolvers/departments/types.graphql +++ b/packages/graphql-server/src/resolvers/departments/types.graphql @@ -30,4 +30,3 @@ type Query { GetDepartments(pageSize: Int, pageNumber: Int, id: [String!], name: String): PagedResultDepartmentModel_! GetDepartmentById(id: String!): DepartmentModel! } - diff --git a/packages/graphql-server/src/resolvers/identity-checks/__tests__/api.test.ts b/packages/graphql-server/src/resolvers/identity-checks/__tests__/api.test.ts new file mode 100644 index 0000000000..073c1d4abd --- /dev/null +++ b/packages/graphql-server/src/resolvers/identity-checks/__tests__/api.test.ts @@ -0,0 +1,105 @@ +import { mockContext } from '../../../__stubs__/context' +import { + callGetIdentityChecksAPI, + callCreateIdentityCheckAPI, + callUpdateIdentityCheckAPI, + callGetIdentityCheckByIdAPI, +} from '../api' +import { createPlatformAxiosInstance } from '../../../utils/axios-instances' +import { identityCheckMock } from '../__stubs__/identity-check' +import { identityChecksMock } from '../__stubs__/identity-checks' +import { createIdentityCheckArgsMock } from '../__stubs__/create-identity-check' +import { updateIdentityCheckArgsMock } from '../__stubs__/update-identity-check' +import { getIdFromCreateHeaders } from '../../../utils/get-id-from-create-headers' + +jest.mock('../../../utils/get-id-from-create-headers', () => ({ + getIdFromCreateHeaders: jest.fn(), +})) + +jest.mock('../../../utils/handle-error', () => ({ + handleError: jest.fn(() => Promise.resolve('caught error')), +})) +jest.mock('../../../logger') +jest.mock('../../../utils/axios-instances', () => ({ + createPlatformAxiosInstance: jest.fn(() => ({ + get: jest.fn(), + post: jest.fn(), + patch: jest.fn(), + put: jest.fn(), + delete: jest.fn(), + })), +})) + +describe('callGetIdentityChecksAPI', () => { + it('should work correctly', async () => { + ;(createPlatformAxiosInstance as jest.Mocked).mockReturnValueOnce({ + get: jest.fn(() => Promise.resolve({ data: identityChecksMock })), + }) + const args = { pageSize: 1 } + const result = await callGetIdentityChecksAPI(args, mockContext) + expect(result).toEqual(identityChecksMock) + }) + it('should catch error correctly', async () => { + ;(createPlatformAxiosInstance as jest.Mocked).mockReturnValueOnce({ + get: jest.fn(() => Promise.reject('error caught')), + }) + const args = { pageSize: 1 } + const result = await callGetIdentityChecksAPI(args, mockContext) + expect(result).toEqual('caught error') + }) +}) + +describe('callGetIdentityCheckByIdAPI', () => { + it('should work correctly', async () => { + ;(createPlatformAxiosInstance as jest.Mocked).mockReturnValueOnce({ + get: jest.fn(() => Promise.resolve({ data: identityCheckMock })), + }) + const args = { id: identityCheckMock.id } + const result = await callGetIdentityCheckByIdAPI(args, mockContext) + expect(result).toEqual(identityCheckMock) + }) + + it('should catch error correctly', async () => { + ;(createPlatformAxiosInstance as jest.Mocked).mockReturnValueOnce({ + get: jest.fn(() => Promise.reject('error caught')), + }) + const args = { id: identityCheckMock.id } + const result = await callGetIdentityCheckByIdAPI(args, mockContext) + expect(result).toEqual('caught error') + }) +}) + +describe('callCreateIdentityCheckAPI', () => { + it('should work correctly', async () => { + ;(createPlatformAxiosInstance as jest.Mocked).mockReturnValueOnce({ + post: jest.fn(() => Promise.resolve({ headers: 'header' })), + get: jest.fn(() => Promise.resolve({ data: identityCheckMock })), + }) + ;(getIdFromCreateHeaders as jest.Mocked).mockReturnValueOnce(identityCheckMock.id) + await callCreateIdentityCheckAPI(createIdentityCheckArgsMock, mockContext) + expect(getIdFromCreateHeaders).toHaveBeenCalledWith({ headers: 'header' }) + }) + it('should catch error correctly', async () => { + ;(createPlatformAxiosInstance as jest.Mocked).mockReturnValueOnce({ + post: jest.fn(() => Promise.reject('error caught')), + }) + const result = await callCreateIdentityCheckAPI(createIdentityCheckArgsMock, mockContext) + expect(result).toEqual('caught error') + }) +}) + +describe('callUpdateIdentityCheckAPI', () => { + it('should work correctly', async () => { + ;(createPlatformAxiosInstance as jest.Mocked).mockReturnValueOnce({ + patch: jest.fn(() => Promise.resolve({ headers: 'header' })), + }) + await callUpdateIdentityCheckAPI(updateIdentityCheckArgsMock, mockContext) + }) + it('should catch error correctly', async () => { + ;(createPlatformAxiosInstance as jest.Mocked).mockReturnValueOnce({ + patch: jest.fn(() => Promise.reject('error caught')), + }) + const result = await callUpdateIdentityCheckAPI(updateIdentityCheckArgsMock, mockContext) + expect(result).toEqual('caught error') + }) +}) diff --git a/packages/graphql-server/src/resolvers/identity-checks/api.ts b/packages/graphql-server/src/resolvers/identity-checks/api.ts index 461cd1135c..4c0dffa6b0 100644 --- a/packages/graphql-server/src/resolvers/identity-checks/api.ts +++ b/packages/graphql-server/src/resolvers/identity-checks/api.ts @@ -14,7 +14,7 @@ import { import errors from '../../errors' import { URLS } from '../../constants/api' import { createPlatformAxiosInstance } from '../../utils/axios-instances' -import handleError from '../../utils/handle-error' +import { handleError } from '../../utils/handle-error' import { getIdFromCreateHeaders } from '../../utils/get-id-from-create-headers' export const callGetIdentityCheckByIdAPI = async ( diff --git a/packages/graphql-server/src/resolvers/landlords/__tests__/api.test.ts b/packages/graphql-server/src/resolvers/landlords/__tests__/api.test.ts index e038300d31..1d4436f9ca 100644 --- a/packages/graphql-server/src/resolvers/landlords/__tests__/api.test.ts +++ b/packages/graphql-server/src/resolvers/landlords/__tests__/api.test.ts @@ -155,7 +155,7 @@ describe('callCreateLandlordRelationshipAPI', () => { }) }) -describe('callUpdateLandlordRelationshipAPI', () => { +describe('callUpdateLandlordAPI', () => { it('should work correctly', async () => { ;(createPlatformAxiosInstance as jest.Mocked).mockReturnValueOnce({ patch: jest.fn(() => Promise.resolve({ headers: 'header' })), diff --git a/packages/graphql-server/src/resolvers/negotiators/__tests__/api.test.ts b/packages/graphql-server/src/resolvers/negotiators/__tests__/api.test.ts new file mode 100644 index 0000000000..2f121118a9 --- /dev/null +++ b/packages/graphql-server/src/resolvers/negotiators/__tests__/api.test.ts @@ -0,0 +1,105 @@ +import { mockContext } from '../../../__stubs__/context' +import { + callGetNegotiatorsAPI, + callCreateNegotiatorAPI, + callUpdateNegotiatorAPI, + callGetNegotiatorByIdAPI, +} from '../api' +import { createPlatformAxiosInstance } from '../../../utils/axios-instances' +import { negotiatorMock } from '../__stubs__/negotiator' +import { negotiatorsMock } from '../__stubs__/negotiators' +import { createNegotiatorArgsMock } from '../__stubs__/create-negotiator' +import { updateNegotiatorArgsMock } from '../__stubs__/update-negotiator' +import { getIdFromCreateHeaders } from '../../../utils/get-id-from-create-headers' + +jest.mock('../../../utils/get-id-from-create-headers', () => ({ + getIdFromCreateHeaders: jest.fn(), +})) + +jest.mock('../../../utils/handle-error', () => ({ + handleError: jest.fn(() => Promise.resolve('caught error')), +})) +jest.mock('../../../logger') +jest.mock('../../../utils/axios-instances', () => ({ + createPlatformAxiosInstance: jest.fn(() => ({ + get: jest.fn(), + post: jest.fn(), + patch: jest.fn(), + put: jest.fn(), + delete: jest.fn(), + })), +})) + +describe('callGetNegotiatorsAPI', () => { + it('should work correctly', async () => { + ;(createPlatformAxiosInstance as jest.Mocked).mockReturnValueOnce({ + get: jest.fn(() => Promise.resolve({ data: negotiatorsMock })), + }) + const args = { pageSize: 1 } + const result = await callGetNegotiatorsAPI(args, mockContext) + expect(result).toEqual(negotiatorsMock) + }) + it('should catch error correctly', async () => { + ;(createPlatformAxiosInstance as jest.Mocked).mockReturnValueOnce({ + get: jest.fn(() => Promise.reject('error caught')), + }) + const args = { pageSize: 1 } + const result = await callGetNegotiatorsAPI(args, mockContext) + expect(result).toEqual('caught error') + }) +}) + +describe('callGetNegotiatorByIdAPI', () => { + it('should work correctly', async () => { + ;(createPlatformAxiosInstance as jest.Mocked).mockReturnValueOnce({ + get: jest.fn(() => Promise.resolve({ data: negotiatorMock })), + }) + const args = { id: negotiatorMock.id } + const result = await callGetNegotiatorByIdAPI(args, mockContext) + expect(result).toEqual(negotiatorMock) + }) + + it('should catch error correctly', async () => { + ;(createPlatformAxiosInstance as jest.Mocked).mockReturnValueOnce({ + get: jest.fn(() => Promise.reject('error caught')), + }) + const args = { id: negotiatorMock.id } + const result = await callGetNegotiatorByIdAPI(args, mockContext) + expect(result).toEqual('caught error') + }) +}) + +describe('callCreateNegotiatorAPI', () => { + it('should work correctly', async () => { + ;(createPlatformAxiosInstance as jest.Mocked).mockReturnValueOnce({ + post: jest.fn(() => Promise.resolve({ headers: 'header' })), + get: jest.fn(() => Promise.resolve({ data: negotiatorMock })), + }) + ;(getIdFromCreateHeaders as jest.Mocked).mockReturnValueOnce(negotiatorMock.id) + await callCreateNegotiatorAPI(createNegotiatorArgsMock, mockContext) + expect(getIdFromCreateHeaders).toHaveBeenCalledWith({ headers: 'header' }) + }) + it('should catch error correctly', async () => { + ;(createPlatformAxiosInstance as jest.Mocked).mockReturnValueOnce({ + post: jest.fn(() => Promise.reject('error caught')), + }) + const result = await callCreateNegotiatorAPI(createNegotiatorArgsMock, mockContext) + expect(result).toEqual('caught error') + }) +}) + +describe('callUpdateNegotiatorAPI', () => { + it('should work correctly', async () => { + ;(createPlatformAxiosInstance as jest.Mocked).mockReturnValueOnce({ + patch: jest.fn(() => Promise.resolve({ headers: 'header' })), + }) + await callUpdateNegotiatorAPI(updateNegotiatorArgsMock, mockContext) + }) + it('should catch error correctly', async () => { + ;(createPlatformAxiosInstance as jest.Mocked).mockReturnValueOnce({ + patch: jest.fn(() => Promise.reject('error caught')), + }) + const result = await callUpdateNegotiatorAPI(updateNegotiatorArgsMock, mockContext) + expect(result).toEqual('caught error') + }) +}) diff --git a/packages/graphql-server/src/resolvers/offers/__tests__/api.test.ts b/packages/graphql-server/src/resolvers/offers/__tests__/api.test.ts new file mode 100644 index 0000000000..121929140e --- /dev/null +++ b/packages/graphql-server/src/resolvers/offers/__tests__/api.test.ts @@ -0,0 +1,100 @@ +import { mockContext } from '../../../__stubs__/context' +import { callGetOffersAPI, callCreateOfferAPI, callUpdateOfferAPI, callGetOfferByIdAPI } from '../api' +import { createPlatformAxiosInstance } from '../../../utils/axios-instances' +import { offerMock } from '../__stubs__/offer' +import { offersMock } from '../__stubs__/offers' +import { createOfferArgsMock } from '../__stubs__/create-offer' +import { updateOfferArgsMock } from '../__stubs__/update-offer' +import { getIdFromCreateHeaders } from '../../../utils/get-id-from-create-headers' + +jest.mock('../../../utils/get-id-from-create-headers', () => ({ + getIdFromCreateHeaders: jest.fn(), +})) + +jest.mock('../../../utils/handle-error', () => ({ + handleError: jest.fn(() => Promise.resolve('caught error')), +})) +jest.mock('../../../logger') +jest.mock('../../../utils/axios-instances', () => ({ + createPlatformAxiosInstance: jest.fn(() => ({ + get: jest.fn(), + post: jest.fn(), + patch: jest.fn(), + put: jest.fn(), + delete: jest.fn(), + })), +})) + +describe('callGetOffersAPI', () => { + it('should work correctly', async () => { + ;(createPlatformAxiosInstance as jest.Mocked).mockReturnValueOnce({ + get: jest.fn(() => Promise.resolve({ data: offersMock })), + }) + const args = { pageSize: 1 } + const result = await callGetOffersAPI(args, mockContext) + expect(result).toEqual(offersMock) + }) + it('should catch error correctly', async () => { + ;(createPlatformAxiosInstance as jest.Mocked).mockReturnValueOnce({ + get: jest.fn(() => Promise.reject('error caught')), + }) + const args = { pageSize: 1 } + const result = await callGetOffersAPI(args, mockContext) + expect(result).toEqual('caught error') + }) +}) + +describe('callGetOfferByIdAPI', () => { + it('should work correctly', async () => { + ;(createPlatformAxiosInstance as jest.Mocked).mockReturnValueOnce({ + get: jest.fn(() => Promise.resolve({ data: offerMock })), + }) + const args = { id: offerMock.id } + const result = await callGetOfferByIdAPI(args, mockContext) + expect(result).toEqual(offerMock) + }) + + it('should catch error correctly', async () => { + ;(createPlatformAxiosInstance as jest.Mocked).mockReturnValueOnce({ + get: jest.fn(() => Promise.reject('error caught')), + }) + const args = { id: offerMock.id } + const result = await callGetOfferByIdAPI(args, mockContext) + expect(result).toEqual('caught error') + }) +}) + +describe('callCreateOfferAPI', () => { + it('should work correctly', async () => { + ;(createPlatformAxiosInstance as jest.Mocked).mockReturnValueOnce({ + post: jest.fn(() => Promise.resolve({ headers: 'header' })), + get: jest.fn(() => Promise.resolve({ data: offerMock })), + }) + ;(getIdFromCreateHeaders as jest.Mocked).mockReturnValueOnce(offerMock.id) + await callCreateOfferAPI(createOfferArgsMock, mockContext) + expect(getIdFromCreateHeaders).toHaveBeenCalledWith({ headers: 'header' }) + }) + it('should catch error correctly', async () => { + ;(createPlatformAxiosInstance as jest.Mocked).mockReturnValueOnce({ + post: jest.fn(() => Promise.reject('error caught')), + }) + const result = await callCreateOfferAPI(createOfferArgsMock, mockContext) + expect(result).toEqual('caught error') + }) +}) + +describe('callUpdateOfferAPI', () => { + it('should work correctly', async () => { + ;(createPlatformAxiosInstance as jest.Mocked).mockReturnValueOnce({ + patch: jest.fn(() => Promise.resolve({ headers: 'header' })), + }) + await callUpdateOfferAPI(updateOfferArgsMock, mockContext) + }) + it('should catch error correctly', async () => { + ;(createPlatformAxiosInstance as jest.Mocked).mockReturnValueOnce({ + patch: jest.fn(() => Promise.reject('error caught')), + }) + const result = await callUpdateOfferAPI(updateOfferArgsMock, mockContext) + expect(result).toEqual('caught error') + }) +}) diff --git a/packages/graphql-server/src/resolvers/offices/__tests__/api.test.ts b/packages/graphql-server/src/resolvers/offices/__tests__/api.test.ts new file mode 100644 index 0000000000..462b9b6021 --- /dev/null +++ b/packages/graphql-server/src/resolvers/offices/__tests__/api.test.ts @@ -0,0 +1,100 @@ +import { mockContext } from '../../../__stubs__/context' +import { callGetOfficesAPI, callCreateOfficeAPI, callUpdateOfficeAPI, callGetOfficeByIdAPI } from '../api' +import { createPlatformAxiosInstance } from '../../../utils/axios-instances' +import { officeMock } from '../__stubs__/office' +import { officesMock } from '../__stubs__/offices' +import { createOfficeArgsMock } from '../__stubs__/create-office' +import { updateOfficeArgsMock } from '../__stubs__/update-office' +import { getIdFromCreateHeaders } from '../../../utils/get-id-from-create-headers' + +jest.mock('../../../utils/get-id-from-create-headers', () => ({ + getIdFromCreateHeaders: jest.fn(), +})) + +jest.mock('../../../utils/handle-error', () => ({ + handleError: jest.fn(() => Promise.resolve('caught error')), +})) +jest.mock('../../../logger') +jest.mock('../../../utils/axios-instances', () => ({ + createPlatformAxiosInstance: jest.fn(() => ({ + get: jest.fn(), + post: jest.fn(), + patch: jest.fn(), + put: jest.fn(), + delete: jest.fn(), + })), +})) + +describe('callGetOfficesAPI', () => { + it('should work correctly', async () => { + ;(createPlatformAxiosInstance as jest.Mocked).mockReturnValueOnce({ + get: jest.fn(() => Promise.resolve({ data: officesMock })), + }) + const args = { pageSize: 1 } + const result = await callGetOfficesAPI(args, mockContext) + expect(result).toEqual(officesMock) + }) + it('should catch error correctly', async () => { + ;(createPlatformAxiosInstance as jest.Mocked).mockReturnValueOnce({ + get: jest.fn(() => Promise.reject('error caught')), + }) + const args = { pageSize: 1 } + const result = await callGetOfficesAPI(args, mockContext) + expect(result).toEqual('caught error') + }) +}) + +describe('callGetOfficeByIdAPI', () => { + it('should work correctly', async () => { + ;(createPlatformAxiosInstance as jest.Mocked).mockReturnValueOnce({ + get: jest.fn(() => Promise.resolve({ data: officeMock })), + }) + const args = { id: officeMock.id } + const result = await callGetOfficeByIdAPI(args, mockContext) + expect(result).toEqual(officeMock) + }) + + it('should catch error correctly', async () => { + ;(createPlatformAxiosInstance as jest.Mocked).mockReturnValueOnce({ + get: jest.fn(() => Promise.reject('error caught')), + }) + const args = { id: officeMock.id } + const result = await callGetOfficeByIdAPI(args, mockContext) + expect(result).toEqual('caught error') + }) +}) + +describe('callCreateOfficeAPI', () => { + it('should work correctly', async () => { + ;(createPlatformAxiosInstance as jest.Mocked).mockReturnValueOnce({ + post: jest.fn(() => Promise.resolve({ headers: 'header' })), + get: jest.fn(() => Promise.resolve({ data: officeMock })), + }) + ;(getIdFromCreateHeaders as jest.Mocked).mockReturnValueOnce(officeMock.id) + await callCreateOfficeAPI(createOfficeArgsMock, mockContext) + expect(getIdFromCreateHeaders).toHaveBeenCalledWith({ headers: 'header' }) + }) + it('should catch error correctly', async () => { + ;(createPlatformAxiosInstance as jest.Mocked).mockReturnValueOnce({ + post: jest.fn(() => Promise.reject('error caught')), + }) + const result = await callCreateOfficeAPI(createOfficeArgsMock, mockContext) + expect(result).toEqual('caught error') + }) +}) + +describe('callUpdateOfficeAPI', () => { + it('should work correctly', async () => { + ;(createPlatformAxiosInstance as jest.Mocked).mockReturnValueOnce({ + patch: jest.fn(() => Promise.resolve({ headers: 'header' })), + }) + await callUpdateOfficeAPI(updateOfficeArgsMock, mockContext) + }) + it('should catch error correctly', async () => { + ;(createPlatformAxiosInstance as jest.Mocked).mockReturnValueOnce({ + patch: jest.fn(() => Promise.reject('error caught')), + }) + const result = await callUpdateOfficeAPI(updateOfficeArgsMock, mockContext) + expect(result).toEqual('caught error') + }) +}) diff --git a/packages/graphql-server/src/resolvers/properties/__tests__/api.test.ts b/packages/graphql-server/src/resolvers/properties/__tests__/api.test.ts new file mode 100644 index 0000000000..a46b47bae2 --- /dev/null +++ b/packages/graphql-server/src/resolvers/properties/__tests__/api.test.ts @@ -0,0 +1,100 @@ +import { mockContext } from '../../../__stubs__/context' +import { callGetPropertiesAPI, callCreatePropertyAPI, callUpdatePropertyAPI, callGetPropertyByIdAPI } from '../api' +import { createPlatformAxiosInstance } from '../../../utils/axios-instances' +import { propertyMock } from '../__stubs__/property' +import { propertiesMock } from '../__stubs__/properties' +import { createPropertyArgsMock } from '../__stubs__/create-property' +import { updatePropertyArgsMock } from '../__stubs__/update-property' +import { getIdFromCreateHeaders } from '../../../utils/get-id-from-create-headers' + +jest.mock('../../../utils/get-id-from-create-headers', () => ({ + getIdFromCreateHeaders: jest.fn(), +})) + +jest.mock('../../../utils/handle-error', () => ({ + handleError: jest.fn(() => Promise.resolve('caught error')), +})) +jest.mock('../../../logger') +jest.mock('../../../utils/axios-instances', () => ({ + createPlatformAxiosInstance: jest.fn(() => ({ + get: jest.fn(), + post: jest.fn(), + patch: jest.fn(), + put: jest.fn(), + delete: jest.fn(), + })), +})) + +describe('callGetPropertiesAPI', () => { + it('should work correctly', async () => { + ;(createPlatformAxiosInstance as jest.Mocked).mockReturnValueOnce({ + get: jest.fn(() => Promise.resolve({ data: propertiesMock })), + }) + const args = { pageSize: 1 } + const result = await callGetPropertiesAPI(args, mockContext) + expect(result).toEqual(propertiesMock) + }) + it('should catch error correctly', async () => { + ;(createPlatformAxiosInstance as jest.Mocked).mockReturnValueOnce({ + get: jest.fn(() => Promise.reject('error caught')), + }) + const args = { pageSize: 1 } + const result = await callGetPropertiesAPI(args, mockContext) + expect(result).toEqual('caught error') + }) +}) + +describe('callGetPropertyByIdAPI', () => { + it('should work correctly', async () => { + ;(createPlatformAxiosInstance as jest.Mocked).mockReturnValueOnce({ + get: jest.fn(() => Promise.resolve({ data: propertyMock })), + }) + const args = { id: propertyMock.id } + const result = await callGetPropertyByIdAPI(args, mockContext) + expect(result).toEqual(propertyMock) + }) + + it('should catch error correctly', async () => { + ;(createPlatformAxiosInstance as jest.Mocked).mockReturnValueOnce({ + get: jest.fn(() => Promise.reject('error caught')), + }) + const args = { id: propertyMock.id } + const result = await callGetPropertyByIdAPI(args, mockContext) + expect(result).toEqual('caught error') + }) +}) + +describe('callCreatePropertyAPI', () => { + it('should work correctly', async () => { + ;(createPlatformAxiosInstance as jest.Mocked).mockReturnValueOnce({ + post: jest.fn(() => Promise.resolve({ headers: 'header' })), + get: jest.fn(() => Promise.resolve({ data: propertyMock })), + }) + ;(getIdFromCreateHeaders as jest.Mocked).mockReturnValueOnce(propertyMock.id) + await callCreatePropertyAPI(createPropertyArgsMock, mockContext) + expect(getIdFromCreateHeaders).toHaveBeenCalledWith({ headers: 'header' }) + }) + it('should catch error correctly', async () => { + ;(createPlatformAxiosInstance as jest.Mocked).mockReturnValueOnce({ + post: jest.fn(() => Promise.reject('error caught')), + }) + const result = await callCreatePropertyAPI(createPropertyArgsMock, mockContext) + expect(result).toEqual('caught error') + }) +}) + +describe('callUpdatePropertyAPI', () => { + it('should work correctly', async () => { + ;(createPlatformAxiosInstance as jest.Mocked).mockReturnValueOnce({ + patch: jest.fn(() => Promise.resolve({ headers: 'header' })), + }) + await callUpdatePropertyAPI(updatePropertyArgsMock, mockContext) + }) + it('should catch error correctly', async () => { + ;(createPlatformAxiosInstance as jest.Mocked).mockReturnValueOnce({ + patch: jest.fn(() => Promise.reject('error caught')), + }) + const result = await callUpdatePropertyAPI(updatePropertyArgsMock, mockContext) + expect(result).toEqual('caught error') + }) +}) diff --git a/packages/graphql-server/src/resolvers/propertyImages/__tests__/api.test.ts b/packages/graphql-server/src/resolvers/propertyImages/__tests__/api.test.ts new file mode 100644 index 0000000000..3e301e60dc --- /dev/null +++ b/packages/graphql-server/src/resolvers/propertyImages/__tests__/api.test.ts @@ -0,0 +1,123 @@ +import { mockContext } from '../../../__stubs__/context' +import { + callGetPropertyImagesAPI, + callGetPropertyImageByIdAPI, + callCreatePropertyImageAPI, + callDeletePropertyImageAPI, + callUpdatePropertyImageAPI, +} from '../api' +import { createPlatformAxiosInstance } from '../../../utils/axios-instances' +import { propertyImageMock } from '../__stubs__/propertyImage' +import { propertyImagesMock } from '../__stubs__/propertyImages' +import { createPropertyImageArgsMock } from '../__stubs__/create-property-image' +import { deletePropertyImageArgsMock } from '../__stubs__/delete-property-image' +import { updatePropertyImageArgsMock } from '../__stubs__/update-property-image' +import { getIdFromCreateHeaders } from '../../../utils/get-id-from-create-headers' + +jest.mock('../../../utils/get-id-from-create-headers', () => ({ + getIdFromCreateHeaders: jest.fn(), +})) + +jest.mock('../../../utils/handle-error', () => ({ + handleError: jest.fn(() => Promise.resolve('caught error')), +})) +jest.mock('../../../logger') +jest.mock('../../../utils/axios-instances', () => ({ + createPlatformAxiosInstance: jest.fn(() => ({ + get: jest.fn(), + post: jest.fn(), + patch: jest.fn(), + put: jest.fn(), + delete: jest.fn(), + })), +})) + +describe('callGetPropertyImagesAPI', () => { + it('should work correctly', async () => { + ;(createPlatformAxiosInstance as jest.Mocked).mockReturnValueOnce({ + get: jest.fn(() => Promise.resolve({ data: propertyImagesMock })), + }) + const args = { pageSize: 1 } + const result = await callGetPropertyImagesAPI(args, mockContext) + expect(result).toEqual(propertyImagesMock) + }) + it('should catch error correctly', async () => { + ;(createPlatformAxiosInstance as jest.Mocked).mockReturnValueOnce({ + get: jest.fn(() => Promise.reject('error caught')), + }) + const args = { pageSize: 1 } + const result = await callGetPropertyImagesAPI(args, mockContext) + expect(result).toEqual('caught error') + }) +}) + +describe('callGetPropertyImageByIdAPI', () => { + it('should work correctly', async () => { + ;(createPlatformAxiosInstance as jest.Mocked).mockReturnValueOnce({ + get: jest.fn(() => Promise.resolve({ data: propertyImageMock })), + }) + const args = { id: propertyImageMock.id } + const result = await callGetPropertyImageByIdAPI(args, mockContext) + expect(result).toEqual(propertyImageMock) + }) + + it('should catch error correctly', async () => { + ;(createPlatformAxiosInstance as jest.Mocked).mockReturnValueOnce({ + get: jest.fn(() => Promise.reject('error caught')), + }) + const args = { id: propertyImageMock.id } + const result = await callGetPropertyImageByIdAPI(args, mockContext) + expect(result).toEqual('caught error') + }) +}) + +describe('callCreatePropertyImageAPI', () => { + it('should work correctly', async () => { + ;(createPlatformAxiosInstance as jest.Mocked).mockReturnValueOnce({ + post: jest.fn(() => Promise.resolve({ headers: 'header' })), + get: jest.fn(() => Promise.resolve({ data: propertyImageMock })), + }) + ;(getIdFromCreateHeaders as jest.Mocked).mockReturnValueOnce(propertyImageMock.id) + await callCreatePropertyImageAPI(createPropertyImageArgsMock, mockContext) + expect(getIdFromCreateHeaders).toHaveBeenCalledWith({ headers: 'header' }) + }) + it('should catch error correctly', async () => { + ;(createPlatformAxiosInstance as jest.Mocked).mockReturnValueOnce({ + post: jest.fn(() => Promise.reject('error caught')), + }) + const result = await callCreatePropertyImageAPI(createPropertyImageArgsMock, mockContext) + expect(result).toEqual('caught error') + }) +}) + +describe('callUpdatePropertyImageAPI', () => { + it('should work correctly', async () => { + ;(createPlatformAxiosInstance as jest.Mocked).mockReturnValueOnce({ + patch: jest.fn(() => Promise.resolve({ headers: 'header' })), + }) + await callUpdatePropertyImageAPI(updatePropertyImageArgsMock, mockContext) + }) + it('should catch error correctly', async () => { + ;(createPlatformAxiosInstance as jest.Mocked).mockReturnValueOnce({ + patch: jest.fn(() => Promise.reject('error caught')), + }) + const result = await callUpdatePropertyImageAPI(updatePropertyImageArgsMock, mockContext) + expect(result).toEqual('caught error') + }) +}) + +describe('callDeletePropertyImageAPI', () => { + it('should work correctly', async () => { + ;(createPlatformAxiosInstance as jest.Mocked).mockReturnValueOnce({ + delete: jest.fn(() => Promise.resolve({ headers: 'header' })), + }) + await callDeletePropertyImageAPI(deletePropertyImageArgsMock, mockContext) + }) + it('should catch error correctly', async () => { + ;(createPlatformAxiosInstance as jest.Mocked).mockReturnValueOnce({ + delete: jest.fn(() => Promise.reject('error caught')), + }) + const result = await callDeletePropertyImageAPI(deletePropertyImageArgsMock, mockContext) + expect(result).toEqual('caught error') + }) +}) diff --git a/packages/graphql-server/src/resolvers/tenancies/__tests__/api.test.ts b/packages/graphql-server/src/resolvers/tenancies/__tests__/api.test.ts new file mode 100644 index 0000000000..ee78308142 --- /dev/null +++ b/packages/graphql-server/src/resolvers/tenancies/__tests__/api.test.ts @@ -0,0 +1,210 @@ +import { mockContext } from '../../../__stubs__/context' +import { + callGetTenanciesAPI, + callGetTenancyByIdAPI, + callGetTenancyChecksAPI, + callGetTenancyCheckByIdAPI, + callGetTenancyRelationshipsAPI, + callCreateTenancyAPI, + callCreateTenancyCheckAPI, + callDeleteTenancyCheckAPI, + callUpdateTenancyCheckAPI, +} from '../api' +import { createPlatformAxiosInstance } from '../../../utils/axios-instances' +import { + tenancyMock, + tenancyCheckMock, + tenanciesListMock, + tenancyChecksListMock, + tenancyRelationshipsListMock, +} from '../__stubs__/tenancy-query' +import { + createTenancyArgsMock, + createTenancyCheckArgsMock, + deleteTenancyCheckArgsMock, + updateTenancyCheckArgsMock, +} from '../__stubs__/tenancy-mutation' +import { getIdFromCreateHeaders } from '../../../utils/get-id-from-create-headers' + +jest.mock('../../../utils/get-id-from-create-headers', () => ({ + getIdFromCreateHeaders: jest.fn(), +})) + +jest.mock('../../../utils/handle-error', () => ({ + handleError: jest.fn(() => Promise.resolve('caught error')), +})) +jest.mock('../../../logger') +jest.mock('../../../utils/axios-instances', () => ({ + createPlatformAxiosInstance: jest.fn(() => ({ + get: jest.fn(), + post: jest.fn(), + patch: jest.fn(), + put: jest.fn(), + delete: jest.fn(), + })), +})) + +describe('callGetTenanciesAPI', () => { + it('should work correctly', async () => { + ;(createPlatformAxiosInstance as jest.Mocked).mockReturnValueOnce({ + get: jest.fn(() => Promise.resolve({ data: tenanciesListMock })), + }) + const args = { pageSize: 1 } + const result = await callGetTenanciesAPI(args, mockContext) + expect(result).toEqual(tenanciesListMock) + }) + it('should catch error correctly', async () => { + ;(createPlatformAxiosInstance as jest.Mocked).mockReturnValueOnce({ + get: jest.fn(() => Promise.reject('error caught')), + }) + const args = { pageSize: 1 } + const result = await callGetTenanciesAPI(args, mockContext) + expect(result).toEqual('caught error') + }) +}) + +describe('callGetTenancyByIdAPI', () => { + it('should work correctly', async () => { + ;(createPlatformAxiosInstance as jest.Mocked).mockReturnValueOnce({ + get: jest.fn(() => Promise.resolve({ data: tenancyMock })), + }) + const args = { id: tenancyMock.id } + const result = await callGetTenancyByIdAPI(args, mockContext) + expect(result).toEqual(tenancyMock) + }) + + it('should catch error correctly', async () => { + ;(createPlatformAxiosInstance as jest.Mocked).mockReturnValueOnce({ + get: jest.fn(() => Promise.reject('error caught')), + }) + const args = { id: tenancyMock.id } + const result = await callGetTenancyByIdAPI(args, mockContext) + expect(result).toEqual('caught error') + }) +}) + +describe('callGetTenancyRelationshipsAPI', () => { + it('should work correctly', async () => { + ;(createPlatformAxiosInstance as jest.Mocked).mockReturnValueOnce({ + get: jest.fn(() => Promise.resolve({ data: tenancyRelationshipsListMock })), + }) + const args = { id: 'id', relationshipId: 'relationshipId' } + const result = await callGetTenancyRelationshipsAPI(args, mockContext) + expect(result).toEqual(tenancyRelationshipsListMock) + }) + + it('should catch error correctly', async () => { + ;(createPlatformAxiosInstance as jest.Mocked).mockReturnValueOnce({ + get: jest.fn(() => Promise.reject('error caught')), + }) + const args = { id: 'id', relationshipId: 'relationshipId' } + const result = await callGetTenancyRelationshipsAPI(args, mockContext) + expect(result).toEqual('caught error') + }) +}) + +describe('callGetTenancyChecksAPI', () => { + it('should work correctly', async () => { + ;(createPlatformAxiosInstance as jest.Mocked).mockReturnValueOnce({ + get: jest.fn(() => Promise.resolve({ data: tenancyChecksListMock })), + }) + const args = { pageSize: 1, id: 'id' } + const result = await callGetTenancyChecksAPI(args, mockContext) + expect(result).toEqual(tenancyChecksListMock) + }) + it('should catch error correctly', async () => { + ;(createPlatformAxiosInstance as jest.Mocked).mockReturnValueOnce({ + get: jest.fn(() => Promise.reject('error caught')), + }) + const args = { pageSize: 1, id: 'id' } + const result = await callGetTenancyChecksAPI(args, mockContext) + expect(result).toEqual('caught error') + }) +}) + +describe('callGetTenancyCheckByIdAPI', () => { + it('should work correctly', async () => { + ;(createPlatformAxiosInstance as jest.Mocked).mockReturnValueOnce({ + get: jest.fn(() => Promise.resolve({ data: tenancyCheckMock })), + }) + const args = { id: tenancyCheckMock.id, checkId: 'checkId' } + const result = await callGetTenancyCheckByIdAPI(args, mockContext) + expect(result).toEqual(tenancyCheckMock) + }) + + it('should catch error correctly', async () => { + ;(createPlatformAxiosInstance as jest.Mocked).mockReturnValueOnce({ + get: jest.fn(() => Promise.reject('error caught')), + }) + const args = { id: tenancyCheckMock.id, checkId: 'checkId' } + const result = await callGetTenancyCheckByIdAPI(args, mockContext) + expect(result).toEqual('caught error') + }) +}) + +describe('callCreateTenancyAPI', () => { + it('should work correctly', async () => { + ;(createPlatformAxiosInstance as jest.Mocked).mockReturnValueOnce({ + post: jest.fn(() => Promise.resolve({ headers: 'header' })), + get: jest.fn(() => Promise.resolve({ data: tenancyMock })), + }) + ;(getIdFromCreateHeaders as jest.Mocked).mockReturnValueOnce(tenancyMock.id) + await callCreateTenancyAPI(createTenancyArgsMock, mockContext) + expect(getIdFromCreateHeaders).toHaveBeenCalledWith({ headers: 'header' }) + }) + it('should catch error correctly', async () => { + ;(createPlatformAxiosInstance as jest.Mocked).mockReturnValueOnce({ + post: jest.fn(() => Promise.reject('error caught')), + }) + const result = await callCreateTenancyAPI(createTenancyArgsMock, mockContext) + expect(result).toEqual('caught error') + }) +}) + +describe('callCreateTenancyCheckAPI', () => { + it('should work correctly', async () => { + ;(createPlatformAxiosInstance as jest.Mocked).mockReturnValueOnce({ + post: jest.fn(() => Promise.resolve({ headers: 'header' })), + }) + await callCreateTenancyCheckAPI(createTenancyCheckArgsMock, mockContext) + }) + it('should catch error correctly', async () => { + ;(createPlatformAxiosInstance as jest.Mocked).mockReturnValueOnce({ + post: jest.fn(() => Promise.reject('error caught')), + }) + const result = await callCreateTenancyCheckAPI(createTenancyCheckArgsMock, mockContext) + expect(result).toEqual('caught error') + }) +}) + +describe('callUpdateTenancyCheckAPI', () => { + it('should work correctly', async () => { + ;(createPlatformAxiosInstance as jest.Mocked).mockReturnValueOnce({ + patch: jest.fn(() => Promise.resolve({ headers: 'header' })), + }) + await callUpdateTenancyCheckAPI(updateTenancyCheckArgsMock, mockContext) + }) + it('should catch error correctly', async () => { + ;(createPlatformAxiosInstance as jest.Mocked).mockReturnValueOnce({ + patch: jest.fn(() => Promise.reject('error caught')), + }) + const result = await callUpdateTenancyCheckAPI(updateTenancyCheckArgsMock, mockContext) + expect(result).toEqual('caught error') + }) +}) + +describe('callDeleteTenancyCheckAPI', () => { + it('should work correctly', async () => { + ;(createPlatformAxiosInstance as jest.Mocked).mockReturnValueOnce({ + delete: jest.fn(() => Promise.resolve({ headers: 'header' })), + }) + await callDeleteTenancyCheckAPI(deleteTenancyCheckArgsMock, mockContext) + }) + it('should catch error correctly', async () => { + ;(createPlatformAxiosInstance as jest.Mocked).mockReturnValueOnce({ + delete: jest.fn(() => Promise.reject('error caught')), + }) + const result = await callDeleteTenancyCheckAPI(deleteTenancyCheckArgsMock, mockContext) + expect(result).toEqual('caught error') + }) +}) diff --git a/packages/graphql-server/src/resolvers/vendors/__stubs__/create-vendor-relationships.ts b/packages/graphql-server/src/resolvers/vendors/__stubs__/create-vendor-relationships.ts new file mode 100644 index 0000000000..d40aacf46e --- /dev/null +++ b/packages/graphql-server/src/resolvers/vendors/__stubs__/create-vendor-relationships.ts @@ -0,0 +1,8 @@ +import { CreateVendorRelationshipArgs } from '../vendors' + +export const createVendorRelationshipArgsStub: CreateVendorRelationshipArgs = { + id: 'OXF200001', + associatedId: 'OXF18000001', + associatedType: 'contact', + isMain: false, +} diff --git a/packages/graphql-server/src/resolvers/vendors/__stubs__/delete-vendor-relationships.ts b/packages/graphql-server/src/resolvers/vendors/__stubs__/delete-vendor-relationships.ts new file mode 100644 index 0000000000..8acb230d6a --- /dev/null +++ b/packages/graphql-server/src/resolvers/vendors/__stubs__/delete-vendor-relationships.ts @@ -0,0 +1,6 @@ +import { DeleteVendorRelationshipArgs } from '../vendors' + +export const deleteVendorRelationshipsArgsStub: DeleteVendorRelationshipArgs = { + id: 'OXF200001', + relationshipId: 'relationshipId1', +} diff --git a/packages/graphql-server/src/resolvers/vendors/__stubs__/vendor-relationship.ts b/packages/graphql-server/src/resolvers/vendors/__stubs__/vendor-relationship.ts new file mode 100644 index 0000000000..3fa084e486 --- /dev/null +++ b/packages/graphql-server/src/resolvers/vendors/__stubs__/vendor-relationship.ts @@ -0,0 +1,20 @@ +import { VendorContactRelationshipModel } from '../../../types' + +export const vendorRelationshipStub: VendorContactRelationshipModel = { + id: 'OXF20002002', + vendorId: 'OXF190001', + created: '2020-01-25T15:44:28.0000000Z', + modified: '2020-01-26T09:24:02.0000000Z', + associatedType: 'contact', + associatedId: 'OXF2000002', + isMain: true, + _links: { + self: { + href: '/vendors/OXF190001/relationships/OXF20002002', + }, + contact: { + href: '/contacts/OXF2000002', + }, + }, + _embedded: null, +} diff --git a/packages/graphql-server/src/resolvers/vendors/__stubs__/vendor-relationships.ts b/packages/graphql-server/src/resolvers/vendors/__stubs__/vendor-relationships.ts new file mode 100644 index 0000000000..4c17ae1146 --- /dev/null +++ b/packages/graphql-server/src/resolvers/vendors/__stubs__/vendor-relationships.ts @@ -0,0 +1,28 @@ +import { PagedResultVendorContactRelationshipModel_ } from '../../../types' + +export const vendorRelationshipsStub: PagedResultVendorContactRelationshipModel_ = { + pageNumber: 1, + pageSize: 1, + pageCount: 1, + totalCount: 1, + _embedded: [ + { + id: 'OXF20002002', + vendorId: 'OXF190001', + created: '2020-01-25T15:44:28.0000000Z', + modified: '2020-01-26T09:24:02.0000000Z', + associatedType: 'contact', + associatedId: 'OXF2000002', + isMain: true, + _links: { + self: { + href: '/vendors/OXF190001/relationships/OXF20002002', + }, + contact: { + href: '/contacts/OXF2000002', + }, + }, + _embedded: null, + }, + ], +} diff --git a/packages/graphql-server/src/resolvers/vendors/__tests__/api.test.ts b/packages/graphql-server/src/resolvers/vendors/__tests__/api.test.ts new file mode 100644 index 0000000000..a838b82136 --- /dev/null +++ b/packages/graphql-server/src/resolvers/vendors/__tests__/api.test.ts @@ -0,0 +1,163 @@ +import { mockContext } from '../../../__stubs__/context' +import { + callGetVendorsAPI, + callUpdateVendorAPI, + callGetVendorByIdAPI, + callGetVendorRelationshipsAPI, + callCreateVendorRelationshipAPI, + callDeleteVendorRelationshipAPI, + callGetVendorRelationshipByIdAPI, +} from '../api' +import { createPlatformAxiosInstance } from '../../../utils/axios-instances' +import { vendorStub } from '../__stubs__/vendor' +import { vendorsStub } from '../__stubs__/vendors' +import { updateVendorArgsStub } from '../__stubs__/update-vendor' +import { vendorRelationshipStub } from '../__stubs__/vendor-relationship' +import { vendorRelationshipsStub } from '../__stubs__/vendor-relationships' +import { createVendorRelationshipArgsStub } from '../__stubs__/create-vendor-relationships' +import { deleteVendorRelationshipsArgsStub } from '../__stubs__/delete-vendor-relationships' + +jest.mock('../../../utils/get-id-from-create-headers', () => ({ + getIdFromCreateHeaders: jest.fn(), +})) + +jest.mock('../../../utils/handle-error', () => ({ + handleError: jest.fn(() => Promise.resolve('caught error')), +})) +jest.mock('../../../logger') +jest.mock('../../../utils/axios-instances', () => ({ + createPlatformAxiosInstance: jest.fn(() => ({ + get: jest.fn(), + post: jest.fn(), + patch: jest.fn(), + put: jest.fn(), + delete: jest.fn(), + })), +})) + +describe('callGetVendorsAPI', () => { + it('should work correctly', async () => { + ;(createPlatformAxiosInstance as jest.Mocked).mockReturnValueOnce({ + get: jest.fn(() => Promise.resolve({ data: vendorsStub })), + }) + const args = { pageSize: 1 } + const result = await callGetVendorsAPI(args, mockContext) + expect(result).toEqual(vendorsStub) + }) + it('should catch error correctly', async () => { + ;(createPlatformAxiosInstance as jest.Mocked).mockReturnValueOnce({ + get: jest.fn(() => Promise.reject('error caught')), + }) + const args = { pageSize: 1 } + const result = await callGetVendorsAPI(args, mockContext) + expect(result).toEqual('caught error') + }) +}) + +describe('callGetVendorByIdAPI', () => { + it('should work correctly', async () => { + ;(createPlatformAxiosInstance as jest.Mocked).mockReturnValueOnce({ + get: jest.fn(() => Promise.resolve({ data: vendorStub })), + }) + const args = { id: vendorStub.id } + const result = await callGetVendorByIdAPI(args, mockContext) + expect(result).toEqual(vendorStub) + }) + + it('should catch error correctly', async () => { + ;(createPlatformAxiosInstance as jest.Mocked).mockReturnValueOnce({ + get: jest.fn(() => Promise.reject('error caught')), + }) + const args = { id: vendorStub.id } + const result = await callGetVendorByIdAPI(args, mockContext) + expect(result).toEqual('caught error') + }) +}) + +describe('callGetVendorRelationshipByIdAPI', () => { + it('should work correctly', async () => { + ;(createPlatformAxiosInstance as jest.Mocked).mockReturnValueOnce({ + get: jest.fn(() => Promise.resolve({ data: vendorRelationshipStub })), + }) + const args = { id: 'id', relationshipId: 'relationshipId' } + const result = await callGetVendorRelationshipByIdAPI(args, mockContext) + expect(result).toEqual(vendorRelationshipStub) + }) + + it('should catch error correctly', async () => { + ;(createPlatformAxiosInstance as jest.Mocked).mockReturnValueOnce({ + get: jest.fn(() => Promise.reject('error caught')), + }) + const args = { id: 'id', relationshipId: 'relationshipId' } + const result = await callGetVendorRelationshipByIdAPI(args, mockContext) + expect(result).toEqual('caught error') + }) +}) + +describe('callGetVendorRelationshipsAPI', () => { + it('should work correctly', async () => { + ;(createPlatformAxiosInstance as jest.Mocked).mockReturnValueOnce({ + get: jest.fn(() => Promise.resolve({ data: vendorRelationshipsStub })), + }) + const args = { pageSize: 1, id: 'id' } + const result = await callGetVendorRelationshipsAPI(args, mockContext) + expect(result).toEqual(vendorRelationshipsStub) + }) + it('should catch error correctly', async () => { + ;(createPlatformAxiosInstance as jest.Mocked).mockReturnValueOnce({ + get: jest.fn(() => Promise.reject('error caught')), + }) + const args = { pageSize: 1, id: 'id' } + const result = await callGetVendorRelationshipsAPI(args, mockContext) + expect(result).toEqual('caught error') + }) +}) + +describe('callCreateVendorRelationshipAPI', () => { + it('should work correctly', async () => { + ;(createPlatformAxiosInstance as jest.Mocked).mockReturnValueOnce({ + post: jest.fn(() => Promise.resolve({ headers: 'header' })), + get: jest.fn(() => Promise.resolve({ data: vendorRelationshipStub })), + }) + await callCreateVendorRelationshipAPI(createVendorRelationshipArgsStub, mockContext) + }) + it('should catch error correctly', async () => { + ;(createPlatformAxiosInstance as jest.Mocked).mockReturnValueOnce({ + post: jest.fn(() => Promise.reject('error caught')), + }) + const result = await callCreateVendorRelationshipAPI(createVendorRelationshipArgsStub, mockContext) + expect(result).toEqual('caught error') + }) +}) + +describe('callUpdateVendorAPI', () => { + it('should work correctly', async () => { + ;(createPlatformAxiosInstance as jest.Mocked).mockReturnValueOnce({ + patch: jest.fn(() => Promise.resolve({ headers: 'header' })), + }) + await callUpdateVendorAPI(updateVendorArgsStub, mockContext) + }) + it('should catch error correctly', async () => { + ;(createPlatformAxiosInstance as jest.Mocked).mockReturnValueOnce({ + patch: jest.fn(() => Promise.reject('error caught')), + }) + const result = await callUpdateVendorAPI(updateVendorArgsStub, mockContext) + expect(result).toEqual('caught error') + }) +}) + +describe('callDeleteVendorRelationshipAPI', () => { + it('should work correctly', async () => { + ;(createPlatformAxiosInstance as jest.Mocked).mockReturnValueOnce({ + delete: jest.fn(() => Promise.resolve({ headers: 'header' })), + }) + await callDeleteVendorRelationshipAPI(deleteVendorRelationshipsArgsStub, mockContext) + }) + it('should catch error correctly', async () => { + ;(createPlatformAxiosInstance as jest.Mocked).mockReturnValueOnce({ + delete: jest.fn(() => Promise.reject('error caught')), + }) + const result = await callDeleteVendorRelationshipAPI(deleteVendorRelationshipsArgsStub, mockContext) + expect(result).toEqual('caught error') + }) +}) diff --git a/packages/graphql-server/src/resolvers/vendors/vendors.d.ts b/packages/graphql-server/src/resolvers/vendors/vendors.d.ts index c2c0ceb87c..8068c7d797 100644 --- a/packages/graphql-server/src/resolvers/vendors/vendors.d.ts +++ b/packages/graphql-server/src/resolvers/vendors/vendors.d.ts @@ -15,28 +15,28 @@ export type GetVendorByIdArgs = { } export type GetVendorsArgs = { - pageSize: number - pageNumber: number - sortBy: string - id: string[] - embed: string[] - negotiatorId: string[] - officeId: string[] - address: string - name: string - createdFrom: string - createdTo: string - lastCallFrom: string - lastCallTo: string - nextCallFrom: string - nextCallTo: string - metadata: string[] + pageSize?: number + pageNumber?: number + sortBy?: string + id?: string[] + embed?: string[] + negotiatorId?: string[] + officeId?: string[] + address?: string + name?: string + createdFrom?: string + createdTo?: string + lastCallFrom?: string + lastCallTo?: string + nextCallFrom?: string + nextCallTo?: string + metadata?: string[] } export type GetVendorRelationshipsArgs = { id: string - pageSize: number - pageNumber: number + pageSize?: number + pageNumber?: number } export type GetVendorRelationshipByIdArgs = {