Skip to content

Commit

Permalink
Merge pull request #227 from ministryofjustice/right-hand-create-move…
Browse files Browse the repository at this point in the history
…-summary-panel

[P4-497] Create Move right hand summary panel
  • Loading branch information
feedmypixel authored Sep 9, 2019
2 parents fc4aa1f + 9332fc3 commit 79caf8a
Show file tree
Hide file tree
Showing 8 changed files with 327 additions and 69 deletions.
16 changes: 16 additions & 0 deletions app/move/controllers/create/base.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const FormWizardController = require('../../../../common/controllers/form-wizard')
const presenters = require('../../../../common/presenters')

class CreateBaseController extends FormWizardController {
middlewareChecks() {
Expand All @@ -9,6 +10,7 @@ class CreateBaseController extends FormWizardController {
middlewareLocals() {
super.middlewareLocals()
this.use(this.setCancelUrl)
this.use(this.setMoveSummary)
}

setCancelUrl(req, res, next) {
Expand All @@ -24,6 +26,20 @@ class CreateBaseController extends FormWizardController {

next()
}

setMoveSummary(req, res, next) {
const currentLocation = req.session.currentLocation
const sessionModel = req.sessionModel.toJSON()
const moveSummary = presenters.moveToMetaListComponent({
...sessionModel,
from_location: currentLocation,
})

res.locals.person = sessionModel.person
res.locals.moveSummary = sessionModel.move_type ? moveSummary : {}

next()
}
}

module.exports = CreateBaseController
94 changes: 94 additions & 0 deletions app/move/controllers/create/base.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,5 +123,99 @@ describe('Move controllers', function() {
})
})
})

describe('#setMoveSummary()', function() {
let req, res, nextSpy

const mockPerson = {
first_names: 'Mr',
fullname: 'Benn, Mr',
last_name: 'Benn',
}
const mockSessionModel = overrides => {
const sessionModel = {
date: '2019-06-09',
time_due: '2000-01-01T14:00:00Z',
move_type: 'court_appearance',
to_location: {
title: 'Mock to location',
},
additional_information: 'Additional information',
person: mockPerson,
...overrides,
}

return {
...sessionModel,
toJSON: () => sessionModel,
get: () => sessionModel.person,
}
}
const expectedMoveSummary = {
items: [
{ key: { text: 'From' }, value: { text: 'Mock location' } },
{
key: { text: 'To' },
value: { text: 'Mock to location — Additional information' },
},
{ key: { text: 'Date' }, value: { text: 'Sunday 9 Jun 2019' } },
{ key: { text: 'Time due' }, value: { text: '2pm' } },
],
}

beforeEach(function() {
nextSpy = sinon.spy()
req = {
session: {
currentLocation: {
title: 'Mock location',
},
},
}
res = {
locals: {},
}
})

context('when current location exists', function() {
beforeEach(async function() {
req.sessionModel = mockSessionModel()

await controller.setMoveSummary(req, res, nextSpy)
})

it('should set locals as expected', function() {
expect(res.locals).to.deep.equal({
person: mockPerson,
moveSummary: expectedMoveSummary,
})
})

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

context('without move_type', function() {
beforeEach(async function() {
req.sessionModel = mockSessionModel({
move_type: '',
})

await controller.setMoveSummary(req, res, nextSpy)
})

it('should set locals as expected', function() {
expect(res.locals).to.deep.equal({
person: mockPerson,
moveSummary: {},
})
})

it('should call next without error', function() {
expect(nextSpy).to.be.calledOnceWithExactly()
})
})
})
})
})
18 changes: 18 additions & 0 deletions app/move/controllers/create/move-details.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,24 @@ class MoveDetailsController extends CreateBaseController {

next()
}

async successHandler(req, res, next) {
try {
const { to_location: toLocationId } = req.sessionModel.toJSON()

if (toLocationId) {
const locationDetail = await referenceDataService.getLocationById(
toLocationId
)

req.sessionModel.set('to_location', locationDetail)
}

super.successHandler(req, res, next)
} catch (error) {
next(error)
}
}
}

module.exports = MoveDetailsController
91 changes: 91 additions & 0 deletions app/move/controllers/create/move-details.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -292,5 +292,96 @@ describe('Move controllers', function() {
})
})
})

