Skip to content
Snippets Groups Projects
Commit e07e6603 authored by Mihail Hagiu's avatar Mihail Hagiu
Browse files

test(ReviewerNumbering): Wrote unit tests for assigning reviewer number

parent 2c4677fc
No related branches found
No related tags found
3 merge requests!176Sprint 24,!171Sprint 24,!158Hin 1115
...@@ -7,4 +7,5 @@ module.exports = { ...@@ -7,4 +7,5 @@ module.exports = {
collectionReviewCompletedID: chance.guid(), collectionReviewCompletedID: chance.guid(),
collectionNoInvitesID: chance.guid(), collectionNoInvitesID: chance.guid(),
twoVersionsCollectionId: chance.guid(), twoVersionsCollectionId: chance.guid(),
oneReviewedFragmentCollectionID: chance.guid(),
} }
...@@ -11,6 +11,7 @@ const { ...@@ -11,6 +11,7 @@ const {
collectionReviewCompletedID, collectionReviewCompletedID,
collectionNoInvitesID, collectionNoInvitesID,
twoVersionsCollectionId, twoVersionsCollectionId,
oneReviewedFragmentCollectionID,
} = require('./collectionIDs') } = require('./collectionIDs')
const chance = new Chance() const chance = new Chance()
...@@ -214,6 +215,36 @@ const collections = { ...@@ -214,6 +215,36 @@ const collections = {
save: jest.fn(() => collections.collection), save: jest.fn(() => collections.collection),
customId: chance.natural({ min: 999999, max: 9999999 }), customId: chance.natural({ min: 999999, max: 9999999 }),
}, },
oneReviewedFragmentCollection: {
id: oneReviewedFragmentCollectionID,
title: chance.sentence(),
type: 'collection',
fragments: [reviewCompletedFragment.id, noInvitesFragment.id],
owners: [user.id],
save: jest.fn(() => collections.collection),
invitations: [
{
id: chance.guid(),
role: 'handlingEditor',
hasAnswer: true,
isAccepted: false,
userId: handlingEditor.id,
invitedOn: chance.timestamp(),
respondedOn: null,
},
],
handlingEditor: {
id: handlingEditor.id,
hasAnswer: false,
isAccepted: false,
email: handlingEditor.email,
invitedOn: chance.timestamp(),
respondedOn: null,
name: `${handlingEditor.firstName} ${handlingEditor.lastName}`,
},
status: 'revisionRequested',
customId: chance.natural({ min: 999999, max: 9999999 }),
},
} }
module.exports = collections module.exports = collections
...@@ -265,6 +265,7 @@ const fragments = { ...@@ -265,6 +265,7 @@ const fragments = {
invitedOn: chance.timestamp(), invitedOn: chance.timestamp(),
isAccepted: true, isAccepted: true,
respondedOn: chance.timestamp(), respondedOn: chance.timestamp(),
reviewerNumber: 2,
}, },
{ {
id: chance.guid(), id: chance.guid(),
......
...@@ -108,7 +108,7 @@ class Collection { ...@@ -108,7 +108,7 @@ class Collection {
async getAllCollectionFragments({ FragmentModel }) { async getAllCollectionFragments({ FragmentModel }) {
const allCollectionFragments = await Promise.all( const allCollectionFragments = await Promise.all(
this.collection.fragments.map(async fragmentId => { get(this.collection, 'fragments', []).map(async fragmentId => {
const fragment = await FragmentModel.find(fragmentId) const fragment = await FragmentModel.find(fragmentId)
return fragment return fragment
}), }),
...@@ -116,47 +116,25 @@ class Collection { ...@@ -116,47 +116,25 @@ class Collection {
return allCollectionFragments return allCollectionFragments
} }
async getAllCollectionInvitations({ FragmentModel }) { async getReviewerNumber({ reqUser, FragmentModel }) {
const allCollectionFragments = await this.getAllCollectionFragments({ const allCollectionFragments = await this.getAllCollectionFragments({
FragmentModel, FragmentModel,
}) })
const allCollectionInvitations = flatten( const allCollectionInvitations = flatten(
allCollectionFragments.map(fragment => fragment.invitations), allCollectionFragments.map(fragment => fragment.invitations),
) )
return allCollectionInvitations const allNumberedInvitationsForUser = allCollectionInvitations
}
async getAllNumberedInvitationsForUser({ reqUser, FragmentModel }) {
const allCollectionInvitations = await this.getAllCollectionInvitations({
FragmentModel,
})
return allCollectionInvitations
.filter(invite => invite.userId === reqUser.id) .filter(invite => invite.userId === reqUser.id)
.filter(invite => invite.reviewerNumber) .filter(invite => invite.reviewerNumber)
}
async getMaxReviewerNumber({ FragmentModel }) {
const allCollectionInvitations = await this.getAllCollectionInvitations({
FragmentModel,
})
const maxReviewerNumber = get(
maxBy(allCollectionInvitations, 'reviewerNumber'),
'reviewerNumber',
0,
)
return maxReviewerNumber
}
async getReviewerNumber({ reqUser, FragmentModel }) { if (isEmpty(allNumberedInvitationsForUser)) {
// maybe i should get fragments here and pass them further const maxReviewerNumber = get(
const allNumberedInvitationsForUser = await this.getAllNumberedInvitationsForUser( maxBy(allCollectionInvitations, 'reviewerNumber'),
{ 'reviewerNumber',
reqUser, 0,
FragmentModel, )
}, return maxReviewerNumber + 1
) }
if (isEmpty(allNumberedInvitationsForUser))
return this.getMaxReviewerNumber() + 1
return allNumberedInvitationsForUser[0].reviewerNumber return allNumberedInvitationsForUser[0].reviewerNumber
} }
} }
......
process.env.NODE_TLS_REJECT_UNAUTHORIZED = '0'
process.env.SUPPRESS_NO_CONFIG_WARNING = true
const { cloneDeep } = require('lodash')
const fixturesService = require('pubsweet-component-fixture-service')
const { fixtures, Model } = fixturesService
const { Collection } = require('../Helper')
describe('Collection helper', () => {
let testFixtures = {}
let models
beforeEach(() => {
testFixtures = cloneDeep(fixtures)
models = Model.build(testFixtures)
})
describe('getAllCollectionFragments', () => {
it('should return an array with all collection fragments', async () => {
const { collection } = testFixtures.collections
const collectionHelper = new Collection({ collection })
const FragmentModel = models.Fragment
const numberOfFragments = collection.fragments.length
const allCollectionFragments = await collectionHelper.getAllCollectionFragments(
{
FragmentModel,
},
)
expect(allCollectionFragments).toHaveLength(numberOfFragments)
expect(typeof allCollectionFragments[0]).toBe('object')
expect(allCollectionFragments[0].type).toBe('fragment')
})
it('should return an empty array when no fragments exist', async () => {
const { noFragmentsCollection } = testFixtures.collections
const collectionHelper = new Collection({ noFragmentsCollection })
const FragmentModel = models.Fragment
const allCollectionFragments = await collectionHelper.getAllCollectionFragments(
{
FragmentModel,
},
)
expect(allCollectionFragments).toHaveLength(0)
expect(typeof allCollectionFragments[0]).toBe('undefined')
})
})
describe('getReviewerNumber', () => {
it('should assign reviewer number 1 on invitation if no other reviewer numbers exist', async () => {
const { collection } = testFixtures.collections
const { reviewer } = testFixtures.users
const collectionHelper = new Collection({ collection })
const FragmentModel = models.Fragment
const reviewerNumber = await collectionHelper.getReviewerNumber({
reqUser: reviewer,
FragmentModel,
})
expect(reviewerNumber).toBe(1)
})
it('should assign next reviewer number on invitation if another reviewer numbers exist', async () => {
const { collectionReviewCompleted } = testFixtures.collections
const { reviewer } = testFixtures.users
const collectionHelper = new Collection({
collection: collectionReviewCompleted,
})
const FragmentModel = models.Fragment
const reviewerNumber = await collectionHelper.getReviewerNumber({
reqUser: reviewer,
FragmentModel,
})
expect(reviewerNumber).toBe(3)
})
it('should keep reviewer number across fragment versions', async () => {
const { oneReviewedFragmentCollection } = testFixtures.collections
const { answerReviewer } = testFixtures.users
const collectionHelper = new Collection({
collection: oneReviewedFragmentCollection,
})
const FragmentModel = models.Fragment
const reviewerNumber = await collectionHelper.getReviewerNumber({
reqUser: answerReviewer,
FragmentModel,
})
expect(reviewerNumber).toBe(2)
})
it('should assign next reviewer number across fragment versions', async () => {
const { oneReviewedFragmentCollection } = testFixtures.collections
const { reviewer } = testFixtures.users
const collectionHelper = new Collection({
collection: oneReviewedFragmentCollection,
})
const FragmentModel = models.Fragment
const reviewerNumber = await collectionHelper.getReviewerNumber({
reqUser: reviewer,
FragmentModel,
})
expect(reviewerNumber).toBe(3)
})
})
})
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