diff --git a/packages/component-invite/src/tests/collectionsInvitations/get.test.js b/packages/component-invite/src/tests/collectionsInvitations/get.test.js index 7fe5d99ae8cdd8955c21fca817a91cf6f9a7386f..b594ef52666a89e8e619b7e63862c2e6eb3ea49e 100644 --- a/packages/component-invite/src/tests/collectionsInvitations/get.test.js +++ b/packages/component-invite/src/tests/collectionsInvitations/get.test.js @@ -1,109 +1,99 @@ process.env.NODE_TLS_REJECT_UNAUTHORIZED = '0' process.env.SUPPRESS_NO_CONFIG_WARNING = true -// const httpMocks = require('node-mocks-http') -// const fixtures = require('./../fixtures/fixtures') -// const Model = require('./../helpers/Model') -// const cloneDeep = require('lodash/cloneDeep') +const httpMocks = require('node-mocks-http') +const fixtures = require('./../fixtures/fixtures') +const Model = require('./../helpers/Model') +const cloneDeep = require('lodash/cloneDeep') -// const getPath = '../../routes/collectionsInvitations/get' +const getPath = '../../routes/collectionsInvitations/get' describe('Get collection invitations route handler', () => { - // let testFixtures = {} - // beforeEach(() => { - // testFixtures = cloneDeep(fixtures) - // }) + let testFixtures = {} + beforeEach(() => { + testFixtures = cloneDeep(fixtures) + }) it('should return success when the request data is correct', async () => { - // const { editorInChief, handlingEditor } = testFixtures.users - // const { collection } = testFixtures.collections - // const req = httpMocks.createRequest() - // req.query = { - // role: 'handlingEditor', - // userId: handlingEditor.id, - // } - // req.params.collectionId = collection.id - // req.user = editorInChief.id - // const res = httpMocks.createResponse() - // const models = Model.build() - // await require(getPath)(models)(req, res) - - // expect(res.statusCode).toBe(200) - // const data = JSON.parse(res._getData()) - // expect(data.length).toBeGreaterThan(0) + const { editorInChief, handlingEditor } = testFixtures.users + const { collection } = testFixtures.collections + const req = httpMocks.createRequest() + req.query = { + role: 'handlingEditor', + userId: handlingEditor.id, + } + req.params.collectionId = collection.id + req.user = editorInChief.id + const res = httpMocks.createResponse() + const models = Model.build() + await require(getPath)(models)(req, res) - expect(true).toBeTruthy() + expect(res.statusCode).toBe(200) + const data = JSON.parse(res._getData()) + expect(data.length).toBeGreaterThan(0) }) it('should return an error when parameters are missing', async () => { - // const { editorInChief } = testFixtures.users - // const req = httpMocks.createRequest() - // req.query = {} - // req.user = editorInChief.id - // const res = httpMocks.createResponse() - // const models = Model.build() - // await require(getPath)(models)(req, res) - // expect(res.statusCode).toBe(400) - // const data = JSON.parse(res._getData()) - // expect(data.error).toEqual('Role is required') - - expect(true).toBeTruthy() + const { editorInChief } = testFixtures.users + const req = httpMocks.createRequest() + req.query = {} + req.user = editorInChief.id + const res = httpMocks.createResponse() + const models = Model.build() + await require(getPath)(models)(req, res) + expect(res.statusCode).toBe(400) + const data = JSON.parse(res._getData()) + expect(data.error).toEqual('Role is required') }) it('should return an error when the collection does not exist', async () => { - // const { editorInChief, handlingEditor } = testFixtures.users + const { editorInChief, handlingEditor } = testFixtures.users - // const req = httpMocks.createRequest() - // req.query = { - // role: 'handlingEditor', - // userId: handlingEditor.id, - // } - // req.params.collectionId = 'invalid-id' - // req.user = editorInChief.id - // const res = httpMocks.createResponse() - // const models = Model.build() - // await require(getPath)(models)(req, res) - // expect(res.statusCode).toBe(404) - // const data = JSON.parse(res._getData()) - // expect(data.error).toEqual('collection not found') - - expect(true).toBeTruthy() + const req = httpMocks.createRequest() + req.query = { + role: 'handlingEditor', + userId: handlingEditor.id, + } + req.params.collectionId = 'invalid-id' + req.user = editorInChief.id + const res = httpMocks.createResponse() + const models = Model.build() + await require(getPath)(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 role is invalid', async () => { - // const { editorInChief, handlingEditor } = testFixtures.users - // const { collection } = testFixtures.collections - // const req = httpMocks.createRequest() - // req.query = { - // role: 'invalidRole', - // userId: handlingEditor.id, - // } - // req.params.collectionId = collection.id - // req.user = editorInChief.id - // const res = httpMocks.createResponse() - // const models = Model.build() - // await require(getPath)(models)(req, res) - // expect(res.statusCode).toBe(400) - // const data = JSON.parse(res._getData()) - // expect(data.error).toEqual(`Role ${req.query.role} is invalid`) - - expect(true).toBeTruthy() + const { editorInChief, handlingEditor } = testFixtures.users + const { collection } = testFixtures.collections + const req = httpMocks.createRequest() + req.query = { + role: 'invalidRole', + userId: handlingEditor.id, + } + req.params.collectionId = collection.id + req.user = editorInChief.id + const res = httpMocks.createResponse() + const models = Model.build() + await require(getPath)(models)(req, res) + expect(res.statusCode).toBe(400) + const data = JSON.parse(res._getData()) + expect(data.error).toEqual(`Role ${req.query.role} is invalid`) }) it('should return an error when the collection does not have a the requested role team', async () => { - // const { editorInChief, handlingEditor } = testFixtures.users - // const { collection } = testFixtures.collections - // const req = httpMocks.createRequest() - // req.query = { - // role: 'reviewer', - // userId: handlingEditor.id, - // } - // delete collection.invitations - // req.params.collectionId = collection.id - // req.user = editorInChief.id - // const res = httpMocks.createResponse() - // const models = Model.build() - // await require(getPath)(models)(req, res) - // expect(res.statusCode).toBe(400) - // const data = JSON.parse(res._getData()) - // expect(data.error).toEqual( - // `The requested collection does not have a ${req.query.role} Team`, - // ) - - expect(true).toBeTruthy() + const { editorInChief, handlingEditor } = testFixtures.users + const { collection } = testFixtures.collections + const req = httpMocks.createRequest() + req.query = { + role: 'reviewer', + userId: handlingEditor.id, + } + delete collection.invitations + req.params.collectionId = collection.id + req.user = editorInChief.id + const res = httpMocks.createResponse() + const models = Model.build() + await require(getPath)(models)(req, res) + expect(res.statusCode).toBe(400) + const data = JSON.parse(res._getData()) + expect(data.error).toEqual( + `The requested collection does not have a ${req.query.role} Team`, + ) }) })