-
Notifications
You must be signed in to change notification settings - Fork 1
TGC-796: Create CheckResponsesPageController #231
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
10edd1d
TGC-796: Create CheckDetailsPageController
davidatdefra 0bad9cc
Merge branch 'main' into TGC-796-check-details-page
davidatdefra 1e7efe3
Rename controller and page
davidatdefra 57ba73d
Update tests
davidatdefra 2b4c15f
Update `adding-value.yaml` with new CheckResponsesPageController
davidatdefra 068a002
Merge branch 'main' into TGC-796-check-details-page
davidatdefra eccec62
Reverted to using the hardcoded `/summary` path until DXT plugin PR i…
davidatdefra 53a8ba4
Merge branch 'main' into TGC-796-check-details-page
davidatdefra 03d52f3
Merge branch 'main' into TGC-796-check-details-page
davidatdefra 6ea3096
Remove commented code
davidatdefra 96fb25e
Address PR comments
davidatdefra File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or 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 hidden or 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 |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| import { SummaryPageController } from '@defra/forms-engine-plugin/controllers/SummaryPageController.js' | ||
|
|
||
| export default class CheckResponsesPageController extends SummaryPageController { | ||
| /** | ||
| * @param {FormModel} model | ||
| * @param {PageSummary} pageDef | ||
| */ | ||
| constructor(model, pageDef) { | ||
| super(model, pageDef) | ||
| this.viewName = 'check-responses-page' | ||
| } | ||
|
|
||
| getSummaryPath() { | ||
| return this.path | ||
| } | ||
|
|
||
| makePostRouteHandler() { | ||
| return (request, context, h) => { | ||
| return this.proceed(request, h, this.getNextPath(context)) | ||
| } | ||
| } | ||
| } | ||
137 changes: 137 additions & 0 deletions
137
src/server/check-responses/check-responses.controller.test.js
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,137 @@ | ||
| import { SummaryPageController } from '@defra/forms-engine-plugin/controllers/SummaryPageController.js' | ||
| import { existsSync } from 'fs' | ||
| import { join } from 'path' | ||
| import CheckResponsesPageController from '~/src/server/check-responses/check-responses.controller.js' | ||
|
|
||
| describe('CheckResponsesPageController', () => { | ||
| let controller | ||
| let mockModel | ||
| let mockPageDef | ||
|
|
||
| beforeEach(() => { | ||
| mockModel = { | ||
| basePath: '/test-form' | ||
| } | ||
| mockPageDef = { | ||
| path: '/check-answers', | ||
| title: 'Check your answers' | ||
| } | ||
| controller = new CheckResponsesPageController(mockModel, mockPageDef) | ||
| }) | ||
|
|
||
| describe('constructor', () => { | ||
| it('should extend SummaryPageController', () => { | ||
| expect(controller).toBeInstanceOf(SummaryPageController) | ||
| }) | ||
|
|
||
| it('should set viewName to check-responses-page', () => { | ||
| expect(controller.viewName).toBe('check-responses-page') | ||
| }) | ||
| }) | ||
|
|
||
| describe('getSummaryPath', () => { | ||
| it('returns this.path when set', () => { | ||
| controller.path = mockPageDef.path | ||
|
|
||
| const result = controller.getSummaryPath() | ||
|
|
||
| expect(result).toBe('/check-answers') | ||
| }) | ||
|
|
||
| it('reflects updates to this.path', () => { | ||
| controller.path = '/check-answers' | ||
| expect(controller.getSummaryPath()).toBe('/check-answers') | ||
|
|
||
| controller.path = '/updated-check-answers' | ||
| expect(controller.getSummaryPath()).toBe('/updated-check-answers') | ||
| }) | ||
|
|
||
| it('returns undefined if this.path is not set - plugin will then use fallback /summary', () => { | ||
| expect(controller.getSummaryPath()).toBeUndefined() | ||
| }) | ||
| }) | ||
|
|
||
| describe('makePostRouteHandler', () => { | ||
| let handler | ||
|
|
||
| beforeEach(() => { | ||
| handler = controller.makePostRouteHandler() | ||
| }) | ||
|
|
||
| it('should return a function', () => { | ||
| expect(typeof handler).toBe('function') | ||
| }) | ||
|
|
||
| it('should call getNextPath with context and proceed with correct arguments, returning its result', () => { | ||
| const request = { method: 'post', path: '/some-path' } | ||
| const context = { payload: { foo: 'bar' } } | ||
| const h = { redirect: jest.fn() } | ||
|
|
||
| const nextPath = '/test-form/declaration' | ||
|
|
||
| controller.getNextPath = jest.fn().mockReturnValue(nextPath) | ||
| const proceedResult = Symbol('proceed-result') | ||
| controller.proceed = jest.fn().mockReturnValue(proceedResult) | ||
|
|
||
| const result = handler(request, context, h) | ||
|
|
||
| expect(controller.getNextPath).toHaveBeenCalledTimes(1) | ||
| expect(controller.getNextPath).toHaveBeenCalledWith(context) | ||
|
|
||
| expect(controller.proceed).toHaveBeenCalledTimes(1) | ||
| expect(controller.proceed).toHaveBeenCalledWith(request, h, nextPath) | ||
|
|
||
| expect(result).toBe(proceedResult) | ||
| }) | ||
|
|
||
| it('should preserve controller context inside returned handler', () => { | ||
| // If `this` is lost, spies won't be hit | ||
| const request = {} | ||
| const context = {} | ||
| const h = {} | ||
|
|
||
| controller.getNextPath = jest.fn().mockReturnValue('/next') | ||
| controller.proceed = jest.fn().mockReturnValue('ok') | ||
|
|
||
| const fn = controller.makePostRouteHandler() | ||
| const ret = fn(request, context, h) | ||
|
|
||
| expect(controller.proceed).toHaveBeenCalledWith(request, h, '/next') | ||
| expect(ret).toBe('ok') | ||
| }) | ||
| }) | ||
|
|
||
| describe('integration with SummaryPageController', () => { | ||
| it('should properly set up the controller instance', () => { | ||
| expect(controller).toBeDefined() | ||
| expect(controller.viewName).toBe('check-responses-page') | ||
| expect(controller).toHaveProperty('makePostRouteHandler') | ||
| }) | ||
|
|
||
| it('should override getSummaryPath from parent', () => { | ||
| expect(controller.getSummaryPath).toBeDefined() | ||
| }) | ||
|
|
||
| it('should override makePostRouteHandler from parent', () => { | ||
| const handler = controller.makePostRouteHandler() | ||
| expect(typeof handler).toBe('function') | ||
| expect(handler.constructor.name).toBe('Function') | ||
| }) | ||
| }) | ||
|
|
||
| describe('view file existence', () => { | ||
| it('should reference a view file that actually exists', () => { | ||
| const viewPath = controller.viewName | ||
| expect(viewPath).toBe('check-responses-page') | ||
|
|
||
| // Check that the view file exists at the expected location | ||
| const absoluteViewPath = join(process.cwd(), 'src/server/check-responses/views', `${viewPath}.html`) | ||
| expect(existsSync(absoluteViewPath)).toBe(true) | ||
| }) | ||
|
|
||
| it('should have view file in the feature-based location', () => { | ||
| const featureViewPath = join(process.cwd(), 'src/server/check-responses/views/check-responses-page.html') | ||
| expect(existsSync(featureViewPath)).toBe(true) | ||
| }) | ||
| }) | ||
| }) |
42 changes: 42 additions & 0 deletions
42
src/server/check-responses/views/check-responses-page.html
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| {% extends baseLayoutPath %} | ||
|
|
||
| {% from "govuk/components/summary-list/macro.njk" import govukSummaryList %} | ||
| {% from "govuk/components/button/macro.njk" import govukButton %} | ||
|
|
||
| {% block pageTitle %} | ||
| {{ pageTitle | evaluate }} | {{ serviceName }} | ||
| {% endblock %} | ||
|
|
||
| {% block content %} | ||
| <div class="govuk-grid-row"> | ||
| <div class="govuk-grid-column-two-thirds"> | ||
| {% block table %} | ||
| <h1 class="govuk-heading-l">{{ pageTitle }}</h1> | ||
|
|
||
| {% for section in checkAnswers %} | ||
| {% if section.title.text %} | ||
| <h2 class="govuk-heading-m"> | ||
| {{ section.title.text }} | ||
| </h2> | ||
| {% endif %} | ||
|
|
||
| {{ govukSummaryList(section.summaryList) }} | ||
| {% endfor %} | ||
|
|
||
| {% endblock %} | ||
|
|
||
| {% block form %} | ||
| <form method="post" novalidate> | ||
| <input type="hidden" name="crumb" value="{{ crumb }}"> | ||
|
|
||
| <div class="govuk-button-group"> | ||
| {{ govukButton({ | ||
| text: "Continue", | ||
| preventDoubleClick: true | ||
| }) }} | ||
| </div> | ||
| </form> | ||
| {% endblock %} | ||
| </div> | ||
| </div> | ||
| {% endblock %} |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.