Skip to content

Commit

Permalink
feat: #2135 graphql for enquiries (#2389)
Browse files Browse the repository at this point in the history
  • Loading branch information
Trường An authored Aug 19, 2020
1 parent 049897d commit 9e8269b
Showing 16 changed files with 4,925 additions and 4,938 deletions.
1 change: 1 addition & 0 deletions packages/graphql-server/src/constants/api.ts
Original file line number Diff line number Diff line change
@@ -20,6 +20,7 @@ export const URLS = {
landlords: '/landlords',
departments: '/departments',
sources: '/sources',
enquiries: '/enquiries',
}

export const HEADERS = {
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { CreateEnquiryArgs } from '../enquiries'

export const createEnquiryArgsMock: CreateEnquiryArgs = {
title: 'Mr',
forename: 'John',
surname: 'Smith',
position: 'renting',
enquiryType: 'salesApplicant',
message: 'Sales enquiry from Rightmove for 10 High Street, Solihull',
officeId: 'JAS',
marketingConsent: 'grant',
sourceName: 'Rightmove',
homePhone: '01234 567890',
workPhone: '01234 567890',
mobilePhone: '07890 123456',
email: '[email protected]',
address: {
buildingName: null,
buildingNumber: '15',
line1: 'Example street',
line2: 'Solihull',
line3: 'West Midlands',
line4: null,
postcode: 'B91 2XX',
countryId: 'GB',
},
propertyIds: ['OXF190012'],
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import { PagedResultEnquiryModel_ } from '../../../types'

export const enquiriesMock: PagedResultEnquiryModel_ = {
_embedded: [
{
id: 45,
created: '2018-12-12T12:30:23.0000000Z',
modified: '2019-01-08T12:30:34.0000000Z',
title: 'Mr',
forename: 'John',
surname: 'Smith',
enquiryType: 'salesApplicant',
message: 'Sales enquiry from Rightmove for 10 High Street, Solihull',
status: 'pending',
marketingConsent: 'grant',
position: 'renting',
officeId: 'OXF',
sourceName: 'Rightmove',
homePhone: '01234 567890',
workPhone: '01234 567890',
mobilePhone: '07890 123456',
email: '[email protected]',
address: {
buildingName: null,
buildingNumber: '15',
line1: 'Example street',
line2: 'Solihull',
line3: 'West Midlands',
line4: '',
postcode: 'B91 2XX',
countryId: 'GB',
},
propertyIds: ['OXF190012'],
_links: {
self: {
href: '/enquiries/45',
},
properties: {
href: '/properties/?id=OXF190012',
},
},
_embedded: null,
},
],
pageNumber: 1,
pageSize: 1,
pageCount: 1,
totalPageCount: 1,
totalCount: 25,
_links: {
self: {
href: '/enquiries/?PageNumber=1&PageSize=1',
},
first: {
href: '/enquiries/?PageNumber=1&PageSize=1',
},
next: {
href: '/enquiries/?PageNumber=2&PageSize=1',
},
last: {
href: '/enquiries/?PageNumber=25&PageSize=1',
},
},
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { EnquiryModel } from '../../../types'

export const enquiryMock: EnquiryModel = {
id: 45,
created: '2018-12-12T12:30:23.0000000Z',
modified: '2019-01-08T12:30:34.0000000Z',
title: 'Mr',
forename: 'John',
surname: 'Smith',
enquiryType: 'salesApplicant',
message: 'Sales enquiry from Rightmove for 10 High Street, Solihull',
status: 'pending',
marketingConsent: 'grant',
position: 'renting',
officeId: 'OXF',
sourceName: 'Rightmove',
homePhone: '01234 567890',
workPhone: '01234 567890',
mobilePhone: '07890 123456',
email: '[email protected]',
address: {
buildingName: null,
buildingNumber: '15',
line1: 'Example street',
line2: 'Solihull',
line3: 'West Midlands',
line4: '',
postcode: 'B91 2XX',
countryId: 'GB',
},
propertyIds: ['OXF190012'],
_links: {
self: {
href: '/enquiries/45',
},
properties: {
href: '/properties/?id=OXF190012',
},
},
_embedded: null,
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
import { mockContext } from '../../../__stubs__/context'
import { callGetEnquiriesAPI, callCreateEnquiryAPI, callGetEnquiryByIdAPI } from '../api'
import { createPlatformAxiosInstance } from '../../../utils/axios-instances'
import { enquiriesMock } from '../__stubs__/enquiries'
import { enquiryMock } from '../__stubs__/enquiry'
import { getIdFromCreateHeaders } from '../../../utils/get-id-from-create-headers'
import { createEnquiryArgsMock } from '../__stubs__/create-enquiry'
import { URLS } from '../../../constants/api'

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().mockImplementation(value => {
if (value === `${URLS.enquiries}/id`) {
return {
data: enquiryMock,
}
}
return {
data: enquiriesMock,
}
}),
post: jest.fn().mockImplementation(() => enquiryMock),
})),
}))

