Skip to content

Commit d1e4d53

Browse files
authored
Merge pull request #354 from NHSDigital/update-nhsuk-frontend
[DTOSS-10748] Update nhsuk frontend to v10.0.0
2 parents bdf62d7 + 85222d4 commit d1e4d53

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

56 files changed

+358
-233
lines changed

jest.setup.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import '@testing-library/jest-dom'
2+
import { outdent } from 'outdent'
23

34
/**
45
* Mock fetch() function for jsdom
@@ -8,3 +9,22 @@ Object.defineProperty(window, 'fetch', {
89
writable: true,
910
value: jest.fn().mockResolvedValue(undefined)
1011
})
12+
13+
beforeEach(() => {
14+
const stylesheet = document.createElement('style')
15+
16+
stylesheet.innerHTML = outdent`
17+
:root {
18+
--nhsuk-breakpoint-mobile: 20rem;
19+
--nhsuk-breakpoint-tablet: 40.0625rem;
20+
--nhsuk-breakpoint-desktop: 48.0625rem;
21+
--nhsuk-breakpoint-large-desktop: 61.875rem;
22+
}
23+
`
24+
25+
// Add styles for NHS.UK frontend checks
26+
document.head.appendChild(stylesheet)
27+
28+
// Flag NHS.UK frontend as supported
29+
document.body.classList.add('nhsuk-frontend-supported')
30+
})
Lines changed: 17 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,31 @@
1+
import { Component, ElementError } from 'nhsuk-frontend'
2+
13
import setSubmit from './set-submit.js'
24

35
/**
46
* Enhance an HTML form to intercept submit events and instead fetch the form action URL.
57
* If successful, show child elements with data-show-on-submit, and hide those with data-hide-on-submit.
68
* If unsuccessful, or the fetch times out, force a page refresh.
79
*/
8-
class CheckIn {
10+
export class CheckIn extends Component {
911
/**
10-
* @param {Element | null} [$root] - HTML element to use for component
12+
* @param {Element | null} $root - HTML element to use for component
1113
*/
1214
constructor($root) {
13-
if (!$root || !($root instanceof HTMLElement)) {
14-
throw new Error('CheckIn initialised without a root element')
15-
}
15+
super($root)
1616

17-
const $form = $root.querySelector('form')
17+
const $form = this.$root.querySelector('form')
1818
if (!$form) {
19-
throw new Error('CheckIn initialised without a form element')
19+
throw new ElementError({
20+
element: $root,
21+
component: CheckIn,
22+
identifier: 'Form element (`$form`)'
23+
})
2024
}
2125

22-
this.$root = $root
2326
this.$form = $form
24-
this.$showOnSubmit = $root.querySelectorAll('[data-show-on-submit]')
25-
this.$hideOnSubmit = $root.querySelectorAll('[data-hide-on-submit]')
27+
this.$showOnSubmit = this.$root.querySelectorAll('[data-show-on-submit]')
28+
this.$hideOnSubmit = this.$root.querySelectorAll('[data-hide-on-submit]')
2629

2730
const showResult = this.showResult.bind(this)
2831

@@ -44,19 +47,9 @@ class CheckIn {
4447
this.$hideOnSubmit.forEach(($elem) => $elem.setAttribute('hidden', ''))
4548
this.$showOnSubmit.forEach(($elem) => $elem.removeAttribute('hidden'))
4649
}
47-
}
48-
49-
/**
50-
* Initialise check in component
51-
*
52-
* @param {object} [options]
53-
* @param {Element | Document | null} [options.scope] - Scope of the document to search within
54-
*/
55-
export function initCheckIn(options = {}) {
56-
const $scope = options.scope || document
57-
const $elements = $scope.querySelectorAll('[data-module="app-check-in"]')
5850

59-
$elements.forEach(($root) => {
60-
new CheckIn($root)
61-
})
51+
/**
52+
* Name for the component used when initialising using data-module attributes
53+
*/
54+
static moduleName = 'app-check-in'
6255
}

manage_breast_screening/assets/js/check-in.test.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import { getByRole } from '@testing-library/dom'
22
import { userEvent } from '@testing-library/user-event'
3+
import { createAll } from 'nhsuk-frontend'
34

4-
import { initCheckIn } from './check-in.js'
5+
import { CheckIn } from './check-in.js'
56

67
describe('Check in', () => {
78
const user = userEvent.setup()
@@ -20,7 +21,7 @@ describe('Check in', () => {
2021

2122
beforeEach(() => {
2223
document.body.innerHTML = `
23-
<div data-module="app-check-in">
24+
<div data-module="${CheckIn.moduleName}">
2425
<div data-hide-on-submit>Not submitted</div>
2526
<div data-show-on-submit hidden>Submitted</div>
2627
<form method="post" action="/example" novalidate>
@@ -47,7 +48,7 @@ describe('Check in', () => {
4748
})
4849
)
4950

50-
initCheckIn()
51+
createAll(CheckIn)
5152

5253
await user.click(button)
5354

@@ -65,7 +66,7 @@ describe('Check in', () => {
6566
})
6667
)
6768

68-
initCheckIn()
69+
createAll(CheckIn)
6970

7071
await user.click(button)
7172

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
1-
import 'nhsuk-frontend/packages/nhsuk.js'
1+
import { createAll, initAll } from 'nhsuk-frontend'
22

3-
import { initCheckIn } from './check-in.js'
3+
import { CheckIn } from './check-in.js'
44

55
document.addEventListener('DOMContentLoaded', () => {
6-
initCheckIn()
6+
// Initialise NHS.UK frontend components
7+
initAll()
8+
9+
// Initialise application components
10+
createAll(CheckIn)
711
})
Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,27 @@
11
import { waitFor } from '@testing-library/dom'
2+
import { outdent } from 'outdent'
23

3-
import { initCheckIn } from './check-in.js'
4-
5-
jest.mock('./check-in.js')
4+
import { CheckIn } from './check-in.js'
65

76
describe('Automatic initialisation', () => {
7+
beforeEach(() => {
8+
jest.spyOn(CheckIn, 'checkSupport')
9+
10+
document.body.innerHTML = outdent`
11+
<div data-module="${CheckIn.moduleName}">
12+
<form method="post" action="/example" novalidate></form>
13+
</div>
14+
`
15+
})
16+
817
it('should init components on DOMContentLoaded', async () => {
918
await import('./index.js')
1019

1120
// Should not initialise on import
12-
expect(initCheckIn).not.toHaveBeenCalled()
21+
expect(CheckIn.checkSupport).not.toHaveBeenCalled()
1322

1423
// Should initialise on DOMContentLoaded
1524
window.document.dispatchEvent(new Event('DOMContentLoaded'))
16-
await waitFor(() => expect(initCheckIn).toHaveBeenCalled())
25+
await waitFor(() => expect(CheckIn.checkSupport).toHaveBeenCalled())
1726
})
1827
})

manage_breast_screening/assets/js/set-submit.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { ElementError } from 'nhsuk-frontend'
2+
13
const TIMEOUT = 2000
24

35
/**
@@ -13,14 +15,20 @@ const TIMEOUT = 2000
1315
*/
1416
export default ($form, options = {}) => {
1517
if (!$form || !($form instanceof HTMLFormElement)) {
16-
throw new Error('setSubmit must be called with an HTMLFormElement')
18+
throw new ElementError({
19+
element: $form,
20+
identifier: 'setSubmit: Form element (`$form`)',
21+
expectedType: 'HTMLFormElement'
22+
})
1723
}
1824

1925
const method = $form.method
2026
const action = $form.action
2127

2228
if (!method || !action) {
23-
throw new Error('Form method and action must be defined')
29+
throw new ElementError({
30+
identifier: 'setSubmit: Form method or action'
31+
})
2432
}
2533

2634
function doSubmit() {

manage_breast_screening/assets/sass/components/_button.scss

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
@use "../vendor/nhsuk-frontend/base" as *;
2+
13
.app-button--link {
24
@include nhsuk-link-style-default;
35

manage_breast_screening/assets/sass/components/_count.scss

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
@use "../vendor/nhsuk-frontend/base" as *;
2+
13
.app-count {
24
@include nhsuk-font-size(16);
35

manage_breast_screening/assets/sass/components/_secondary-navigation.scss

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
@use "../vendor/nhsuk-frontend/base" as *;
2+
13
/* FIXME: taken from prototype */
24
$govuk-spacing1: 5px;
35
$govuk-spacing2: 10px;
Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,6 @@
1+
@use "../vendor/nhsuk-frontend/base" as *;
2+
13
.app-special-appointment-banner__action {
24
text-align: right;
35
@include nhsuk-font-size(19);
46
}
5-
6-
.app-special-appointment-banner__tag--no-details {
7-
color: $nhsuk-secondary-text-color;
8-
}
9-
10-
.app-special-appointment-banner__tag--temporary {
11-
@extend .nhsuk-tag;
12-
@extend .nhsuk-tag--white;
13-
margin-top: nhsuk-spacing(2);
14-
}

0 commit comments

Comments
 (0)