Skip to content

Commit

Permalink
After removing the last author, reask the 'Did you write the PRErevie…
Browse files Browse the repository at this point in the history
…w with anyone else?' question

Rather than presenting the user with an empty list, this change directs them to the step where they either have to add a new author or say they did it alone.

Refs #388
  • Loading branch information
thewilkybarkid committed Oct 20, 2022
1 parent 2ce26a4 commit 74e2f62
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 6 deletions.
18 changes: 12 additions & 6 deletions src/write-review/write-review-remove-author.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import * as M from 'hyper-ts/lib/Middleware'
import * as RM from 'hyper-ts/lib/ReaderMiddleware'
import * as D from 'io-ts/Decoder'
import { get } from 'spectacles-ts'
import { match } from 'ts-pattern'
import { P, match } from 'ts-pattern'
import { canAddAuthors } from '../feature-flags'
import { MissingE, hasAnError, missingE } from '../form'
import { html, plainText, rawHtml, sendHtml } from '../html'
Expand Down Expand Up @@ -113,16 +113,22 @@ const handleRemoveAuthorForm = ({
match(state)
.with({ removeAuthor: 'yes' }, () =>
pipe(
{
RM.of({
otherAuthors: pipe(
form.otherAuthors ?? [],
RA.deleteAt(author.index),
O.getOrElse(() => form.otherAuthors),
),
},
updateForm(form),
RM.fromReaderTaskK(saveForm(user.orcid, preprint.doi)),
RM.ichainMiddlewareK(() => seeOther(format(writeReviewAddAuthorsMatch.formatter, { doi: preprint.doi }))),
}),
RM.map(updateForm(form)),
RM.chainFirstReaderTaskK(saveForm(user.orcid, preprint.doi)),
RM.ichainMiddlewareK(form =>
match(form)
.with({ otherAuthors: P.optional([]) }, () =>
seeOther(format(writeReviewAuthorsMatch.formatter, { doi: preprint.doi })),
)
.otherwise(() => seeOther(format(writeReviewAddAuthorsMatch.formatter, { doi: preprint.doi }))),
),
),
)
.with(
Expand Down
76 changes: 76 additions & 0 deletions test/write-review/write-review-remove-author.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ describe('writeReviewRemoveAuthor', () => {
moreAuthors: fc.constant('yes'),
otherAuthors: fc.nonEmptyArray(
fc.record({ name: fc.nonEmptyString(), orcid: fc.orcid() }, { requiredKeys: ['name'] }),
{ minLength: 2 },
),
persona: fc.constantFrom('public', 'pseudonym'),
review: fc.nonEmptyString(),
Expand Down Expand Up @@ -172,6 +173,81 @@ describe('writeReviewRemoveAuthor', () => {
)
})

test('when there are no authors left', async () => {
await fc.assert(
fc.asyncProperty(
fc.preprintDoi(),
fc.record({ title: fc.html(), language: fc.languageCode() }),
fc.tuple(fc.uuid(), fc.string()).chain(([sessionId, secret]) =>
fc.tuple(
fc.connection({
body: fc.constant({ removeAuthor: 'yes' }),
headers: fc.constant({ Cookie: `session=${cookieSignature.sign(sessionId, secret)}` }),
method: fc.constant('POST'),
}),
fc.constant(sessionId),
fc.constant(secret),
),
),
fc.user(),
fc.record(
{
competingInterests: fc.constantFrom('yes', 'no'),
competingInterestsDetails: fc.lorem(),
conduct: fc.constant('yes'),
moreAuthors: fc.constant('yes'),
otherAuthors: fc.tuple(
fc.record({ name: fc.nonEmptyString(), orcid: fc.orcid() }, { requiredKeys: ['name'] }),
),
persona: fc.constantFrom('public', 'pseudonym'),
review: fc.nonEmptyString(),
},
{ requiredKeys: ['moreAuthors', 'otherAuthors'] },
),
async (preprintDoi, preprintTitle, [connection, sessionId, secret], user, newReview) => {
const sessionStore = new Keyv()
await sessionStore.set(sessionId, UserC.encode(user))
const formStore = new Keyv()
await formStore.set(`${user.orcid}_${preprintDoi}`, newReview)
const canAddAuthors: jest.MockedFunction<CanAddAuthorsEnv['canAddAuthors']> = jest.fn(_ => true)
const getPreprintTitle: jest.MockedFunction<_.GetPreprintTitleEnv['getPreprintTitle']> = jest.fn(_ =>
TE.right(preprintTitle),
)
const actual = await runMiddleware(
_.writeReviewRemoveAuthor(
preprintDoi,
0,
)({
canAddAuthors,
formStore,
getPreprintTitle,
secret,
sessionStore,
}),
connection,
)()

expect(await formStore.get(`${user.orcid}_${preprintDoi}`)).toMatchObject({ otherAuthors: [] })
expect(actual).toStrictEqual(
E.right([
{ type: 'setStatus', status: Status.SeeOther },
{
type: 'setHeader',
name: 'Location',
value: `/preprints/doi-${encodeURIComponent(
preprintDoi.toLowerCase().replaceAll('-', '+').replaceAll('/', '-'),
)}/write-a-prereview/more-authors`,
},
{ type: 'endResponse' },
]),
)
expect(canAddAuthors).toHaveBeenCalledWith(user)
expect(getPreprintTitle).toHaveBeenCalledWith(preprintDoi)
},
),
)
})

test('when there are no more authors', async () => {
await fc.assert(
fc.asyncProperty(
Expand Down

0 comments on commit 74e2f62

Please sign in to comment.