-
Notifications
You must be signed in to change notification settings - Fork 87
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test(formUtils): add unit tests for getFormFieldById
- Loading branch information
Showing
2 changed files
with
67 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]' | ||
|
@@ -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) | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters