Skip to content

Commit

Permalink
Make tests easier to evolve by making the cases clearer
Browse files Browse the repository at this point in the history
Refs #1307
  • Loading branch information
thewilkybarkid committed Nov 2, 2023
1 parent 775bd9f commit b9db1fb
Showing 1 changed file with 39 additions and 84 deletions.
123 changes: 39 additions & 84 deletions test/my-details-page/change-contact-email-address.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,9 @@ describe('changeContactEmailAddress', () => {
),
),
fc.user(),
fc.contactEmailAddress(),
fc.either(fc.constant('not-found' as const), fc.contactEmailAddress()),
])(
'there is an email address already',
'when an email address is given',
async (oauth, publicUrl, [emailAddress, connection], user, existingEmailAddress) => {
const saveContactEmailAddress = jest.fn<_.Env['saveContactEmailAddress']>(_ => TE.right(undefined))

Expand All @@ -74,7 +74,7 @@ describe('changeContactEmailAddress', () => {
publicUrl,
oauth,
deleteContactEmailAddress: shouldNotBeCalled,
getContactEmailAddress: () => TE.right(existingEmailAddress),
getContactEmailAddress: () => TE.fromEither(existingEmailAddress),
saveContactEmailAddress,
}),
connection,
Expand All @@ -94,91 +94,49 @@ describe('changeContactEmailAddress', () => {
test.prop([
fc.oauth(),
fc.origin(),
fc.emailAddress().chain(emailAddress =>
fc.tuple(
fc.constant(emailAddress),
fc.connection({
body: fc.constant({ emailAddress }),
method: fc.constant('POST'),
}),
),
),
fc.connection({
body: fc.record({
emailAddress: fc
.nonEmptyString()
.filter(string => !string.includes('.') || !string.includes('@') || /\s/g.test(string)),
}),
method: fc.constant('POST'),
}),
fc.user(),
])("there isn't an email address already", async (oauth, publicUrl, [emailAddress, connection], user) => {
const saveContactEmailAddress = jest.fn<_.Env['saveContactEmailAddress']>(_ => TE.right(undefined))

fc.either(fc.constant('not-found' as const), fc.contactEmailAddress()),
])('it is not an email address', async (oauth, publicUrl, connection, user, emailAddress) => {
const actual = await runMiddleware(
_.changeContactEmailAddress({
canChangeContactEmailAddress: () => true,
getUser: () => M.right(user),
publicUrl,
oauth,
deleteContactEmailAddress: shouldNotBeCalled,
getContactEmailAddress: () => TE.left('not-found'),
saveContactEmailAddress,
getContactEmailAddress: () => TE.fromEither(emailAddress),
saveContactEmailAddress: shouldNotBeCalled,
}),
connection,
)()

expect(actual).toStrictEqual(
E.right([
{ type: 'setStatus', status: Status.SeeOther },
{ type: 'setHeader', name: 'Location', value: format(myDetailsMatch.formatter, {}) },
{ type: 'endResponse' },
{ type: 'setStatus', status: Status.BadRequest },
{ type: 'setHeader', name: 'Content-Type', value: MediaType.textHTML },
{ type: 'setBody', body: expect.anything() },
]),
)
expect(saveContactEmailAddress).toHaveBeenCalledWith(user.orcid, { type: 'unverified', value: emailAddress })
})
})

test.prop([
fc.oauth(),
fc.origin(),
fc.connection({
body: fc.record({
emailAddress: fc
.nonEmptyString()
.filter(string => !string.includes('.') || !string.includes('@') || /\s/g.test(string)),
}),
method: fc.constant('POST'),
}),
fc.user(),
fc.either(fc.constant('not-found' as const), fc.contactEmailAddress()),
])('it is not an email address', async (oauth, publicUrl, connection, user, emailAddress) => {
const actual = await runMiddleware(
_.changeContactEmailAddress({
canChangeContactEmailAddress: () => true,
getUser: () => M.right(user),
publicUrl,
oauth,
deleteContactEmailAddress: shouldNotBeCalled,
getContactEmailAddress: () => TE.fromEither(emailAddress),
saveContactEmailAddress: shouldNotBeCalled,
test.prop([
fc.oauth(),
fc.origin(),
fc.connection({
body: fc.record({ emailAddress: fc.emailAddress() }),
method: fc.constant('POST'),
}),
connection,
)()

expect(actual).toStrictEqual(
E.right([
{ type: 'setStatus', status: Status.BadRequest },
{ type: 'setHeader', name: 'Content-Type', value: MediaType.textHTML },
{ type: 'setBody', body: expect.anything() },
]),
)
})

test.prop([
fc.oauth(),
fc.origin(),
fc.connection({
body: fc.record({ emailAddress: fc.emailAddress() }),
method: fc.constant('POST'),
}),
fc.user(),
fc.either(fc.constant('not-found' as const), fc.contactEmailAddress()),
])(
'when the form has been submitted but the email address cannot be saved',
async (oauth, publicUrl, connection, user, emailAddress) => {
fc.user(),
fc.either(fc.constant('not-found' as const), fc.contactEmailAddress()),
])('the email address cannot be saved', async (oauth, publicUrl, connection, user, emailAddress) => {
const actual = await runMiddleware(
_.changeContactEmailAddress({
canChangeContactEmailAddress: () => true,
Expand All @@ -200,20 +158,17 @@ describe('changeContactEmailAddress', () => {
{ type: 'setBody', body: expect.anything() },
]),
)
},
)
})

test.prop([
fc.oauth(),
fc.origin(),
fc.connection({
body: fc.record({ emailAddress: fc.constant('') }, { withDeletedKeys: true }),
method: fc.constant('POST'),
}),
fc.user(),
])(
'when the form has been submitted without setting an email address',
async (oauth, publicUrl, connection, user) => {
test.prop([
fc.oauth(),
fc.origin(),
fc.connection({
body: fc.record({ emailAddress: fc.constant('') }, { withDeletedKeys: true }),
method: fc.constant('POST'),
}),
fc.user(),
])('when no email address is set', async (oauth, publicUrl, connection, user) => {
const deleteContactEmailAddress = jest.fn<_.Env['deleteContactEmailAddress']>(_ => TE.right(undefined))

const actual = await runMiddleware(
Expand All @@ -237,8 +192,8 @@ describe('changeContactEmailAddress', () => {
]),
)
expect(deleteContactEmailAddress).toHaveBeenCalledWith(user.orcid)
},
)
})
})
})

test.prop([fc.oauth(), fc.origin(), fc.connection()])(
Expand Down

0 comments on commit b9db1fb

Please sign in to comment.