Skip to content

Commit

Permalink
Add custom matcher for paths with possible empty fragment (#1805)
Browse files Browse the repository at this point in the history
https://eaflood.atlassian.net/browse/IWTF-3759

This adds a custom matcher to clean up the tests as discussed in #1799
  • Loading branch information
irisfaraway authored Oct 5, 2023
1 parent 0dad4ef commit 89584dc
Show file tree
Hide file tree
Showing 27 changed files with 180 additions and 158 deletions.
24 changes: 23 additions & 1 deletion packages/gafl-webapp-service/gafl-jest-matchers.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,28 @@ function toThrowRedirectTo (error, uri) {
pass: false
}
}

function toHaveValidPathFor (location, expectedPath) {
const regex = new RegExp('^' + expectedPath + '#?$')
if (location.match(regex)) {
return {
message: () =>
`expected ${this.utils.printReceived(location)} to have the path be formatted as ${this.utils.printReceived(
expectedPath
)} or ${this.utils.printReceived(expectedPath + '#')}`,
pass: true
}
}
return {
message: () =>
`expected ${this.utils.printReceived(location)} to have the path be formatted as ${this.utils.printReceived(
expectedPath
)} or ${this.utils.printReceived(expectedPath + '#')}`,
pass: false
}
}

expect.extend({
toThrowRedirectTo
toThrowRedirectTo,
toHaveValidPathFor
})
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ describe('The agreed handler', () => {
it('loads the order complete page on return from the GOV.UK Payment pages', async () => {
await injectWithCookies('GET', AGREED.uri)
const response = await injectWithCookies('GET', AGREED.uri)
expect(response.headers.location).toBe(`${ORDER_COMPLETE.uri}#`)
expect(response.headers.location).toHaveValidPathFor(ORDER_COMPLETE.uri)
})

it('updates the journal entry with the complete status', async () => {
Expand Down Expand Up @@ -190,7 +190,7 @@ describe('The agreed handler', () => {

it('redirects to the order complete page', async () => {
const response = await injectWithCookies('GET', AGREED.uri)
expect(response.headers.location).toBe(`${ORDER_COMPLETE.uri}#`)
expect(response.headers.location).toHaveValidPathFor(ORDER_COMPLETE.uri)
})

it('updates the cache', async () => {
Expand Down Expand Up @@ -263,7 +263,7 @@ describe('The agreed handler', () => {
it('loads the order complete page', async () => {
await injectWithCookies('GET', TEST_STATUS.uri)
const response = await injectWithCookies('GET', AGREED.uri)
expect(response.headers.location).toBe(`${ORDER_COMPLETE.uri}#`)
expect(response.headers.location).toHaveValidPathFor(ORDER_COMPLETE.uri)
})

const getMockRequest = (overrides = {}) => ({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ describe('The agreed handler', () => {
salesApi.updatePaymentJournal = jest.fn()
const data2 = await injectWithCookies('GET', AGREED.uri)
expect(data2.statusCode).toBe(302)
expect(data2.headers.location).toBe(`${PAYMENT_FAILED.uri}#`)
expect(data2.headers.location).toHaveValidPathFor(PAYMENT_FAILED.uri)

// Ensure that the journal status has been updated correctly
expect(salesApi.updatePaymentJournal).toHaveBeenCalledWith(ADULT_FULL_1_DAY_LICENCE.transactionResponse.id, {
Expand All @@ -198,7 +198,7 @@ describe('The agreed handler', () => {
await injectWithCookies('GET', PAYMENT_FAILED.uri)
const data4 = await injectWithCookies('POST', PAYMENT_FAILED.uri, {})
expect(data4.statusCode).toBe(302)
expect(data4.headers.location).toBe(`${AGREED.uri}#`)
expect(data4.headers.location).toHaveValidPathFor(AGREED.uri)

// Test that the status has been updated correctly
const { payload: status2 } = await injectWithCookies('GET', TEST_STATUS.uri)
Expand Down Expand Up @@ -277,7 +277,7 @@ describe('The agreed handler', () => {
salesApi.updatePaymentJournal = jest.fn()
const data2 = await injectWithCookies('GET', AGREED.uri)
expect(data2.statusCode).toBe(302)
expect(data2.headers.location).toBe(`${PAYMENT_CANCELLED.uri}#`)
expect(data2.headers.location).toHaveValidPathFor(PAYMENT_CANCELLED.uri)

// Ensure the the jounal status is set to cancelled
expect(salesApi.updatePaymentJournal).toHaveBeenCalledWith(ADULT_FULL_1_DAY_LICENCE.transactionResponse.id, {
Expand All @@ -301,7 +301,7 @@ describe('The agreed handler', () => {
await injectWithCookies('GET', PAYMENT_CANCELLED.uri)
const data3 = await injectWithCookies('POST', PAYMENT_CANCELLED.uri, {})
expect(data3.statusCode).toBe(302)
expect(data3.headers.location).toBe(`${AGREED.uri}#`)
expect(data3.headers.location).toHaveValidPathFor(AGREED.uri)

// Test that the status has been updated correctly
const { payload: status2 } = await injectWithCookies('GET', TEST_STATUS.uri)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ describe('The agreed handler', () => {
const data3 = await injectWithCookies('GET', AGREED.uri)
expect(data3.statusCode).toBe(302)

expect(data3.headers.location).toBe(`${ORDER_COMPLETE.uri}#`)
expect(data3.headers.location).toHaveValidPathFor(ORDER_COMPLETE.uri)
const { payload } = await injectWithCookies('GET', TEST_TRANSACTION.uri)
expect(JSON.parse(payload).id).toBe(ADULT_FULL_1_DAY_LICENCE.transactionResponse.id)
const { payload: status } = await injectWithCookies('GET', TEST_STATUS.uri)
Expand All @@ -106,6 +106,6 @@ describe('The agreed handler', () => {

const data4 = await injectWithCookies('GET', AGREED.uri)
expect(data4.statusCode).toBe(302)
expect(data4.headers.location).toBe(`${ORDER_COMPLETE.uri}#`)
expect(data4.headers.location).toHaveValidPathFor(ORDER_COMPLETE.uri)
})
})
Original file line number Diff line number Diff line change
Expand Up @@ -9,24 +9,24 @@ describe('The controller handler', () => {
it('If there is no transaction then initialize redirect to the controller', async () => {
const data = await injectWithCookies('GET', '/')
expect(data.statusCode).toBe(302)
expect(data.headers.location).toBe(`${CONTROLLER.uri}#`)
expect(data.headers.location).toHaveValidPathFor(CONTROLLER.uri)
})

it('Adding a new transaction returns a redirect to the start of the journey', async () => {
const data = await injectWithCookies('GET', NEW_TRANSACTION.uri)
expect(data.statusCode).toBe(302)
expect(data.headers.location).toBe(`${CONTROLLER.uri}#`)
expect(data.headers.location).toHaveValidPathFor(CONTROLLER.uri)
})

it('Adding a new permission returns a redirect to the controller', async () => {
const data = await injectWithCookies('GET', ADD_PERMISSION.uri)
expect(data.statusCode).toBe(302)
expect(data.headers.location).toBe(`${CONTROLLER.uri}#`)
expect(data.headers.location).toHaveValidPathFor(CONTROLLER.uri)
})

it('The controller returns a redirect to the start of the journey', async () => {
const data = await injectWithCookies('GET', CONTROLLER.uri)
expect(data.statusCode).toBe(302)
expect(data.headers.location).toBe(`${LICENCE_FOR.uri}#`)
expect(data.headers.location).toHaveValidPathFor(LICENCE_FOR.uri)
})
})
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ afterAll(d => stop(d))
describe('The date of birth page', () => {
it('redirects back to LICENCE_FOR if not been on already', async () => {
const response = await injectWithCookies('GET', DATE_OF_BIRTH.uri)
expect(response.headers.location).toBe(`${LICENCE_FOR.uri}#`)
expect(response.headers.location).toHaveValidPathFor(LICENCE_FOR.uri)
})

it('return success on requesting the page', async () => {
Expand All @@ -28,7 +28,7 @@ describe('The date of birth page', () => {
it('redirects back to itself on posting no response', async () => {
const response = await injectWithCookies('POST', DATE_OF_BIRTH.uri, {})
expect(response.statusCode).toBe(302)
expect(response.headers.location).toBe(`${DATE_OF_BIRTH.uri}#`)
expect(response.headers.location).toHaveValidPathFor(DATE_OF_BIRTH.uri)
})

it('redirects back to itself on posting an invalid date', async () => {
Expand All @@ -38,34 +38,34 @@ describe('The date of birth page', () => {
'date-of-birth-year': '1970'
})
expect(response.statusCode).toBe(302)
expect(response.headers.location).toBe(`${DATE_OF_BIRTH.uri}#`)
expect(response.headers.location).toHaveValidPathFor(DATE_OF_BIRTH.uri)
})

it('redirects to the disability-concession page on posting a date of birth for an adult licence', async () => {
const response = await injectWithCookies('POST', DATE_OF_BIRTH.uri, dobHelper(SENIOR_TODAY))
expect(response.statusCode).toBe(302)
expect(response.headers.location).toBe(`${DISABILITY_CONCESSION.uri}#`)
expect(response.headers.location).toHaveValidPathFor(DISABILITY_CONCESSION.uri)
})

it('redirects to the disability-concession page on posting a date of birth for an adult licence', async () => {
const response = await injectWithCookies('POST', DATE_OF_BIRTH.uri, dobHelper(ADULT_TODAY))
expect(response.statusCode).toBe(302)
expect(response.headers.location).toBe(`${DISABILITY_CONCESSION.uri}#`)
expect(response.headers.location).toHaveValidPathFor(DISABILITY_CONCESSION.uri)
})

it(`redirects to the disability-concession page if a junior on ${DATE_AT_ADVANCED_PURCHASE_MAX_DAYS.format(
'YYYY-MM-DD'
)} - i.e. born ${JUNIOR_AT_ADVANCE_PURCHASE_MAX.format('YYYY-MM-DD')}`, async () => {
const response = await injectWithCookies('POST', DATE_OF_BIRTH.uri, dobHelper(JUNIOR_AT_ADVANCE_PURCHASE_MAX))
expect(response.statusCode).toBe(302)
expect(response.headers.location).toBe(`${DISABILITY_CONCESSION.uri}#`)
expect(response.headers.location).toHaveValidPathFor(DISABILITY_CONCESSION.uri)
})

it(`redirects to the no-licence-required page if a minor on ${DATE_AT_ADVANCED_PURCHASE_MAX_DAYS.format(
'YYYY-MM-DD'
)} - i.e. born ${MINOR_AT_ADVANCE_PURCHASE_MAX.format('YYYY-MM-DD')}`, async () => {
const response = await injectWithCookies('POST', DATE_OF_BIRTH.uri, dobHelper(MINOR_AT_ADVANCE_PURCHASE_MAX))
expect(response.statusCode).toBe(302)
expect(response.headers.location).toBe(`${NO_LICENCE_REQUIRED.uri}#`)
expect(response.headers.location).toHaveValidPathFor(NO_LICENCE_REQUIRED.uri)
})
})
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ describe('The disability concession page', () => {
it('redirects back to itself on posting an empty payload', async () => {
const response = await injectWithCookies('POST', DISABILITY_CONCESSION.uri, {})
expect(response.statusCode).toBe(302)
expect(response.headers.location).toBe(`${DISABILITY_CONCESSION.uri}#`)
expect(response.headers.location).toHaveValidPathFor(DISABILITY_CONCESSION.uri)
})

it('redirects back to itself on posting PIP with an invalid NI number', async () => {
Expand All @@ -28,7 +28,7 @@ describe('The disability concession page', () => {
'ni-number': 'not-valid'
})
expect(response.statusCode).toBe(302)
expect(response.headers.location).toBe(`${DISABILITY_CONCESSION.uri}#`)
expect(response.headers.location).toHaveValidPathFor(DISABILITY_CONCESSION.uri)
})

it('redirects back to itself on posting blue badge with an empty blue number', async () => {
Expand All @@ -37,7 +37,7 @@ describe('The disability concession page', () => {
'blue-badge-number': ''
})
expect(response.statusCode).toBe(302)
expect(response.headers.location).toBe(`${DISABILITY_CONCESSION.uri}#`)
expect(response.headers.location).toHaveValidPathFor(DISABILITY_CONCESSION.uri)
})

it('on setting a correct ni number it redirects to the licence-to-start page', async () => {
Expand All @@ -46,7 +46,7 @@ describe('The disability concession page', () => {
'ni-number': 'NH 34 67 44 A'
})
expect(response.statusCode).toBe(302)
expect(response.headers.location).toBe(`${LICENCE_TO_START.uri}#`)
expect(response.headers.location).toHaveValidPathFor(LICENCE_TO_START.uri)
})

it('on setting a correct ni number adds a disabled concession to the cache', async () => {
Expand All @@ -68,7 +68,7 @@ describe('The disability concession page', () => {
'blue-badge-number': '1234'
})
expect(response.statusCode).toBe(302)
expect(response.headers.location).toBe(`${LICENCE_TO_START.uri}#`)
expect(response.headers.location).toHaveValidPathFor(LICENCE_TO_START.uri)
})

it('on setting a correct blue badge number adds a disabled concession to the cache', async () => {
Expand Down Expand Up @@ -114,6 +114,6 @@ describe('The disability concession page', () => {
'disability-concession': disabilityConcessionTypes.no
})
expect(response.statusCode).toBe(302)
expect(response.headers.location).toBe(`${LICENCE_TO_START.uri}#`)
expect(response.headers.location).toHaveValidPathFor(LICENCE_TO_START.uri)
})
})
Original file line number Diff line number Diff line change
Expand Up @@ -56,79 +56,79 @@ describe('The manual address entry page', () => {
it('redirects back to itself on posting no response', async () => {
const response = await injectWithCookies('POST', ADDRESS_ENTRY.uri, {})
expect(response.statusCode).toBe(302)
expect(response.headers.location).toBe(`${ADDRESS_ENTRY.uri}#`)
expect(response.headers.location).toHaveValidPathFor(ADDRESS_ENTRY.uri)
})

it('redirects back to itself on posting address with no premises', async () => {
const addr = Object.assign({}, goodAddress)
delete addr.premises
const response = await injectWithCookies('POST', ADDRESS_ENTRY.uri, addr)
expect(response.statusCode).toBe(302)
expect(response.headers.location).toBe(`${ADDRESS_ENTRY.uri}#`)
expect(response.headers.location).toHaveValidPathFor(ADDRESS_ENTRY.uri)
})

it('redirects back to itself on posting too long premises', async () => {
const addr = Object.assign({}, goodAddress)
addr.premises = 'A'.repeat(101)
const response = await injectWithCookies('POST', ADDRESS_ENTRY.uri, addr)
expect(response.statusCode).toBe(302)
expect(response.headers.location).toBe(`${ADDRESS_ENTRY.uri}#`)
expect(response.headers.location).toHaveValidPathFor(ADDRESS_ENTRY.uri)
})

it('redirects back to itself on posting too long street', async () => {
const addr = Object.assign({}, goodAddress)
addr.street = 'A'.repeat(101)
const response = await injectWithCookies('POST', ADDRESS_ENTRY.uri, addr)
expect(response.statusCode).toBe(302)
expect(response.headers.location).toBe(`${ADDRESS_ENTRY.uri}#`)
expect(response.headers.location).toHaveValidPathFor(ADDRESS_ENTRY.uri)
})

it('redirects back to itself on posting too long locality', async () => {
const addr = Object.assign({}, goodAddress)
addr.locality = 'A'.repeat(101)
const response = await injectWithCookies('POST', ADDRESS_ENTRY.uri, addr)
expect(response.statusCode).toBe(302)
expect(response.headers.location).toBe(`${ADDRESS_ENTRY.uri}#`)
expect(response.headers.location).toHaveValidPathFor(ADDRESS_ENTRY.uri)
})

it('redirects back to itself on posting address with no town', async () => {
const addr = Object.assign({}, goodAddress)
delete addr.town
const response = await injectWithCookies('POST', ADDRESS_ENTRY.uri, addr)
expect(response.statusCode).toBe(302)
expect(response.headers.location).toBe(`${ADDRESS_ENTRY.uri}#`)
expect(response.headers.location).toHaveValidPathFor(ADDRESS_ENTRY.uri)
})

it('redirects back to itself on posting too long town', async () => {
const addr = Object.assign({}, goodAddress)
addr.town = 'A'.repeat(101)
const response = await injectWithCookies('POST', ADDRESS_ENTRY.uri, addr)
expect(response.statusCode).toBe(302)
expect(response.headers.location).toBe(`${ADDRESS_ENTRY.uri}#`)
expect(response.headers.location).toHaveValidPathFor(ADDRESS_ENTRY.uri)
})

it('redirects back to itself on posting with missing country code', async () => {
const addr = Object.assign({}, goodAddress)
delete addr['country-code']
const response = await injectWithCookies('POST', ADDRESS_ENTRY.uri, addr)
expect(response.statusCode).toBe(302)
expect(response.headers.location).toBe(`${ADDRESS_ENTRY.uri}#`)
expect(response.headers.location).toHaveValidPathFor(ADDRESS_ENTRY.uri)
})

it('redirects back to itself on posting invalid UK postcode', async () => {
const addr = Object.assign({}, goodAddress)
addr.postcode = 'foo'
const response = await injectWithCookies('POST', ADDRESS_ENTRY.uri, addr)
expect(response.statusCode).toBe(302)
expect(response.headers.location).toBe(`${ADDRESS_ENTRY.uri}#`)
expect(response.headers.location).toHaveValidPathFor(ADDRESS_ENTRY.uri)
})

describe('on successful valid UK address submission', () => {
it('redirects to the contact page on posting a valid UK address', async () => {
const addr = Object.assign({}, goodAddress)
const response = await injectWithCookies('POST', ADDRESS_ENTRY.uri, addr)
expect(response.statusCode).toBe(302)
expect(response.headers.location).toBe(`${CONTACT.uri}#`)
expect(response.headers.location).toHaveValidPathFor(CONTACT.uri)

const { payload } = await injectWithCookies('GET', TEST_TRANSACTION.uri)
expect(JSON.parse(payload).permissions[0].licensee).toEqual({
Expand All @@ -147,7 +147,7 @@ describe('The manual address entry page', () => {
await injectWithCookies('POST', LICENCE_LENGTH.uri, { 'licence-length': '1D' })
const response = await injectWithCookies('POST', ADDRESS_ENTRY.uri, addr)
expect(response.statusCode).toBe(302)
expect(response.headers.location).toBe(`${CONTACT.uri}#`)
expect(response.headers.location).toHaveValidPathFor(CONTACT.uri)
})

it('redirects to the contact page if it is 8 day adult licence', async () => {
Expand All @@ -156,7 +156,7 @@ describe('The manual address entry page', () => {
await injectWithCookies('POST', LICENCE_LENGTH.uri, { 'licence-length': '1D' })
const response = await injectWithCookies('POST', ADDRESS_ENTRY.uri, addr)
expect(response.statusCode).toBe(302)
expect(response.headers.location).toBe(`${CONTACT.uri}#`)
expect(response.headers.location).toHaveValidPathFor(CONTACT.uri)
})

it('redirects to the licence fulfilment page if it is a 12 month adult licence', async () => {
Expand All @@ -166,7 +166,7 @@ describe('The manual address entry page', () => {
isPhysical.mockReturnValueOnce(true)
const response = await injectWithCookies('POST', ADDRESS_ENTRY.uri, addr)
expect(response.statusCode).toBe(302)
expect(response.headers.location).toBe(`${LICENCE_FULFILMENT.uri}#`)
expect(response.headers.location).toHaveValidPathFor(LICENCE_FULFILMENT.uri)
})

it('redirects to the contact page if it is a 12 month junior licence on posting a valid UK address', async () => {
Expand All @@ -175,7 +175,7 @@ describe('The manual address entry page', () => {
await injectWithCookies('POST', LICENCE_LENGTH.uri, { 'licence-length': '12M' })
const response = await injectWithCookies('POST', ADDRESS_ENTRY.uri, addr)
expect(response.statusCode).toBe(302)
expect(response.headers.location).toBe(`${CONTACT.uri}#`)
expect(response.headers.location).toHaveValidPathFor(CONTACT.uri)
})
})

Expand All @@ -185,7 +185,7 @@ describe('The manual address entry page', () => {
addr.postcode = 'not checked'
const response = await injectWithCookies('POST', ADDRESS_ENTRY.uri, addr)
expect(response.statusCode).toBe(302)
expect(response.headers.location).toBe(`${CONTACT.uri}#`)
expect(response.headers.location).toHaveValidPathFor(CONTACT.uri)

const { payload } = await injectWithCookies('GET', TEST_TRANSACTION.uri)
expect(JSON.parse(payload).permissions[0].licensee).toMatchObject({
Expand Down Expand Up @@ -213,6 +213,6 @@ describe('The manual address entry page', () => {
await injectWithCookies('GET', CONTACT_SUMMARY.uri)
const response = await injectWithCookies('POST', ADDRESS_ENTRY.uri, goodAddress)
expect(response.statusCode).toBe(302)
expect(response.headers.location).toBe(`${CONTACT_SUMMARY.uri}#`)
expect(response.headers.location).toHaveValidPathFor(CONTACT_SUMMARY.uri)
})
})
Loading

0 comments on commit 89584dc

Please sign in to comment.