-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #67 from DEFRA/DSFAAP-580_tests-individual-questio…
…n-pages Unit tests for questions simplified
- Loading branch information
Showing
8 changed files
with
181 additions
and
758 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' */ |
Oops, something went wrong.