describe('#successHandler()', function() {
const toLocationId = '123456-7'
const mockLocationDetail = {
title: 'mock to location',
}
let req, res, nextSpy

beforeEach(function() {
nextSpy = sinon.spy()
req = {
sessionModel: {
toJSON: sinon.stub(),
set: sinon.spy(),
},
}
res = {
locals: {},
}

sinon.spy(FormController.prototype, 'successHandler')
})

context('with location_id', function() {
beforeEach(async function() {
req.sessionModel.toJSON.returns({ to_location: toLocationId })
sinon
.stub(referenceDataService, 'getLocationById')
.resolves(mockLocationDetail)

await controller.successHandler(req, res, nextSpy)
})

it('should set to_location in session as expected', function() {
expect(req.sessionModel.set).to.be.calledOnceWithExactly(
'to_location',
mockLocationDetail
)
})

it('should call parent successHandler', function() {
expect(FormController.prototype.successHandler).to.be.calledOnceWith(
req,
res,
nextSpy
)
})
})

context('without location_id', function() {
beforeEach(async function() {
req.sessionModel.toJSON.returns({ to_location: '' })
sinon
.stub(referenceDataService, 'getLocationById')
.resolves(mockLocationDetail)

await controller.successHandler(req, res, nextSpy)
})

it('should not set to_location in session', function() {
expect(req.sessionModel.set).not.to.be.called
})

it('should call parent successHandler', function() {
expect(FormController.prototype.successHandler).to.be.calledOnceWith(
req,
res,
nextSpy
)
})
})

context('when getLocationById throws and error', function() {
const errorMock = new Error('Problem')

beforeEach(async function() {
req.sessionModel.toJSON.returns({ to_location: toLocationId })
sinon.stub(referenceDataService, 'getLocationById').throws(errorMock)

await controller.successHandler(req, res, nextSpy)
})

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

it('should not call parent successHandler', function() {
expect(FormController.prototype.successHandler).not.to.be.called
})
})
})
})
})
54 changes: 28 additions & 26 deletions app/move/views/view.njk
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
{% extends "layouts/base.njk" %}
{% extends "layouts/two-column.njk" %}

{% set additionalContainerClass = "sticky-sidebar-container" %}

{% block customGtagConfig %}
gtag('set', {'page_title': 'Move details'});
Expand Down Expand Up @@ -39,7 +41,7 @@
{% endif %}
{% endblock %}

{% block content %}
{% block contentHeader %}
<header class="govuk-!-margin-bottom-8">
<h1 class="govuk-heading-xl govuk-!-margin-bottom-1">
{{ move.person.fullname | upper }}
Expand All @@ -59,36 +61,36 @@
<div>
{% endif %}
</header>
{% endblock %}

<div class="govuk-grid-row sticky-sidebar-container">
<div class="govuk-grid-column-two-thirds">
<h2 class="govuk-heading-m">
{{ t("moves::steps.personal_details.heading") }}
</h2>
{% block contentMain %}
<h2 class="govuk-heading-m">
{{ t("moves::steps.personal_details.heading") }}
</h2>

{{ govukSummaryList(personalDetailsSummary) }}
{{ govukSummaryList(personalDetailsSummary) }}

{% include "move/views/_includes/assessment.njk" %}
{% include "move/views/_includes/assessment.njk" %}

{% if canAccess("move:cancel") and move.status == "requested" %}
<p class="govuk-!-margin-top-9 govuk-!-margin-bottom-0">
<a href="{{ move.id }}/cancel" class="app-link--destructive">
{{ t("actions::cancel_move") }}
</a>
</p>
{% endif %}
</div>
{% if canAccess("move:cancel") and move.status == "requested" %}
<p class="govuk-!-margin-top-9 govuk-!-margin-bottom-0">
<a href="{{ move.id }}/cancel" class="app-link--destructive">
{{ t("actions::cancel_move") }}
</a>
</p>
{% endif %}
{% endblock %}

<div class="govuk-grid-column-one-third">
<div class="sticky-sidebar">
<div class="sticky-sidebar__inner">
<h3 class="govuk-heading-m app-border-top-2 app-border--blue govuk-!-padding-top-4">
{{ move.person.fullname | upper }}
</h3>
{% block contentSidebar %}
{% if moveSummary %}
<div class="sticky-sidebar">
<div class="sticky-sidebar__inner">
<h3 class="govuk-heading-m app-border-top-2 app-border--blue govuk-!-padding-top-4">
{{ move.person.fullname | upper }}
</h3>

{{ appMetaList(moveSummary) }}
</div>
{{ appMetaList(moveSummary) }}
</div>
</div>
</div>
{% endif %}
{% endblock %}
Loading

0 comments on commit 79caf8a

Please sign in to comment.