Skip to content

Commit

Permalink
chore: #2362 graphql api layer tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Cuong Vu committed Aug 19, 2020
1 parent 768f5f8 commit 6e1ee78
Show file tree
Hide file tree
Showing 23 changed files with 1,762 additions and 22 deletions.
1 change: 0 additions & 1 deletion packages/graphql-server/jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ module.exports = {
testPathIgnorePatterns: ['<rootDir>/src/tests/'],
coveragePathIgnorePatterns: [
'<rootDir>[/\\\\](node_modules|src/types|src/tests|src/scripts|src/__stubs__|dist)[/\\\\]',
'api.ts',
'.d.ts',
'index.ts',
],
Expand Down
184 changes: 184 additions & 0 deletions packages/graphql-server/src/resolvers/applicants/__tests__/api.test.ts
Original file line number Diff line number Diff line change
@@ -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<any>).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<any>).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<any>).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<any>).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<any>).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<any>).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<any>).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<any>).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<any>).mockReturnValueOnce({
post: jest.fn(() => Promise.resolve({ headers: 'header' })),
get: jest.fn(() => Promise.resolve({ data: applicantMock })),
})
;(getIdFromCreateHeaders as jest.Mocked<any>).mockReturnValueOnce(applicantMock.id)
await callCreateApplicantAPI(createApplicantArgsMock, mockContext)
expect(getIdFromCreateHeaders).toHaveBeenCalledWith({ headers: 'header' })
})
it('should catch error correctly', async () => {
;(createPlatformAxiosInstance as jest.Mocked<any>).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<any>).mockReturnValueOnce({
post: jest.fn(() => Promise.resolve({ headers: 'header' })),
})
await callCreateApplicantRelationshipAPI(createRelationshipsArgs, mockContext)
})
it('should catch error correctly', async () => {
;(createPlatformAxiosInstance as jest.Mocked<any>).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<any>).mockReturnValueOnce({
patch: jest.fn(() => Promise.resolve({ headers: 'header' })),
})
await callUpdateApplicantAPI(updateApplicantArgsMock, mockContext)
})
it('should catch error correctly', async () => {
;(createPlatformAxiosInstance as jest.Mocked<any>).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<any>).mockReturnValueOnce({
delete: jest.fn(() => Promise.resolve({ headers: 'header' })),
})
await callDeleteApplicantRelationshipAPI(deleteRelationshipMockArgs, mockContext)
})
it('should catch error correctly', async () => {
;(createPlatformAxiosInstance as jest.Mocked<any>).mockReturnValueOnce({
delete: jest.fn(() => Promise.reject('error caught')),
})
const result = await callDeleteApplicantRelationshipAPI(deleteRelationshipMockArgs, mockContext)
expect(result).toEqual('caught error')
})
})
Original file line number Diff line number Diff line change
@@ -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<any>).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<any>).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<any>).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<any>).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<any>).mockReturnValueOnce({
post: jest.fn(() => Promise.resolve({ headers: 'header' })),
get: jest.fn(() => Promise.resolve({ data: appointmentMock })),
})
;(getIdFromCreateHeaders as jest.Mocked<any>).mockReturnValueOnce(appointmentMock.id)
await callCreateAppointmentAPI(createAppointmentArgsMock, mockContext)
expect(getIdFromCreateHeaders).toHaveBeenCalledWith({ headers: 'header' })
})
it('should catch error correctly', async () => {
;(createPlatformAxiosInstance as jest.Mocked<any>).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<any>).mockReturnValueOnce({
patch: jest.fn(() => Promise.resolve({ headers: 'header' })),
})
await callUpdateAppointmentAPI(updateAppointmentArgsMock, mockContext)
})
it('should catch error correctly', async () => {
;(createPlatformAxiosInstance as jest.Mocked<any>).mockReturnValueOnce({
patch: jest.fn(() => Promise.reject('error caught')),
})
const result = await callUpdateAppointmentAPI(updateAppointmentArgsMock, mockContext)
expect(result).toEqual('caught error')
})
})
100 changes: 100 additions & 0 deletions packages/graphql-server/src/resolvers/areas/__tests__/api.test.ts
Original file line number Diff line number Diff line change
@@ -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<any>).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<any>).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<any>).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<any>).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<any>).mockReturnValueOnce({
post: jest.fn(() => Promise.resolve({ headers: 'header' })),
get: jest.fn(() => Promise.resolve({ data: areaMock })),
})
;(getIdFromCreateHeaders as jest.Mocked<any>).mockReturnValueOnce(areaMock.id)
await callCreateAreaAPI(createAreaArgsMock, mockContext)
expect(getIdFromCreateHeaders).toHaveBeenCalledWith({ headers: 'header' })
})
it('should catch error correctly', async () => {
;(createPlatformAxiosInstance as jest.Mocked<any>).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<any>).mockReturnValueOnce({
patch: jest.fn(() => Promise.resolve({ headers: 'header' })),
})
await callUpdateAreaAPI(updateAreaArgsMock, mockContext)
})
it('should catch error correctly', async () => {
;(createPlatformAxiosInstance as jest.Mocked<any>).mockReturnValueOnce({
patch: jest.fn(() => Promise.reject('error caught')),
})
const result = await callUpdateAreaAPI(updateAreaArgsMock, mockContext)
expect(result).toEqual('caught error')
})
})
Loading

0 comments on commit 6e1ee78

Please sign in to comment.