Skip to content

Commit

Permalink
test(formUtils): add unit tests for getFormFieldById
Browse files Browse the repository at this point in the history
  • Loading branch information
karrui committed Apr 15, 2021
1 parent 13858cb commit 1ea1112
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 3 deletions.
68 changes: 66 additions & 2 deletions src/app/modules/form/__tests__/form.utils.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import { Permission } from 'src/types'
import { ObjectId } from 'bson-ext'
import { Types } from 'mongoose'

import { getCollabEmailsWithPermission } from '../form.utils'
import { BasicField, IFieldSchema, Permission } from 'src/types'

import { generateDefaultField } from 'tests/unit/backend/helpers/generate-form-data'

import { getCollabEmailsWithPermission, getFormFieldById } from '../form.utils'

const MOCK_EMAIL_1 = '[email protected]'
const MOCK_EMAIL_2 = '[email protected]'
Expand Down Expand Up @@ -42,4 +47,63 @@ describe('form.utils', () => {
expect(result).toEqual([MOCK_EMAIL_2])
})
})

describe('getFormFieldById', () => {
it('should return form field with valid id when form fields given is a primitive array', async () => {
// Arrange
const fieldToFind = generateDefaultField(BasicField.HomeNo)
const formFields = [generateDefaultField(BasicField.Date), fieldToFind]

// Act
const result = getFormFieldById(formFields, fieldToFind._id)

// Assert
expect(result).toEqual(fieldToFind)
})

it('should return form field with valid id when form fields given is a mongoose document array', async () => {
// Arrange
const fieldToFind = generateDefaultField(BasicField.Number)
// Should not turn this unit test into an integration test, so mocking return and leaving responsibility to mongoose.
const mockDocArray = ({
0: generateDefaultField(BasicField.LongText),
1: fieldToFind,
isMongooseDocumentArray: true,
id: jest.fn().mockReturnValue(fieldToFind),
} as unknown) as Types.DocumentArray<IFieldSchema>

// Act
const result = getFormFieldById(mockDocArray, fieldToFind._id)

// Assert
expect(result).toEqual(fieldToFind)
expect(mockDocArray.id).toHaveBeenCalledWith(fieldToFind._id)
})

it('should return null when given form fields are undefined', async () => {
// Arrange
const someFieldId = new ObjectId()

// Act
const result = getFormFieldById(undefined, someFieldId)

// Assert
expect(result).toEqual(null)
})

it('should return null when no fields correspond to given field id', async () => {
// Arrange
const invalidFieldId = new ObjectId()
const formFields = [
generateDefaultField(BasicField.Date),
generateDefaultField(BasicField.Date),
]

// Act
const result = getFormFieldById(formFields, invalidFieldId)

// Assert
expect(result).toEqual(null)
})
})
})
2 changes: 1 addition & 1 deletion src/app/modules/form/form.utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ export const isMongooseDocumentArray = <T extends Document>(
*/
export const getFormFieldById = (
formFields: IFormSchema['form_fields'],
fieldId: string,
fieldId: IFieldSchema['_id'],
): IFieldSchema | null => {
if (!formFields) {
return null
Expand Down

0 comments on commit 1ea1112

Please sign in to comment.