From 1ea111242a42ca76f7f73490c1c2c75f3d9bf2d4 Mon Sep 17 00:00:00 2001 From: Kar Rui Lau Date: Wed, 14 Apr 2021 11:56:07 +0800 Subject: [PATCH] test(formUtils): add unit tests for getFormFieldById --- .../modules/form/__tests__/form.utils.spec.ts | 68 ++++++++++++++++++- src/app/modules/form/form.utils.ts | 2 +- 2 files changed, 67 insertions(+), 3 deletions(-) diff --git a/src/app/modules/form/__tests__/form.utils.spec.ts b/src/app/modules/form/__tests__/form.utils.spec.ts index 968f45c678..0f63b8504c 100644 --- a/src/app/modules/form/__tests__/form.utils.spec.ts +++ b/src/app/modules/form/__tests__/form.utils.spec.ts @@ -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 = 'a@abc.com' const MOCK_EMAIL_2 = 'b@def.com' @@ -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 + + // 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) + }) + }) }) diff --git a/src/app/modules/form/form.utils.ts b/src/app/modules/form/form.utils.ts index d173245d65..2694886565 100644 --- a/src/app/modules/form/form.utils.ts +++ b/src/app/modules/form/form.utils.ts @@ -102,7 +102,7 @@ export const isMongooseDocumentArray = ( */ export const getFormFieldById = ( formFields: IFormSchema['form_fields'], - fieldId: string, + fieldId: IFieldSchema['_id'], ): IFieldSchema | null => { if (!formFields) { return null