From 1d2f901ae7317b787475e3d7b1c9928f223ea033 Mon Sep 17 00:00:00 2001
From: Sebastian Mihalache <sebastian.mihalache@gmail.con>
Date: Wed, 9 May 2018 10:44:03 +0300
Subject: [PATCH] update post tests

---
 .../routes/fragmentsRecommendations/post.js   |  6 +--
 .../fragmentsRecommendations/post.test.js     | 40 ++++++++++++++++++-
 2 files changed, 41 insertions(+), 5 deletions(-)

diff --git a/packages/component-manuscript-manager/src/routes/fragmentsRecommendations/post.js b/packages/component-manuscript-manager/src/routes/fragmentsRecommendations/post.js
index 64865ee07..1f0050e31 100644
--- a/packages/component-manuscript-manager/src/routes/fragmentsRecommendations/post.js
+++ b/packages/component-manuscript-manager/src/routes/fragmentsRecommendations/post.js
@@ -20,14 +20,12 @@ module.exports = models => async (req, res) => {
     collection = await models.Collection.find(collectionId)
     if (!collection.fragments.includes(fragmentId))
       return res.status(400).json({
-        error: `collection ${
-          collection.id
-        } does not contain fragment ${fragmentId}.`,
+        error: `Collection and fragment do not match.`,
       })
 
     fragment = await models.Fragment.find(fragmentId)
   } catch (e) {
-    const notFoundError = await helpers.handleNotFoundError(e, 'item')
+    const notFoundError = await helpers.handleNotFoundError(e, 'Item')
     return res.status(notFoundError.status).json({
       error: notFoundError.message,
     })
diff --git a/packages/component-manuscript-manager/src/tests/fragmentsRecommendations/post.test.js b/packages/component-manuscript-manager/src/tests/fragmentsRecommendations/post.test.js
index e861ff55a..1bc350601 100644
--- a/packages/component-manuscript-manager/src/tests/fragmentsRecommendations/post.test.js
+++ b/packages/component-manuscript-manager/src/tests/fragmentsRecommendations/post.test.js
@@ -36,7 +36,7 @@ describe('Post collections invitations route handler', () => {
     body = cloneDeep(reqBody)
     models = Model.build(testFixtures)
   })
-  it('should return an error params are missing', async () => {
+  it('should return an error when params are missing', async () => {
     const { reviewer } = testFixtures.users
     delete body.comments
     const res = await requests.sendRequest({
@@ -70,4 +70,42 @@ describe('Post collections invitations route handler', () => {
     const data = JSON.parse(res._getData())
     expect(data.userId).toEqual(reviewer.id)
   })
+  it('should return an error when the fragmentId does not match the collectionId', async () => {
+    const { reviewer } = testFixtures.users
+    const { collection } = testFixtures.collections
+    const { fragment } = testFixtures.fragments
+    collection.fragments.length = 0
+    const res = await requests.sendRequest({
+      body,
+      userId: reviewer.id,
+      models,
+      path,
+      params: {
+        collectionId: collection.id,
+        fragmentId: fragment.id,
+      },
+    })
+
+    expect(res.statusCode).toBe(400)
+    const data = JSON.parse(res._getData())
+    expect(data.error).toEqual('Collection and fragment do not match.')
+  })
+  it('should return an error when the collection does not exist', async () => {
+    const { reviewer } = testFixtures.users
+    const { fragment } = testFixtures.fragments
+    const res = await requests.sendRequest({
+      body,
+      userId: reviewer.id,
+      models,
+      path,
+      params: {
+        collectionId: 'invalid-id',
+        fragmentId: fragment.id,
+      },
+    })
+
+    expect(res.statusCode).toBe(404)
+    const data = JSON.parse(res._getData())
+    expect(data.error).toEqual('Item not found')
+  })
 })
-- 
GitLab