@@ -5,6 +5,7 @@ describe('Table of contents', function () {
5
5
var $html
6
6
var $tocBase
7
7
var $toc
8
+ var $mainContentPane
8
9
var $closeButton
9
10
var $openButton
10
11
var $tocStickyHeader
@@ -13,28 +14,33 @@ describe('Table of contents', function () {
13
14
beforeAll ( function ( ) {
14
15
$html = $ ( 'html' )
15
16
$tocBase = $ (
16
- '<div class="toc" data-module="table-of-contents" tabindex="-1" aria-label="Table of contents">' +
17
- '<div class="search" data-module="search" data-path-to-site-root="/">' +
18
- '<form action="/search/index.html" method="get" role="search" class="search__form govuk-!-margin-bottom-4">' +
19
- '<label class="govuk-label search__label" for="search">Search this documentation</label>' +
20
- '<input type="text" id="search" name="q" class="govuk-input govuk-!-margin-bottom-0 search__input" aria-controls="search-results" placeholder="Search">' +
21
- '<button type="submit" class="search__button">Search</button>' +
22
- '</form>' +
17
+ '<div class="app-pane__toc">' +
18
+ '<div class="toc" data-module="table-of-contents" tabindex="-1" aria-label="Table of contents">' +
19
+ '<div class="search" data-module="search" data-path-to-site-root="/">' +
20
+ '<form action="/search/index.html" method="get" role="search" class="search__form govuk-!-margin-bottom-4">' +
21
+ '<label class="govuk-label search__label" for="search">Search this documentation</label>' +
22
+ '<input type="text" id="search" name="q" class="govuk-input govuk-!-margin-bottom-0 search__input" aria-controls="search-results" placeholder="Search">' +
23
+ '<button type="submit" class="search__button">Search</button>' +
24
+ '</form>' +
25
+ '</div>' +
26
+ '<button type="button" class="toc__close js-toc-close" aria-controls="toc" aria-label="Hide table of contents"></button>' +
27
+ '<nav id="toc" class="js-toc-list toc__list" aria-labelledby="toc-heading" data-module="collapsible-navigation">' +
28
+ '<ul>' +
29
+ '<li>' +
30
+ '<a href="/"><span>Technical Documentation Template</span></a>' +
31
+ '</li>' +
32
+ '<li>' +
33
+ '<a href="/"><span>Get started</span></a>' +
34
+ '</li>' +
35
+ '<li>' +
36
+ '<a href="/"><span>Configure your documentation site</span></a>' +
37
+ '</li>' +
38
+ '</ul>' +
39
+ '</nav>' +
23
40
'</div>' +
24
- '<button type="button" class="toc__close js-toc-close" aria-controls="toc" aria-label="Hide table of contents"></button>' +
25
- '<nav id="toc" class="js-toc-list toc__list" aria-labelledby="toc-heading" data-module="collapsible-navigation">' +
26
- '<ul>' +
27
- '<li>' +
28
- '<a href="/"><span>Technical Documentation Template</span></a>' +
29
- '</li>' +
30
- '<li>' +
31
- '<a href="/"><span>Get started</span></a>' +
32
- '</li>' +
33
- '<li>' +
34
- '<a href="/"><span>Configure your documentation site</span></a>' +
35
- '</li>' +
36
- '</ul>' +
37
- '</nav>' +
41
+ '</div>' +
42
+ '<div class="app-pane__content toc-open-disabled" aria-label="content">' +
43
+ '<main id="content"></main>' +
38
44
'</div>'
39
45
)
40
46
@@ -51,7 +57,7 @@ describe('Table of contents', function () {
51
57
} )
52
58
53
59
beforeEach ( function ( ) {
54
- $toc = $tocBase . clone ( )
60
+ var $tocClone = $tocBase . clone ( )
55
61
56
62
$html . find ( 'body' )
57
63
. append (
@@ -61,8 +67,10 @@ describe('Table of contents', function () {
61
67
'</button>' +
62
68
'</div>'
63
69
)
64
- . append ( $toc )
70
+ . append ( $tocClone )
65
71
72
+ $toc = $tocClone . eq ( 0 ) . find ( '.toc' )
73
+ $mainContentPane = $tocClone . eq ( 1 )
66
74
$closeButton = $toc . find ( '.js-toc-close' )
67
75
$openButton = $html . find ( '.js-toc-show' )
68
76
@@ -73,14 +81,15 @@ describe('Table of contents', function () {
73
81
// clear up any classes left on <html>
74
82
$html . removeClass ( 'toc-open' )
75
83
$html . find ( 'body #toc-heading' ) . remove ( )
76
- $html . find ( 'body .toc' ) . remove ( )
84
+ $html . find ( 'body .app-pane__toc' ) . remove ( )
85
+ $html . find ( 'body .app-pane__content' ) . remove ( )
77
86
if ( $tocStickyHeader && $tocStickyHeader . length ) {
78
87
$tocStickyHeader . remove ( )
79
88
}
80
89
} )
81
90
82
91
describe ( 'when the module is started' , function ( ) {
83
- it ( 'on a mobile-size screen, it should mark the table of contents as hidden ' , function ( ) {
92
+ it ( 'on a mobile-size screen, it should hide the table of contents and stop the main content pane being focusable ' , function ( ) {
84
93
// styles applied by this test simulate the styles media-queries will apply on real web pages
85
94
// the .mobile-size class hides the table of contents and the open button
86
95
$html . addClass ( 'mobile-size' ) // simulate the styles media-queries will apply on real web pages
@@ -89,18 +98,20 @@ describe('Table of contents', function () {
89
98
module . start ( $toc )
90
99
91
100
expect ( $toc . attr ( 'aria-hidden' ) ) . toEqual ( 'true' )
101
+ expect ( $mainContentPane . get ( 0 ) . hasAttribute ( 'tabindex' ) ) . toBe ( false )
92
102
93
103
$html . removeClass ( 'mobile-size' )
94
104
} )
95
105
96
- it ( 'on a desktop-size screen, it should mark the table of contents as visible ' , function ( ) {
106
+ it ( 'on a desktop-size screen, it should show the table of contents and make the main content pane focusable ' , function ( ) {
97
107
// styles applied by this test simulate the styles media-queries will apply on real web pages
98
108
// by default, they show the table of contents
99
109
100
110
module = new GOVUK . Modules . TableOfContents ( )
101
111
module . start ( $toc )
102
112
103
113
expect ( $toc . attr ( 'aria-hidden' ) ) . toEqual ( 'false' )
114
+ expect ( $mainContentPane . attr ( 'tabindex' ) ) . toEqual ( '0' )
104
115
} )
105
116
} )
106
117
@@ -156,10 +167,15 @@ describe('Table of contents', function () {
156
167
157
168
describe ( 'if the open button is clicked' , function ( ) {
158
169
beforeEach ( function ( ) {
170
+ $html . addClass ( 'mobile-size' )
159
171
module = new GOVUK . Modules . TableOfContents ( )
160
172
module . start ( $toc )
161
173
} )
162
174
175
+ afterEach ( function ( ) {
176
+ $html . removeClass ( 'toc-open mobile-size' )
177
+ } )
178
+
163
179
it ( 'the click event should be cancelled' , function ( ) {
164
180
var clickEvt = new $ . Event ( 'click' )
165
181
@@ -168,7 +184,7 @@ describe('Table of contents', function () {
168
184
expect ( clickEvt . isDefaultPrevented ( ) ) . toBe ( true )
169
185
} )
170
186
171
- it ( 'the table of contents should show and be focused' , function ( ) {
187
+ it ( 'the table of contents should show and be focused and the main content hidden ' , function ( ) {
172
188
// detecting focus has proved unreliable so track calls to $toc.focus instead
173
189
var _focus = $ . fn . focus
174
190
var tocFocusSpy = jasmine . createSpy ( 'tocFocusSpy' )
@@ -188,6 +204,7 @@ describe('Table of contents', function () {
188
204
$openButton . trigger ( clickEvt )
189
205
190
206
expect ( $toc . attr ( 'aria-hidden' ) ) . toEqual ( 'false' )
207
+ expect ( $mainContentPane . attr ( 'aria-hidden' ) ) . toEqual ( 'true' )
191
208
192
209
expect ( tocFocusSpy ) . toHaveBeenCalled ( )
193
210
@@ -197,7 +214,8 @@ describe('Table of contents', function () {
197
214
} )
198
215
199
216
describe ( 'if the close button is clicked' , function ( ) {
200
- var clickEvt
217
+ var openClickEvt
218
+ var closeClickEvt
201
219
202
220
beforeEach ( function ( ) {
203
221
$html . addClass ( 'mobile-size' )
@@ -206,24 +224,35 @@ describe('Table of contents', function () {
206
224
module . start ( $toc )
207
225
208
226
// tocIsVisible = false // controls what $toc.is(':visible') returns, which will be controlled by CSS in a web page
209
- clickEvt = new $ . Event ( 'click' )
210
- $closeButton . trigger ( clickEvt )
227
+ openClickEvt = new $ . Event ( 'click' )
228
+ closeClickEvt = new $ . Event ( 'click' )
229
+
230
+ $openButton . trigger ( openClickEvt )
231
+ $closeButton . trigger ( closeClickEvt )
211
232
} )
212
233
213
234
afterEach ( function ( ) {
214
235
$html . removeClass ( 'mobile-size' )
215
236
} )
216
237
217
238
it ( 'the click event should be cancelled' , function ( ) {
218
- expect ( clickEvt . isDefaultPrevented ( ) ) . toBe ( true )
239
+ expect ( closeClickEvt . isDefaultPrevented ( ) ) . toBe ( true )
219
240
} )
220
241
221
242
it ( 'the table of contents should be hidden' , function ( ) {
222
243
expect ( $toc . attr ( 'aria-hidden' ) ) . toEqual ( 'true' )
223
244
} )
245
+
246
+ it ( 'the button that triggered the dialog is refocused' , function ( ) {
247
+ expect ( document . activeElement ) . toBe ( $openButton . get ( 0 ) )
248
+ } )
249
+
250
+ it ( 'the main content area should be shown' , function ( ) {
251
+ expect ( $mainContentPane . attr ( 'aria-hidden' ) ) . toEqual ( 'false' )
252
+ } )
224
253
} )
225
254
226
- it ( 'on mobile-size screens, when the table of contents is open and the escape key is activated, the table of contents should be hidden' , function ( ) {
255
+ it ( 'on mobile-size screens, when the table of contents is open and the escape key is activated, the table of contents should be hidden and the main content shown ' , function ( ) {
227
256
$html . addClass ( 'mobile-size' )
228
257
229
258
module = new GOVUK . Modules . TableOfContents ( )
@@ -236,6 +265,8 @@ describe('Table of contents', function () {
236
265
} ) )
237
266
238
267
expect ( $html . hasClass ( 'toc-open' ) ) . toBe ( false )
268
+ expect ( $toc . attr ( 'aria-hidden' ) ) . toEqual ( 'true' )
269
+ expect ( $mainContentPane . attr ( 'aria-hidden' ) ) . toEqual ( 'false' )
239
270
240
271
$html . removeClass ( 'mobile-size' )
241
272
} )
0 commit comments