Newer
Older

Sebastian Mihalache
committed
process.env.NODE_TLS_REJECT_UNAUTHORIZED = '0'
process.env.SUPPRESS_NO_CONFIG_WARNING = true
const Chance = require('chance')
const config = require('config')

Sebastian Mihalache
committed
const { cloneDeep } = require('lodash')
const fixturesService = require('pubsweet-component-fixture-service')
const chance = new Chance()
const { fixtures, Model } = fixturesService

Sebastian Mihalache
committed
const { Fragment } = require('../Helper')
const { recommendations: configRecommendations } = config

Sebastian Mihalache
committed
const acceptedReviewerId = chance.guid()
const submittedReviewerId1 = chance.guid()
const submittedReviewerId2 = chance.guid()

Tania Fecheta
committed
const handlingEditorId = chance.guid()
const editorInChiefId = chance.guid()

Sebastian Mihalache
committed
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
const fragment = {
invitations: [
{
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: submittedReviewerId1,
invitedOn: chance.timestamp(),
respondedOn: chance.timestamp(),
type: 'invitation',
},
{
id: chance.guid(),
role: 'reviewer',
hasAnswer: true,
isAccepted: true,
userId: submittedReviewerId2,
invitedOn: chance.timestamp(),
respondedOn: chance.timestamp(),
type: 'invitation',
},
],
recommendations: [
{
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: submittedReviewerId1,
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: submittedReviewerId2,
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(),
},
],
}

