Skip to content
Snippets Groups Projects
Commit be599474 authored by Andrei Cioromila's avatar Andrei Cioromila
Browse files

refactor(helper-service): use pubsweet collection method to retrieve all fragments

parent c5692378
No related branches found
No related tags found
3 merge requests!176Sprint 24,!171Sprint 24,!158Hin 1115
...@@ -31,6 +31,7 @@ const collections = { ...@@ -31,6 +31,7 @@ const collections = {
fragments: [fragment.id], fragments: [fragment.id],
owners: [user.id], owners: [user.id],
save: jest.fn(() => collections.collection), save: jest.fn(() => collections.collection),
getFragments: jest.fn(() => [fragment]),
invitations: [ invitations: [
{ {
id: chance.guid(), id: chance.guid(),
...@@ -74,6 +75,7 @@ const collections = { ...@@ -74,6 +75,7 @@ const collections = {
fragments: [fragment.id], fragments: [fragment.id],
owners: [user.id], owners: [user.id],
save: jest.fn(() => collections.collection), save: jest.fn(() => collections.collection),
getFragments: jest.fn(() => [fragment]),
invitations: [ invitations: [
{ {
id: chance.guid(), id: chance.guid(),
...@@ -116,6 +118,7 @@ const collections = { ...@@ -116,6 +118,7 @@ const collections = {
fragments: [fragment1.id, noInvitesFragment.id], fragments: [fragment1.id, noInvitesFragment.id],
owners: [user.id], owners: [user.id],
save: jest.fn(() => collections.collection2), save: jest.fn(() => collections.collection2),
getFragments: jest.fn(() => [fragment1, noInvitesFragment]),
invitations: [ invitations: [
{ {
id: chance.guid(), id: chance.guid(),
...@@ -160,6 +163,7 @@ const collections = { ...@@ -160,6 +163,7 @@ const collections = {
created: chance.timestamp(), created: chance.timestamp(),
customId: '0000001', customId: '0000001',
fragments: [reviewCompletedFragment.id], fragments: [reviewCompletedFragment.id],
getFragments: jest.fn(() => [reviewCompletedFragment]),
invitations: [ invitations: [
{ {
id: chance.guid(), id: chance.guid(),
...@@ -190,6 +194,7 @@ const collections = { ...@@ -190,6 +194,7 @@ const collections = {
fragments: [fragment.id, reviewCompletedFragment.id], fragments: [fragment.id, reviewCompletedFragment.id],
owners: [user.id], owners: [user.id],
save: jest.fn(() => collections.collection), save: jest.fn(() => collections.collection),
getFragments: jest.fn(() => [fragment, reviewCompletedFragment]),
invitations: [ invitations: [
{ {
id: chance.guid(), id: chance.guid(),
...@@ -220,6 +225,7 @@ const collections = { ...@@ -220,6 +225,7 @@ const collections = {
fragments: [], fragments: [],
owners: [user.id], owners: [user.id],
save: jest.fn(() => collections.collection), save: jest.fn(() => collections.collection),
getFragments: jest.fn(() => []),
customId: chance.natural({ min: 999999, max: 9999999 }), customId: chance.natural({ min: 999999, max: 9999999 }),
}, },
noEditorRecomedationCollection: { noEditorRecomedationCollection: {
...@@ -229,6 +235,7 @@ const collections = { ...@@ -229,6 +235,7 @@ const collections = {
fragments: [noEditorRecomedationFragment.id], fragments: [noEditorRecomedationFragment.id],
owners: [user.id], owners: [user.id],
save: jest.fn(() => collections.noEditorRecomedationCollection), save: jest.fn(() => collections.noEditorRecomedationCollection),
getFragments: jest.fn(() => [noEditorRecomedationFragment]),
invitations: [ invitations: [
{ {
id: chance.guid(), id: chance.guid(),
...@@ -271,6 +278,7 @@ const collections = { ...@@ -271,6 +278,7 @@ const collections = {
fragments: [reviewCompletedFragment.id, noInvitesFragment.id], fragments: [reviewCompletedFragment.id, noInvitesFragment.id],
owners: [user.id], owners: [user.id],
save: jest.fn(() => collections.collection), save: jest.fn(() => collections.collection),
getFragments: jest.fn(() => [reviewCompletedFragment, noInvitesFragment]),
invitations: [ invitations: [
{ {
id: chance.guid(), id: chance.guid(),
......
...@@ -108,20 +108,8 @@ class Collection { ...@@ -108,20 +108,8 @@ class Collection {
return lastName || firstName return lastName || firstName
} }
async getAllCollectionFragments({ FragmentModel }) { async getReviewerNumber({ userId }) {
const allCollectionFragments = await Promise.all( const allCollectionFragments = await this.collection.getFragments()
get(this.collection, 'fragments', []).map(async fragmentId => {
const fragment = await FragmentModel.find(fragmentId)
return fragment
}),
)
return allCollectionFragments
}
async getReviewerNumber({ userId, FragmentModel }) {
const allCollectionFragments = await this.getAllCollectionFragments({
FragmentModel,
})
const allCollectionInvitations = flatMap( const allCollectionInvitations = flatMap(
allCollectionFragments, allCollectionFragments,
fragment => fragment.invitations, fragment => fragment.invitations,
......
...@@ -4,48 +4,13 @@ process.env.SUPPRESS_NO_CONFIG_WARNING = true ...@@ -4,48 +4,13 @@ process.env.SUPPRESS_NO_CONFIG_WARNING = true
const { cloneDeep } = require('lodash') const { cloneDeep } = require('lodash')
const fixturesService = require('pubsweet-component-fixture-service') const fixturesService = require('pubsweet-component-fixture-service')
const { fixtures, Model } = fixturesService const { fixtures } = fixturesService
const { Collection } = require('../Helper') const { Collection } = require('../Helper')
describe('Collection helper', () => { describe('Collection helper', () => {
let testFixtures = {} let testFixtures = {}
let models
beforeEach(() => { beforeEach(() => {
testFixtures = cloneDeep(fixtures) 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', () => { describe('getReviewerNumber', () => {
...@@ -53,11 +18,9 @@ describe('Collection helper', () => { ...@@ -53,11 +18,9 @@ describe('Collection helper', () => {
const { collection } = testFixtures.collections const { collection } = testFixtures.collections
const { reviewer } = testFixtures.users const { reviewer } = testFixtures.users
const collectionHelper = new Collection({ collection }) const collectionHelper = new Collection({ collection })
const FragmentModel = models.Fragment
const reviewerNumber = await collectionHelper.getReviewerNumber({ const reviewerNumber = await collectionHelper.getReviewerNumber({
userId: reviewer.id, userId: reviewer.id,
FragmentModel,
}) })
expect(reviewerNumber).toBe(1) expect(reviewerNumber).toBe(1)
...@@ -68,11 +31,9 @@ describe('Collection helper', () => { ...@@ -68,11 +31,9 @@ describe('Collection helper', () => {
const collectionHelper = new Collection({ const collectionHelper = new Collection({
collection: collectionReviewCompleted, collection: collectionReviewCompleted,
}) })
const FragmentModel = models.Fragment
const reviewerNumber = await collectionHelper.getReviewerNumber({ const reviewerNumber = await collectionHelper.getReviewerNumber({
userId: reviewer.id, userId: reviewer.id,
FragmentModel,
}) })
expect(reviewerNumber).toBe(3) expect(reviewerNumber).toBe(3)
...@@ -83,11 +44,9 @@ describe('Collection helper', () => { ...@@ -83,11 +44,9 @@ describe('Collection helper', () => {
const collectionHelper = new Collection({ const collectionHelper = new Collection({
collection: oneReviewedFragmentCollection, collection: oneReviewedFragmentCollection,
}) })
const FragmentModel = models.Fragment
const reviewerNumber = await collectionHelper.getReviewerNumber({ const reviewerNumber = await collectionHelper.getReviewerNumber({
userId: answerReviewer.id, userId: answerReviewer.id,
FragmentModel,
}) })
expect(reviewerNumber).toBe(2) expect(reviewerNumber).toBe(2)
...@@ -98,11 +57,9 @@ describe('Collection helper', () => { ...@@ -98,11 +57,9 @@ describe('Collection helper', () => {
const collectionHelper = new Collection({ const collectionHelper = new Collection({
collection: oneReviewedFragmentCollection, collection: oneReviewedFragmentCollection,
}) })
const FragmentModel = models.Fragment
const reviewerNumber = await collectionHelper.getReviewerNumber({ const reviewerNumber = await collectionHelper.getReviewerNumber({
userId: reviewer.id, userId: reviewer.id,
FragmentModel,
}) })
expect(reviewerNumber).toBe(3) expect(reviewerNumber).toBe(3)
......
...@@ -66,11 +66,10 @@ module.exports = models => async (req, res) => { ...@@ -66,11 +66,10 @@ module.exports = models => async (req, res) => {
} }
const collectionHelper = new Collection({ collection }) const collectionHelper = new Collection({ collection })
const FragmentModel = models.Fragment
const reviewerNumber = await collectionHelper.getReviewerNumber({ const reviewerNumber = await collectionHelper.getReviewerNumber({
userId: req.authInfo.id, userId: req.authInfo.id,
FragmentModel,
}) })
find(fragment.invitations, [ find(fragment.invitations, [
'userId', 'userId',
userId, userId,
......
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