describe('callGetEnquiriesAPI', () => {
it('should work correctly', async () => {
;(createPlatformAxiosInstance as jest.Mocked<any>).mockReturnValueOnce({
get: jest.fn(() => Promise.resolve({ data: enquiriesMock })),
})
const args = { pageSize: 1 }
const result = await callGetEnquiriesAPI(args, mockContext)
expect(result).toEqual(enquiriesMock)
})
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 callGetEnquiriesAPI(args, mockContext)
expect(result).toEqual('caught error')
})
})

describe('callGetEnquiryByIdAPI', () => {
it('should work correctly', async () => {
;(createPlatformAxiosInstance as jest.Mocked<any>).mockReturnValueOnce({
get: jest.fn(() => Promise.resolve({ data: enquiryMock })),
})
const args = { id: enquiryMock.id }
const result = await callGetEnquiryByIdAPI(args, mockContext)
expect(result).toEqual(enquiryMock)
})

it('should catch error correctly', async () => {
;(createPlatformAxiosInstance as jest.Mocked<any>).mockReturnValueOnce({
get: jest.fn(() => Promise.reject('error caught')),
})
const args = { id: enquiryMock.id }
const result = await callGetEnquiryByIdAPI(args, mockContext)
expect(result).toEqual('caught error')
})
})

