From 79225893d457651e2822e632341261f14eb64bd9 Mon Sep 17 00:00:00 2001 From: Sebastian Mihalache <sebi.mihalache@gmail.com> Date: Fri, 26 Oct 2018 13:45:29 +0300 Subject: [PATCH] fix(fragmentHelper): write unit tests for getReviewers --- .../src/services/Fragment.js | 1 + .../src/tests/fragment.test.js | 109 ++++++++++++++++-- 2 files changed, 101 insertions(+), 9 deletions(-) diff --git a/packages/component-helper-service/src/services/Fragment.js b/packages/component-helper-service/src/services/Fragment.js index 174067efd..be5aecf95 100644 --- a/packages/component-helper-service/src/services/Fragment.js +++ b/packages/component-helper-service/src/services/Fragment.js @@ -92,6 +92,7 @@ class Fragment { getInvitations({ isAccepted = true, role = 'reviewer', type }) { const { fragment: { invitations = [], recommendations = [] } } = this + let filteredInvitations = isAccepted ? invitations.filter( inv => inv.role === role && inv.hasAnswer && inv.isAccepted, diff --git a/packages/component-helper-service/src/tests/fragment.test.js b/packages/component-helper-service/src/tests/fragment.test.js index 3b5875eaf..8439942e2 100644 --- a/packages/component-helper-service/src/tests/fragment.test.js +++ b/packages/component-helper-service/src/tests/fragment.test.js @@ -3,7 +3,9 @@ process.env.SUPPRESS_NO_CONFIG_WARNING = true const { cloneDeep } = require('lodash') const fixturesService = require('pubsweet-component-fixture-service') +const Chance = require('chance') +const chance = new Chance() const { fixtures } = fixturesService const { Fragment } = require('../Helper') @@ -13,17 +15,106 @@ describe('Fragment helper', () => { testFixtures = cloneDeep(fixtures) }) - it('hasReviewReport - should return true if the fragment has a review report', () => { - const { fragment } = testFixtures.fragments - const fragmentHelper = new Fragment({ fragment }) + describe('hasReviewReport', () => { + it('should return true if the fragment has a review report', () => { + const { fragment } = testFixtures.fragments + const fragmentHelper = new Fragment({ fragment }) - expect(fragmentHelper.hasReviewReport()).toBeTruthy() + expect(fragmentHelper.hasReviewReport()).toBeTruthy() + }) + it('should return false if the fragment does not have a review report', () => { + const { fragment } = testFixtures.fragments + fragment.recommendations = [] + const fragmentHelper = new Fragment({ fragment }) + + expect(fragmentHelper.hasReviewReport()).toBeFalsy() + }) }) - it('hasReviewReport - should return false if the fragment does not have a review report', () => { - const { fragment } = testFixtures.fragments - fragment.recommendations = [] - const fragmentHelper = new Fragment({ fragment }) - expect(fragmentHelper.hasReviewReport()).toBeFalsy() + describe('getInvitations', () => { + it('should return accepted invitations if type is accepted', () => { + const { fragment } = testFixtures.fragments + const acceptedReviewerId = chance.guid() + const submittedReviewerId = chance.guid() + + fragment.invitations.push( + { + id: chance.guid(), + role: 'reviewer', + hasAnswer: true, + isAccepted: true, + userId: acceptedReviewerId, + invitedOn: chance.timestamp(), + respondedOn: chance.timestamp(), + type: 'invitation', + }, + { + id: chance.guid(), + role: 'reviewer', + hasAnswer: true, + isAccepted: true, + userId: submittedReviewerId, + invitedOn: chance.timestamp(), + respondedOn: chance.timestamp(), + type: 'invitation', + }, + ) + + fragment.recommendations.push( + { + recommendation: 'publish', + recommendationType: 'review', + comments: [ + { + content: chance.paragraph(), + public: chance.bool(), + files: [ + { + id: chance.guid(), + name: 'file.pdf', + size: chance.natural(), + }, + ], + }, + ], + id: chance.guid(), + userId: submittedReviewerId, + createdOn: chance.timestamp(), + updatedOn: chance.timestamp(), + submittedOn: chance.timestamp(), + }, + { + recommendation: 'publish', + recommendationType: 'review', + comments: [ + { + content: chance.paragraph(), + public: chance.bool(), + files: [ + { + id: chance.guid(), + name: 'file.pdf', + size: chance.natural(), + }, + ], + }, + ], + id: chance.guid(), + userId: acceptedReviewerId, + createdOn: chance.timestamp(), + updatedOn: chance.timestamp(), + // submittedOn: chance.timestamp(), + }, + ) + + const fragmentHelper = new Fragment({ fragment }) + + const acceptedInvitations = fragmentHelper.getInvitations({ + isAccepted: true, + type: 'accepted', + }) + + console.log(acceptedInvitations) + }) }) }) -- GitLab