Skip to content
Snippets Groups Projects
Commit b7cc0dd5 authored by Sebastian's avatar Sebastian
Browse files

tests(components-invite): add some tests for post assignation

parent 6d64770d
No related branches found
No related tags found
No related merge requests found
......@@ -45,7 +45,7 @@ module.exports = models => async (req, res) => {
await models.Collection.find(collectionId)
// TODO: create a team and add the team id to the user's teams array
user.assignation.hasAnswer = true
if (accept) {
if (accept === true) {
user.assignation.isAccepted = true
}
await user.save()
......
......@@ -4,6 +4,7 @@ process.env.SUPPRESS_NO_CONFIG_WARNING = true
const httpMocks = require('node-mocks-http')
const fixtures = require('./fixtures/fixtures')
const UserMock = require('./mocks/User')
const cloneDeep = require('lodash/cloneDeep')
jest.mock('pubsweet-component-mail-service', () => ({
setupAssignEmail: jest.fn(),
......@@ -38,6 +39,7 @@ const { standardCollection } = fixtures.collections
const postAssignationPath = '../routes/postAssignation'
describe('Post assignation route handler', () => {
it('should return success when the handling editor accepts work on a collection', async () => {
const acceptingHE = cloneDeep(handlingEditor)
const body = {
type: 'handlingEditor',
accept: true,
......@@ -45,13 +47,83 @@ describe('Post assignation route handler', () => {
const req = httpMocks.createRequest({
body,
})
req.user = acceptingHE
const res = httpMocks.createResponse()
const models = buildModels(standardCollection, acceptingHE)
await require(postAssignationPath)(models)(req, res)
expect(res.statusCode).toBe(204)
expect(acceptingHE.assignation.hasAnswer).toBeTruthy()
expect(acceptingHE.assignation.isAccepted).toBeTruthy()
})
it('should return success when the handling editor refuses work on a collection', async () => {
const refusingHE = cloneDeep(handlingEditor)
const body = {
type: 'handlingEditor',
accept: false,
}
const req = httpMocks.createRequest({
body,
})
req.user = refusingHE
const res = httpMocks.createResponse()
const models = buildModels(standardCollection, refusingHE)
await require(postAssignationPath)(models)(req, res)
expect(res.statusCode).toBe(204)
expect(refusingHE.assignation.hasAnswer).toBeTruthy()
expect(refusingHE.assignation.isAccepted).toBeFalsy()
})
it('should return an error params are missing', async () => {
const body = {
type: 'handlingEditor',
}
const req = httpMocks.createRequest({
body,
})
req.user = handlingEditor
const res = httpMocks.createResponse()
const models = buildModels(standardCollection, handlingEditor)
await require(postAssignationPath)(models)(req, res)
expect(res.statusCode).toBe(204)
expect(handlingEditor.assignation.hasAnswer).toBeTruthy()
expect(handlingEditor.assignation.isAccepted).toBeTruthy()
expect(res.statusCode).toBe(400)
const data = JSON.parse(res._getData())
expect(data.error).toEqual('Type and accept are required')
})
it('should return an error if the collection id does not exists', async () => {
const body = {
type: 'handlingEditor',
accept: false,
}
const req = httpMocks.createRequest({
body,
})
req.user = handlingEditor
const res = httpMocks.createResponse()
const models = buildModels(notFoundError, handlingEditor)
await require(postAssignationPath)(models)(req, res)
expect(res.statusCode).toBe(404)
const data = JSON.parse(res._getData())
expect(data.error).toEqual('collection not found')
})
it('should return an error when the request user does not have any assignation', async () => {
const noAssignationEditor = cloneDeep(handlingEditor)
const body = {
type: 'handlingEditor',
accept: false,
}
const req = httpMocks.createRequest({
body,
})
delete noAssignationEditor.assignation
req.user = noAssignationEditor
const res = httpMocks.createResponse()
const models = buildModels(standardCollection, noAssignationEditor)
await require(postAssignationPath)(models)(req, res)
expect(res.statusCode).toBe(400)
const data = JSON.parse(res._getData())
expect(data.error).toEqual('The user has no assignation')
})
})
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment