Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(api-refactor): add specific update end page endpoint in server #1760

Merged
merged 9 commits into from
May 4, 2021
76 changes: 76 additions & 0 deletions src/app/models/__tests__/form.server.model.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import getFormModel, {
} from 'src/app/models/form.server.model'
import {
BasicField,
EndPage,
FormFieldWithId,
IEncryptedForm,
IFieldSchema,
Expand Down Expand Up @@ -1202,6 +1203,81 @@ describe('Form Model', () => {
})
})
})

describe('updateEndPageById', () => {
it('should update end page and return updated form when successful', async () => {
karrui marked this conversation as resolved.
Show resolved Hide resolved
// Arrange
const formParams = merge({}, MOCK_EMAIL_FORM_PARAMS, {
admin: MOCK_ADMIN_OBJ_ID,
endPage: {
title: 'old title',
},
})
const form = (await Form.create(formParams)).toObject()
const updatedEndPage: EndPage = {
title: 'some new title',
paragraph: 'some description paragraph',
buttonText: 'custom button text',
}

// Act
const actual = await Form.updateEndPageById(form._id, updatedEndPage)

// Assert
// Should have defaults populated but also replace the endpage with the new params
expect(actual?.toObject()).toEqual({
...form,
lastModified: expect.any(Date),
endPage: { ...updatedEndPage },
})
})

it('should update end page with defaults when optional values are not provided', async () => {
// Arrange
const formParams = merge({}, MOCK_EMAIL_FORM_PARAMS, {
admin: MOCK_ADMIN_OBJ_ID,
})
const form = (await Form.create(formParams)).toObject()
const updatedEndPage: EndPage = {
paragraph: 'some description paragraph',
}

// Act
const actual = await Form.updateEndPageById(form._id, updatedEndPage)

// Assert
// Should have defaults populated but also replace the endpage with the new params
expect(actual?.toObject()).toEqual({
...form,
lastModified: expect.any(Date),
endPage: {
...updatedEndPage,
// Defaults should be populated and returned
buttonText: 'Submit another form',
title: 'Thank you for filling out the form.',
},
})
})

it('should return null when formId given is not in the database', async () => {
// Arrange
await expect(Form.countDocuments()).resolves.toEqual(0)
const updatedEndPage: EndPage = {
title: 'some new title',
paragraph: 'does not really matter',
}

// Act
const actual = await Form.updateEndPageById(
new ObjectId().toHexString(),
updatedEndPage,
)

// Assert
expect(actual).toEqual(null)
await expect(Form.countDocuments()).resolves.toEqual(0)
})
})
})

describe('Methods', () => {
Expand Down
13 changes: 13 additions & 0 deletions src/app/models/form.server.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
AuthType,
BasicField,
Colors,
EndPage,
FormField,
FormFieldWithId,
FormLogoState,
Expand Down Expand Up @@ -684,6 +685,18 @@ const compileFormModel = (db: Mongoose): IFormModel => {
).exec()
}

FormSchema.statics.updateEndPageById = async function (
this: IFormModel,
formId: string,
newEndPage: EndPage,
) {
return this.findByIdAndUpdate(
formId,
{ endPage: newEndPage },
{ new: true, runValidators: true },
).exec()
}

// Hooks
FormSchema.pre<IFormSchema>('validate', function (next) {
// Reject save if form document is too large
Expand Down
Loading