Skip to content

Commit

Permalink
Add cancel option to form wizard
Browse files Browse the repository at this point in the history
This adds a cancel url local that can be used by the template to
the form wizard so that the form journey can be cancelled by the user.
  • Loading branch information
teneightfive committed Aug 5, 2019
1 parent fb07006 commit a5d9811
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 0 deletions.
10 changes: 10 additions & 0 deletions app/move/controllers/new.form.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,16 @@ class FormController extends Controller {
this.use(this.checkCurrentLocation)
}

middlewareLocals() {
super.middlewareLocals()
this.use(this.setCancelUrl)
}

setCancelUrl(req, res, next) {
res.locals.cancelUrl = `/moves${res.locals.MOVES_QUERY_STRING || ''}`
next()
}

checkCurrentLocation(req, res, next) {
if (!req.session.currentLocation) {
const error = new Error(
Expand Down
60 changes: 60 additions & 0 deletions app/move/controllers/new.form.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,66 @@ describe('Move controllers', function() {
})
})

describe('#middlewareLocals()', function() {
beforeEach(function() {
sinon.stub(FormController.prototype, 'middlewareLocals')
sinon.stub(controller, 'use')

controller.middlewareLocals()
})

it('should call parent method', function() {
expect(FormController.prototype.middlewareLocals).to.have.been
.calledOnce
})

it('should call set cancel url method', function() {
expect(controller.use).to.have.been.calledWith(controller.setCancelUrl)
})
})

describe('#setCancelUrl()', function() {
let res, nextSpy

beforeEach(function() {
nextSpy = sinon.spy()
res = {
locals: {},
}
})

context('with empty moves query', function() {
beforeEach(function() {
controller.setCancelUrl({}, res, nextSpy)
})

it('should set cancel url correctly', function() {
expect(res.locals).to.have.property('cancelUrl')
expect(res.locals.cancelUrl).to.equal('/moves')
})

it('should call next', function() {
expect(nextSpy).to.be.calledOnceWithExactly()
})
})

context('with moves query', function() {
beforeEach(function() {
res.locals.MOVES_QUERY_STRING = '?move-date=2019-10-10'
controller.setCancelUrl({}, res, nextSpy)
})

it('should set cancel url correctly', function() {
expect(res.locals).to.have.property('cancelUrl')
expect(res.locals.cancelUrl).to.equal('/moves?move-date=2019-10-10')
})

it('should call next', function() {
expect(nextSpy).to.be.calledOnceWithExactly()
})
})
})

describe('#checkCurrentLocation()', function() {
let req, nextSpy

Expand Down
4 changes: 4 additions & 0 deletions common/templates/form-wizard.njk
Original file line number Diff line number Diff line change
Expand Up @@ -47,5 +47,9 @@
{{ govukButton({
text: t(options.buttonText or "actions::continue")
}) }}

{% if cancelUrl %}
<a href="{{ cancelUrl }}" class="govuk-button govuk-button--text">Cancel</a>
{% endif %}
</form>
{% endblock %}

0 comments on commit a5d9811

Please sign in to comment.