Skip to content

Commit

Permalink
Merge pull request #67 from DEFRA/DSFAAP-580_tests-individual-questio…
Browse files Browse the repository at this point in the history
…n-pages

Unit tests for questions simplified
  • Loading branch information
joseluisgraa authored Dec 12, 2024
2 parents 767d4d7 + 747c2ad commit 71d9c57
Show file tree
Hide file tree
Showing 8 changed files with 181 additions and 758 deletions.
14 changes: 13 additions & 1 deletion src/server/common/model/answer/address.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ describe('Address.validate', () => {
expect(isValid).toBe(true)
})

it('should return false for too short input', () => {
it('should return false for too long input', () => {
const address = new Address({
...validAddress,
addressLine1: 'A'.repeat(256)
Expand Down Expand Up @@ -99,6 +99,18 @@ describe('Address.validate', () => {
expect(isValid).toBe(false)
expect(errors.addressPostcode.text).toBe('Enter a full UK postcode')
})

it('should return false for empty postcode', () => {
const address = new Address({
...validAddress,
addressPostcode: ''
})

const { isValid, errors } = address.validate()

expect(isValid).toBe(false)
expect(errors.addressPostcode.text).toBe('Enter postcode')
})
})

it('should return false for missing required fields', () => {
Expand Down
2 changes: 1 addition & 1 deletion src/server/common/model/answer/cph-number.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ describe('#CphNumber.validate', () => {
expect(errors.cphNumber.text).toBe('Enter the farm or premises CPH number')
})

it('should return false for too short input', () => {
it('should return false for malformed input', () => {
const cphNumber = new CphNumber({
cphNumber: '1/2/3'
})
Expand Down
201 changes: 40 additions & 161 deletions src/server/licence/email-address/index.test.js
Original file line number Diff line number Diff line change
@@ -1,183 +1,62 @@
import { createServer } from '~/src/server/index.js'
import { statusCodes } from '~/src/server/common/constants/status-codes.js'
import { withCsrfProtection } from '~/src/server/common/test-helpers/csrf.js'
import { parseDocument } from '~/src/server/common/test-helpers/dom.js'
import SessionTester from '../../common/test-helpers/session-helper.js'
import { emailAddress, emailAddressPage, EmailAddressPage } from './index.js'
import { licenceCheckAnswersPage } from '../check-answers/index.js'
import { EmailAddress } from '../../common/model/answer/email-address.js'

const pageTitle = 'What email address would you like the licence sent to?'
const testEmail = '[email protected]'
const sectionKey = 'licence'
const question = 'What email address would you like the licence sent to?'
const questionKey = 'emailAddress'
const view = 'licence/email-address/index'
const pageUrl = '/receiving-the-licence/licence-enter-email-address'

describe('#licenceEmailAddress', () => {
/** @type {Server} */
let server
describe('EmailAddressPage', () => {
let page

beforeAll(async () => {
server = await createServer()
await server.initialize()
beforeEach(() => {
page = new EmailAddressPage()
})

afterAll(async () => {
await server.stop({ timeout: 0 })
it('should have the correct urlPath', () => {
expect(page.urlPath).toBe(pageUrl)
})

it('Should provide expected response', async () => {
const { payload, statusCode } = await server.inject({
method: 'GET',
url: '/receiving-the-licence/licence-enter-email-address'
})

expect(parseDocument(payload).title).toEqual(pageTitle)
expect(statusCode).toBe(statusCodes.ok)
it('should have the correct sectionKey', () => {
expect(page.sectionKey).toBe(sectionKey)
})

it('Should process the result and provide expected response', async () => {
const { headers, statusCode } = await server.inject(
withCsrfProtection({
method: 'POST',
url: '/receiving-the-licence/licence-enter-email-address',
payload: {
emailAddress: testEmail
}
})
)

expect(statusCode).toBe(statusCodes.redirect)

expect(headers.location).toBe('/receiving-the-licence/check-answers')
it('should have the correct question', () => {
expect(page.question).toBe(question)
})

it('Should display an error to the user if no value entered', async () => {
const { payload, statusCode } = await server.inject(
withCsrfProtection({
method: 'POST',
url: '/receiving-the-licence/licence-enter-email-address',
payload: {}
})
)

expect(parseDocument(payload).title).toBe(`Error: ${pageTitle}`)

expect(payload).toEqual(expect.stringContaining('There is a problem'))
expect(payload).toEqual(
expect.stringContaining(
'Enter the email address you would like the licence sent to'
)
)

expect(statusCode).toBe(statusCodes.ok)
it('should have the correct questionKey', () => {
expect(page.questionKey).toBe(questionKey)
})

it('Should display an error when the user entered an invalid email address', async () => {
const { payload, statusCode } = await server.inject(
withCsrfProtection({
method: 'POST',
url: '/receiving-the-licence/licence-enter-email-address',
payload: {
emailAddress: 'invalid format'
}
})
)

expect(parseDocument(payload).title).toBe(`Error: ${pageTitle}`)

expect(payload).toEqual(expect.stringContaining('There is a problem'))
expect(payload).toEqual(
expect.stringContaining(
'Enter an email address in the correct format, like [email protected]'
)
)

expect(statusCode).toBe(statusCodes.ok)
it('should have the correct view', () => {
expect(page.view).toBe(view)
})

describe('when there is data already in the session', () => {
let session

beforeEach(async () => {
session = await SessionTester.create(server)
await session.setState('licence', {
emailAddress: testEmail
})
})

it('should repopulate the form from state', async () => {
const { payload, statusCode } = await server.inject(
withCsrfProtection(
{
method: 'GET',
url: '/receiving-the-licence/licence-enter-email-address'
},
{
Cookie: session.sessionID
}
)
)

expect(statusCode).toBe(statusCodes.ok)
expect(payload).toEqual(
expect.stringContaining(
`<input class="govuk-input govuk-input--width-20" id="emailAddress" name="emailAddress" type="email" spellcheck="false" value="${testEmail}" autocomplete="email-address">`
)
)
})
it('should have the correct Answer model', () => {
expect(page.Answer).toBe(EmailAddress)
})

describe('when nextPage is provided', () => {
it('should set the next page appropriately', async () => {
const { payload, statusCode } = await server.inject({
method: 'GET',
url: '/receiving-the-licence/licence-enter-email-address?redirect_uri=/receiving-the-licence/check-answers'
})

expect(payload).toEqual(
expect.stringContaining(
'<input type="hidden" name="nextPage" value="/receiving-the-licence/check-answers" />'
)
)

expect(statusCode).toBe(statusCodes.ok)
})

it('should redirect to the page it came from via redirect_uri', async () => {
const { headers, statusCode } = await server.inject(
withCsrfProtection({
method: 'POST',
url: '/receiving-the-licence/licence-enter-email-address',
payload: {
emailAddress: testEmail,
nextPage: '/some/page'
}
})
)

expect(headers.location).toBe('/some/page')
expect(statusCode).toBe(statusCodes.redirect)
})

it('Should display an error and set next page appropriately', async () => {
const { payload, statusCode } = await server.inject(
withCsrfProtection({
method: 'POST',
url: '/receiving-the-licence/licence-enter-email-address',
payload: {
emailAddress: 'invalid format',
nextPage: '/receiving-the-licence/check-answers'
}
})
)
it('nextPage should return licenceCheckAnswersPage', () => {
const nextPage = page.nextPage()
expect(nextPage).toBe(licenceCheckAnswersPage)
})

expect(parseDocument(payload).title).toBe(`Error: ${pageTitle}`)
expect(payload).toEqual(
expect.stringContaining(
'<input type="hidden" name="nextPage" value="/receiving-the-licence/check-answers" />'
)
)
it('should export page', () => {
expect(emailAddressPage).toBeInstanceOf(EmailAddressPage)
})

expect(statusCode).toBe(statusCodes.ok)
})
it('should export emailAddress as a plugin', () => {
expect(emailAddress).toHaveProperty('plugin')
const plugin = /** @type {PluginBase<void> & PluginNameVersion} */ (
emailAddress.plugin
)
expect(plugin).toHaveProperty('name')
expect(plugin.name).toBe(`${sectionKey}-${questionKey}`)
expect(plugin).toHaveProperty('register')
})
})

/**
* @import { Server } from '@hapi/hapi'
*/
/** @import { PluginBase, PluginNameVersion } from '@hapi/hapi' */
Loading

0 comments on commit 71d9c57

Please sign in to comment.