describe('callCreateEnquiryAPI', () => {
it('should work correctly', async () => {
;(createPlatformAxiosInstance as jest.Mocked<any>).mockReturnValueOnce({
post: jest.fn(() => Promise.resolve({ headers: 'header' })),
get: jest.fn(() => Promise.resolve({ data: enquiryMock })),
})
;(getIdFromCreateHeaders as jest.Mocked<any>).mockReturnValueOnce(enquiryMock.id)
await callCreateEnquiryAPI(createEnquiryArgsMock, 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 callCreateEnquiryAPI(createEnquiryArgsMock, mockContext)
expect(result).toEqual('caught error')
})
})
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import enquirieServices from '../services'
import { checkPermission } from '../../../utils/check-permission'
import errors from '../../../errors'
import { queryGetEnquiryById, queryGetEnquiries, mutationCreateEnquiry } from '../resolvers'
import { createEnquiryArgsMock } from '../__stubs__/create-enquiry'
import { enquiryMock } from '../__stubs__/enquiry'
import { enquiriesMock } from '../__stubs__/enquiries'
import { mockContext } from '../../../__stubs__/context'

jest.mock('../services', () => ({
getEnquiryById: jest.fn(() => enquiryMock),
getEnquiries: jest.fn(() => enquiriesMock),
createEnquiry: jest.fn(() => true),
}))
jest.mock('../../../errors', () => ({
generateAuthenticationError: jest.fn(() => 'authentication error'),
}))
jest.mock('../../../logger')
jest.mock('../../../utils/check-permission', () => ({
checkPermission: jest.fn(() => true),
}))

describe('queryGetEnquiryById', () => {
it('should return correctly', () => {
;(checkPermission as jest.Mock).mockReturnValue(true)
const args = { id: 5 }
const result = queryGetEnquiryById(null, args, mockContext)
expect(result).toEqual(enquirieServices.getEnquiryById(args, mockContext))
})

it('should return auth error correctly', () => {
;(checkPermission as jest.Mock).mockReturnValue(false)
const args = { id: 5 }
const result = queryGetEnquiryById(null, args, mockContext)
expect(result).toEqual(errors.generateAuthenticationError(mockContext.traceId))
})
})

describe('queryGetEnquiries', () => {
it('should return correctly', () => {
;(checkPermission as jest.Mock).mockReturnValue(true)
const args = { id: ['id1', 'id2'], pageSize: 10, pageNumber: 1 }
const result = queryGetEnquiries(null, args, mockContext)
expect(result).toEqual(enquirieServices.getEnquiries(args, mockContext))
})

it('should return auth error correctly', () => {
;(checkPermission as jest.Mock).mockReturnValue(false)
const args = { id: ['id1', 'id2'], pageSize: 10, pageNumber: 1 }
const result = queryGetEnquiries(null, args, mockContext)
expect(result).toEqual(errors.generateAuthenticationError(mockContext.traceId))
})
})

describe('mutationCreateEnquiry', () => {
it('should return correctly', () => {
;(checkPermission as jest.Mock).mockReturnValue(true)
const result = mutationCreateEnquiry(null, createEnquiryArgsMock, mockContext)
expect(result).toEqual(enquirieServices.createEnquiry(createEnquiryArgsMock, mockContext))
})

it('should return auth error correctly', () => {
;(checkPermission as jest.Mock).mockReturnValue(false)
const result = mutationCreateEnquiry(null, createEnquiryArgsMock, mockContext)
expect(result).toEqual(errors.generateAuthenticationError(mockContext.traceId))
})
})
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { callGetEnquiryByIdAPI, callGetEnquiriesAPI, callCreateEnquiryAPI } from '../api'
import { mockContext } from '../../../__stubs__/context'
import { createEnquiryArgsMock } from '../__stubs__/create-enquiry'
import { getEnquiryById, getEnquiries, createEnquiry } from '../services'
import { enquiryMock } from '../__stubs__/enquiry'
import { enquiriesMock } from '../__stubs__/enquiries'

jest.mock('../../../logger')
jest.mock('../api', () => ({
callGetEnquiryByIdAPI: jest.fn(() => Promise.resolve(enquiryMock)),
callGetEnquiriesAPI: jest.fn(() => Promise.resolve(enquiriesMock)),
callCreateEnquiryAPI: jest.fn(() => Promise.resolve(true)),
}))

describe('getEnquirieById', () => {
it('should return correctly', async () => {
const args = { id: 5 }
const result = await getEnquiryById(args, mockContext)
expect(callGetEnquiryByIdAPI).toHaveBeenCalledWith(args, mockContext)
expect(result).toEqual(enquiryMock)
})
})

describe('getEnquiries', () => {
it('should return correctly', async () => {
const args = { id: ['id1', 'id2'], pageSize: 10, pageNumber: 1 }
const result = await getEnquiries(args, mockContext)
expect(callGetEnquiriesAPI).toHaveBeenCalledWith(args, mockContext)
expect(result).toEqual(enquiriesMock)
})
})

describe('createEnquirie', () => {
it('should return correctly', async () => {
const result = await createEnquiry(createEnquiryArgsMock, mockContext)
expect(callCreateEnquiryAPI).toHaveBeenCalledWith(createEnquiryArgsMock, mockContext)
expect(result).toEqual(true)
})
})
Loading

0 comments on commit 9e8269b

Please sign in to comment.