Sebastian Mihalache
committed
describe('Fragment helper', () => {
let testFixtures = {}

Sebastian Mihalache
committed
let testFragment = {}

Sebastian Mihalache
committed
beforeEach(() => {
testFixtures = cloneDeep(fixtures)

Sebastian Mihalache
committed
testFragment = cloneDeep(fragment)

Sebastian Mihalache
committed
})
describe('hasReviewReport', () => {
it('should return true if the fragment has a review report', () => {
const { fragment } = testFixtures.fragments
const fragmentHelper = new Fragment({ fragment })

Sebastian Mihalache
committed
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()
})

Sebastian Mihalache
committed
})
describe('getInvitations', () => {

Sebastian Mihalache
committed
it('should return accepted invitations if type is accepted and a review report has been started', () => {
const fragmentHelper = new Fragment({ fragment: testFragment })

Sebastian Mihalache
committed
const acceptedInvitations = fragmentHelper.getInvitations({
isAccepted: true,
type: 'accepted',
})

Sebastian Mihalache
committed
expect(acceptedInvitations).toHaveLength(1)
})
it('should return accepted invitations if type is accepted and no review report has been started', () => {
testFragment.recommendations = [
{
recommendation: 'publish',
recommendationType: 'review',
comments: [
{
content: chance.paragraph(),
public: chance.bool(),
files: [
{
id: chance.guid(),
name: 'file.pdf',
size: chance.natural(),
},
],
},
],
id: chance.guid(),

Sebastian Mihalache
committed
userId: submittedReviewerId1,
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(),

Sebastian Mihalache
committed
userId: submittedReviewerId2,
createdOn: chance.timestamp(),
updatedOn: chance.timestamp(),

Sebastian Mihalache
committed
submittedOn: chance.timestamp(),

Sebastian Mihalache
committed
]

Sebastian Mihalache
committed
const fragmentHelper = new Fragment({ fragment: testFragment })
const acceptedInvitations = fragmentHelper.getInvitations({
isAccepted: true,
type: 'accepted',
})

Sebastian Mihalache
committed
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
expect(acceptedInvitations).toHaveLength(1)
})
it('should return invitations of submitted reviewers if type is submitted', () => {
const fragmentHelper = new Fragment({ fragment: testFragment })
const submittedInvitations = fragmentHelper.getInvitations({
isAccepted: true,
type: 'submitted',
})
expect(submittedInvitations).toHaveLength(2)
})
it('should return invitations of pending reviewers if type is pending and isAccepted is false', () => {
testFragment.invitations.push({
id: chance.guid(),
role: 'reviewer',
hasAnswer: false,
isAccepted: false,
userId: chance.guid(),
invitedOn: chance.timestamp(),
respondedOn: chance.timestamp(),
type: 'invitation',
})
const fragmentHelper = new Fragment({ fragment: testFragment })
const pendingInvitations = fragmentHelper.getInvitations({
isAccepted: false,
type: 'pending',
})
expect(pendingInvitations).toHaveLength(1)

Sebastian Mihalache
committed
})
describe('getReviewersAndEditorsData', () => {
it('should return an array of users (reviewers, editors) that have submitted a review', async () => {
const { collection } = testFixtures.collections
const { fragment } = testFixtures.fragments
const fragmentHelper = new Fragment({ fragment })
const fragmentUsers = await fragmentHelper.getReviewersAndEditorsData({
collection,
UserModel: models.User,
})
expect(fragmentUsers.length).toBeGreaterThan(0)
const submittedRecommendations = fragment.recommendations.filter(
rec =>
rec.recommendationType === configRecommendations.type.editor ||
(rec.recommendationType === configRecommendations.type.review &&
rec.submittedOn),
)
expect(fragmentUsers).toHaveLength(submittedRecommendations.length)

Sebastian Mihalache
committed
it('should return an error when the collection does not have a handling editor', async () => {
const { collection } = testFixtures.collections
const { fragment } = testFixtures.fragments
const fragmentHelper = new Fragment({ fragment })
delete collection.handlingEditor
try {
await fragmentHelper.getReviewersAndEditorsData({
collection,
UserModel: models.User,
})
} catch (e) {
expect(e.message).toEqual(
`Collection ${collection.id} does not have a Handling Editor`,
)
}
})

Tania Fecheta
committed
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
describe('canHEMakeAnotherRecommendation', () => {
it('should return true when He makes a recommendation after EIC decision was to return to HE', async () => {
testFragment.recommendations = [
{
recommendation: 'publish',
recommendationType: 'editorRecommendation',
comments: [
{
content: chance.paragraph(),
public: true,
files: [
{
id: chance.guid(),
name: 'file.pdf',
size: chance.natural(),
},
],
},
],
id: chance.guid(),
userId: handlingEditorId,
createdOn: 1542361074012,
updatedOn: chance.timestamp(),
},
{
recommendation: 'return-to-handling-editor',
recommendationType: 'editorRecommendation',
comments: [
{
content: chance.paragraph(),
public: true,
files: [
{
id: chance.guid(),
name: 'file.pdf',
size: chance.natural(),
},
],
},
],
id: chance.guid(),
userId: editorInChiefId,
createdOn: 1542361115749,
updatedOn: chance.timestamp(),
},
]
const currentUserRecommendations = testFragment.recommendations.filter(
r => r.userId === handlingEditorId,
)
const fragmentHelper = new Fragment({ fragment: testFragment })
const canHEMakeAnotherRecommendation = await fragmentHelper.canHEMakeAnotherRecommendation(
currentUserRecommendations,
)
expect(canHEMakeAnotherRecommendation).toBe(true)
})
it('should return false when He makes another recommendation', async () => {
testFragment.recommendations = [
{
recommendation: 'publish',
recommendationType: 'editorRecommendation',
comments: [
{
content: chance.paragraph(),
public: true,
files: [
{
id: chance.guid(),
name: 'file.pdf',
size: chance.natural(),
},
],
},
],
id: chance.guid(),
userId: handlingEditorId,
createdOn: 1542361074012,
updatedOn: chance.timestamp(),
},
]
const currentUserRecommendations = testFragment.recommendations.filter(
r => r.userId === handlingEditorId,
)
const fragmentHelper = new Fragment({ fragment: testFragment })
const canHEMakeAnotherRecommendation = await fragmentHelper.canHEMakeAnotherRecommendation(
currentUserRecommendations,
)
expect(canHEMakeAnotherRecommendation).toBe(false)
})
})