| 1 |
const mustache = require( 'mustache' ); |
| 2 |
const fs = require( 'fs' ); |
| 3 |
const tableOfContentsTemplate = fs.readFileSync( 'includes/templates/TableOfContents.mustache', 'utf8' ); |
| 4 |
const tableOfContentsContentsTemplate = fs.readFileSync( 'includes/templates/TableOfContents__list.mustache', 'utf8' ); |
| 5 |
const tableOfContentsLineTemplate = fs.readFileSync( 'includes/templates/TableOfContents__line.mustache', 'utf8' ); |
| 6 |
const pinnableElementOpenTemplate = fs.readFileSync( 'includes/templates/PinnableElement/Open.mustache', 'utf8' ); |
| 7 |
const pinnableElementCloseTemplate = fs.readFileSync( 'includes/templates/PinnableElement/Close.mustache', 'utf8' ); |
| 8 |
const pinnableHeaderTemplate = fs.readFileSync( 'includes/templates/PinnableHeader.mustache', 'utf8' ); |
| 9 |
const initTableOfContents = require( '../../resources/skins.vector.js/tableOfContents.js' ); |
| 10 |
|
| 11 |
let container, |
| 12 |
fooSection, |
| 13 |
barSection, |
| 14 |
bazSection, |
| 15 |
quxSection, |
| 16 |
quuxSection; |
| 17 |
const onHeadingClick = jest.fn(); |
| 18 |
const onHashChange = jest.fn(); |
| 19 |
const onToggleClick = jest.fn(); |
| 20 |
const onTogglePinned = jest.fn(); |
| 21 |
|
| 22 |
const SECTIONS = [ |
| 23 |
{ |
| 24 |
toclevel: 1, |
| 25 |
number: '1', |
| 26 |
line: 'foo', |
| 27 |
anchor: 'foo', |
| 28 |
linkAnchor: 'foo', |
| 29 |
'is-top-level-section': true, |
| 30 |
'is-parent-section': false, |
| 31 |
'array-sections': null |
| 32 |
}, { |
| 33 |
toclevel: 1, |
| 34 |
number: '2', |
| 35 |
line: 'bar', |
| 36 |
anchor: 'bar', |
| 37 |
linkAnchor: 'bar', |
| 38 |
'is-top-level-section': true, |
| 39 |
'is-parent-section': true, |
| 40 |
'vector-button-label': 'Toggle bar subsection', |
| 41 |
'array-sections': [ { |
| 42 |
toclevel: 2, |
| 43 |
number: '2.1', |
| 44 |
line: 'baz', |
| 45 |
anchor: 'baz', |
| 46 |
linkAnchor: 'baz', |
| 47 |
'is-top-level-section': false, |
| 48 |
'is-parent-section': true, |
| 49 |
'array-sections': [ { |
| 50 |
toclevel: 3, |
| 51 |
number: '2.1.1', |
| 52 |
line: 'qux', |
| 53 |
anchor: 'qux', |
| 54 |
linkAnchor: 'qux', |
| 55 |
'is-top-level-section': false, |
| 56 |
'is-parent-section': false, |
| 57 |
'array-sections': null |
| 58 |
} ] |
| 59 |
} ] |
| 60 |
}, { |
| 61 |
toclevel: 1, |
| 62 |
number: '3', |
| 63 |
line: 'quux', |
| 64 |
anchor: 'quux', |
| 65 |
linkAnchor: 'quux', |
| 66 |
'is-top-level-section': true, |
| 67 |
'is-parent-section': false, |
| 68 |
'array-sections': null |
| 69 |
} |
| 70 |
]; |
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
function render( templateProps = {} ) { |
| 77 |
const templateData = Object.assign( { |
| 78 |
'msg-vector-toc-beginning': 'Beginning', |
| 79 |
'vector-is-collapse-sections-enabled': false, |
| 80 |
'array-sections': SECTIONS, |
| 81 |
id: 'vector-toc', |
| 82 |
'data-pinnable-header': { |
| 83 |
'is-pinned': true, |
| 84 |
'data-feature-name': 'pinned', |
| 85 |
'data-pinnable-element-id': 'vector-toc', |
| 86 |
label: 'Contents', |
| 87 |
'label-tag-name': 'h2', |
| 88 |
'pin-label': 'move to sidebar', |
| 89 |
'unpin-label': 'hide' |
| 90 |
} |
| 91 |
}, templateProps ); |
| 92 |
|
| 93 |
return mustache.render( tableOfContentsTemplate, templateData, { |
| 94 |
'PinnableElement/Open': pinnableElementOpenTemplate, |
| 95 |
'PinnableElement/Close': pinnableElementCloseTemplate, |
| 96 |
PinnableHeader: pinnableHeaderTemplate, |
| 97 |
TableOfContents__list: tableOfContentsContentsTemplate, |
| 98 |
TableOfContents__line: tableOfContentsLineTemplate |
| 99 |
} ); |
| 100 |
} |
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
function mount( templateProps = {} ) { |
| 107 |
document.body.innerHTML = render( templateProps ); |
| 108 |
|
| 109 |
container = ( document.getElementById( 'vector-toc' ) ); |
| 110 |
fooSection = ( document.getElementById( 'toc-foo' ) ); |
| 111 |
barSection = ( document.getElementById( 'toc-bar' ) ); |
| 112 |
bazSection = ( document.getElementById( 'toc-baz' ) ); |
| 113 |
quxSection = ( document.getElementById( 'toc-qux' ) ); |
| 114 |
quuxSection = ( document.getElementById( 'toc-quux' ) ); |
| 115 |
|
| 116 |
return initTableOfContents( { |
| 117 |
container, |
| 118 |
onHeadingClick, |
| 119 |
onHashChange, |
| 120 |
onToggleClick, |
| 121 |
onTogglePinned |
| 122 |
} ); |
| 123 |
} |
| 124 |
|
| 125 |
describe( 'Table of contents', () => { |
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
let toc; |
| 130 |
|
| 131 |
beforeEach( () => { |
| 132 |
global.window.matchMedia = jest.fn( () => ( {} ) ); |
| 133 |
} ); |
| 134 |
|
| 135 |
afterEach( () => { |
| 136 |
if ( toc ) { |
| 137 |
toc.unmount(); |
| 138 |
toc = undefined; |
| 139 |
} |
| 140 |
|
| 141 |
mw.util.getTargetFromFragment = undefined; |
| 142 |
} ); |
| 143 |
|
| 144 |
describe( 'renders', () => { |
| 145 |
test( 'when `vector-is-collapse-sections-enabled` is false', () => { |
| 146 |
toc = mount(); |
| 147 |
expect( document.body.innerHTML ).toMatchSnapshot(); |
| 148 |
expect( barSection.classList.contains( toc.EXPANDED_SECTION_CLASS ) ).toEqual( true ); |
| 149 |
} ); |
| 150 |
test( 'when `vector-is-collapse-sections-enabled` is true', () => { |
| 151 |
toc = mount( { 'vector-is-collapse-sections-enabled': true } ); |
| 152 |
expect( document.body.innerHTML ).toMatchSnapshot(); |
| 153 |
expect( barSection.classList.contains( toc.EXPANDED_SECTION_CLASS ) ).toEqual( false ); |
| 154 |
} ); |
| 155 |
test( 'toggles for top level parent sections', () => { |
| 156 |
toc = mount(); |
| 157 |
expect( fooSection.getElementsByClassName( toc.TOGGLE_CLASS ).length ).toEqual( 0 ); |
| 158 |
expect( barSection.getElementsByClassName( toc.TOGGLE_CLASS ).length ).toEqual( 1 ); |
| 159 |
expect( bazSection.getElementsByClassName( toc.TOGGLE_CLASS ).length ).toEqual( 0 ); |
| 160 |
expect( quxSection.getElementsByClassName( toc.TOGGLE_CLASS ).length ).toEqual( 0 ); |
| 161 |
expect( quuxSection.getElementsByClassName( toc.TOGGLE_CLASS ).length ).toEqual( 0 ); |
| 162 |
} ); |
| 163 |
} ); |
| 164 |
|
| 165 |
describe( 'binds event listeners', () => { |
| 166 |
test( 'for onHeadingClick', () => { |
| 167 |
toc = mount(); |
| 168 |
const heading = ( document.querySelector( `#toc-foo .${ toc.LINK_CLASS }` ) ); |
| 169 |
heading.click(); |
| 170 |
|
| 171 |
expect( onToggleClick ).not.toBeCalled(); |
| 172 |
expect( onHashChange ).not.toBeCalled(); |
| 173 |
expect( onHeadingClick ).toBeCalled(); |
| 174 |
} ); |
| 175 |
test( 'for onToggleClick', () => { |
| 176 |
toc = mount(); |
| 177 |
const toggle = ( document.querySelector( `#toc-bar .${ toc.TOGGLE_CLASS }` ) ); |
| 178 |
toggle.click(); |
| 179 |
|
| 180 |
expect( onHeadingClick ).not.toBeCalled(); |
| 181 |
expect( onHashChange ).not.toBeCalled(); |
| 182 |
expect( onToggleClick ).toBeCalled(); |
| 183 |
} ); |
| 184 |
test( 'for onHashChange', () => { |
| 185 |
mw.util.getTargetFromFragment = jest.fn().mockImplementation( ( hash ) => hash === 'toc-foo' ? fooSection : null ); |
| 186 |
mount(); |
| 187 |
|
| 188 |
|
| 189 |
location.hash = 'foo'; |
| 190 |
window.dispatchEvent( new HashChangeEvent( 'hashchange', { |
| 191 |
oldURL: 'http://example.com', |
| 192 |
newURL: 'http://example.com#foo' |
| 193 |
} ) ); |
| 194 |
|
| 195 |
expect( onHeadingClick ).not.toBeCalled(); |
| 196 |
expect( onToggleClick ).not.toBeCalled(); |
| 197 |
expect( onHashChange ).toBeCalled(); |
| 198 |
} ); |
| 199 |
} ); |
| 200 |
|
| 201 |
describe( 'applies correct classes', () => { |
| 202 |
test( 'when changing active sections', () => { |
| 203 |
toc = mount( { 'vector-is-collapse-sections-enabled': true } ); |
| 204 |
let activeSections; |
| 205 |
let activeTopSections; |
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
function testActiveClasses( id, activeSection, activeTopSection ) { |
| 213 |
toc.changeActiveSection( id ); |
| 214 |
activeSections = container.querySelectorAll( `.${ toc.ACTIVE_SECTION_CLASS }` ); |
| 215 |
activeTopSections = container.querySelectorAll( `.${ toc.ACTIVE_TOP_SECTION_CLASS }` ); |
| 216 |
expect( activeSections.length ).toEqual( 1 ); |
| 217 |
expect( activeTopSections.length ).toEqual( 1 ); |
| 218 |
expect( activeSections[ 0 ] ).toEqual( activeSection ); |
| 219 |
expect( activeTopSections[ 0 ] ).toEqual( activeTopSection ); |
| 220 |
} |
| 221 |
|
| 222 |
testActiveClasses( 'toc-foo', fooSection, fooSection ); |
| 223 |
testActiveClasses( 'toc-bar', barSection, barSection ); |
| 224 |
testActiveClasses( 'toc-baz', bazSection, barSection ); |
| 225 |
testActiveClasses( 'toc-qux', quxSection, barSection ); |
| 226 |
testActiveClasses( 'toc-quux', quuxSection, quuxSection ); |
| 227 |
} ); |
| 228 |
|
| 229 |
test( 'when expanding sections', () => { |
| 230 |
toc = mount(); |
| 231 |
toc.expandSection( 'toc-bar' ); |
| 232 |
expect( barSection.classList.contains( toc.EXPANDED_SECTION_CLASS ) ).toEqual( true ); |
| 233 |
} ); |
| 234 |
|
| 235 |
test( 'when toggling sections', () => { |
| 236 |
toc = mount(); |
| 237 |
toc.toggleExpandSection( 'toc-bar' ); |
| 238 |
expect( barSection.classList.contains( toc.EXPANDED_SECTION_CLASS ) ).toEqual( false ); |
| 239 |
toc.toggleExpandSection( 'toc-bar' ); |
| 240 |
expect( barSection.classList.contains( toc.EXPANDED_SECTION_CLASS ) ).toEqual( true ); |
| 241 |
} ); |
| 242 |
} ); |
| 243 |
|
| 244 |
describe( 'applies the correct aria attributes', () => { |
| 245 |
test( 'when initialized', () => { |
| 246 |
toc = mount(); |
| 247 |
const toggleButton = ( barSection.querySelector( `.${ toc.TOGGLE_CLASS }` ) ); |
| 248 |
|
| 249 |
expect( toggleButton.getAttribute( 'aria-expanded' ) ).toEqual( 'true' ); |
| 250 |
} ); |
| 251 |
|
| 252 |
test( 'when expanding sections', () => { |
| 253 |
toc = mount(); |
| 254 |
const toggleButton = ( barSection.querySelector( `.${ toc.TOGGLE_CLASS }` ) ); |
| 255 |
|
| 256 |
toc.expandSection( 'toc-bar' ); |
| 257 |
expect( toggleButton.getAttribute( 'aria-expanded' ) ).toEqual( 'true' ); |
| 258 |
} ); |
| 259 |
|
| 260 |
test( 'when toggling sections', () => { |
| 261 |
toc = mount(); |
| 262 |
const toggleButton = ( barSection.querySelector( `.${ toc.TOGGLE_CLASS }` ) ); |
| 263 |
|
| 264 |
toc.toggleExpandSection( 'toc-bar' ); |
| 265 |
expect( toggleButton.getAttribute( 'aria-expanded' ) ).toEqual( 'false' ); |
| 266 |
|
| 267 |
toc.toggleExpandSection( 'toc-bar' ); |
| 268 |
expect( toggleButton.getAttribute( 'aria-expanded' ) ).toEqual( 'true' ); |
| 269 |
} ); |
| 270 |
} ); |
| 271 |
|
| 272 |
describe( 'when the hash fragment changes', () => { |
| 273 |
test( 'expands and activates corresponding section', () => { |
| 274 |
mw.util.getTargetFromFragment = jest.fn().mockImplementation( ( hash ) => hash === 'toc-qux' ? quxSection : null ); |
| 275 |
toc = mount( { 'vector-is-collapse-sections-enabled': true } ); |
| 276 |
expect( |
| 277 |
quxSection.classList.contains( toc.ACTIVE_SECTION_CLASS ) |
| 278 |
).toEqual( false ); |
| 279 |
|
| 280 |
|
| 281 |
location.hash = 'qux'; |
| 282 |
window.dispatchEvent( new HashChangeEvent( 'hashchange', { |
| 283 |
oldURL: 'http://example.com', |
| 284 |
newURL: 'http://example.com#qux' |
| 285 |
} ) ); |
| 286 |
|
| 287 |
const activeSections = container.querySelectorAll( `.${ toc.ACTIVE_SECTION_CLASS }` ); |
| 288 |
const activeTopSections = container.querySelectorAll( `.${ toc.ACTIVE_TOP_SECTION_CLASS }` ); |
| 289 |
expect( activeSections.length ).toEqual( 1 ); |
| 290 |
expect( activeTopSections.length ).toEqual( 1 ); |
| 291 |
expect( |
| 292 |
barSection.classList.contains( toc.ACTIVE_TOP_SECTION_CLASS ) |
| 293 |
).toEqual( true ); |
| 294 |
expect( |
| 295 |
barSection.classList.contains( toc.EXPANDED_SECTION_CLASS ) |
| 296 |
).toEqual( true ); |
| 297 |
expect( |
| 298 |
quxSection.classList.contains( toc.ACTIVE_SECTION_CLASS ) |
| 299 |
).toEqual( true ); |
| 300 |
} ); |
| 301 |
} ); |
| 302 |
|
| 303 |
describe( 'reloadTableOfContents', () => { |
| 304 |
test( 're-renders toc when wikipage.tableOfContents hook is fired with empty sections', async () => { |
| 305 |
toc = mount(); |
| 306 |
await toc.reloadTableOfContents( [] ); |
| 307 |
|
| 308 |
expect( document.body.innerHTML ).toMatchSnapshot(); |
| 309 |
} ); |
| 310 |
|
| 311 |
test( 're-renders toc when wikipage.tableOfContents hook is fired with sections', async () => { |
| 312 |
jest.spyOn( mw.loader, 'using' ).mockImplementation( () => Promise.resolve() ); |
| 313 |
mw.template.getCompiler = () => {}; |
| 314 |
jest.spyOn( mw, 'message' ).mockImplementation( ( msg ) => { |
| 315 |
const msgFactory = ( text ) => ( { |
| 316 |
parse: () => '', |
| 317 |
plain: () => '', |
| 318 |
escaped: () => '', |
| 319 |
exists: () => true, |
| 320 |
text: () => text |
| 321 |
} ); |
| 322 |
switch ( msg ) { |
| 323 |
case 'vector-toc-beginning': |
| 324 |
return msgFactory( 'Beginning' ); |
| 325 |
default: |
| 326 |
return msgFactory( '' ); |
| 327 |
} |
| 328 |
|
| 329 |
} ); |
| 330 |
jest.spyOn( mw.template, 'getCompiler' ).mockImplementation( () => ( { |
| 331 |
compile: () => ( { |
| 332 |
render: ( data ) => ( { |
| 333 |
html: () => render( data ) |
| 334 |
} ) |
| 335 |
} ) |
| 336 |
} ) ); |
| 337 |
|
| 338 |
toc = mount(); |
| 339 |
|
| 340 |
const toggleButton = ( barSection.querySelector( `.${ toc.TOGGLE_CLASS }` ) ); |
| 341 |
|
| 342 |
toc.toggleExpandSection( 'toc-bar' ); |
| 343 |
expect( toggleButton.getAttribute( 'aria-expanded' ) ).toEqual( 'false' ); |
| 344 |
|
| 345 |
|
| 346 |
|
| 347 |
|
| 348 |
await toc.reloadTableOfContents( [ |
| 349 |
|
| 350 |
SECTIONS[ 0 ], |
| 351 |
|
| 352 |
SECTIONS[ 1 ], |
| 353 |
|
| 354 |
SECTIONS[ 1 ][ 'array-sections' ][ 0 ], |
| 355 |
|
| 356 |
SECTIONS[ 1 ][ 'array-sections' ][ 0 ][ 'array-sections' ][ 0 ], |
| 357 |
|
| 358 |
SECTIONS[ 2 ], |
| 359 |
|
| 360 |
{ |
| 361 |
toclevel: 1, |
| 362 |
number: '4', |
| 363 |
line: 'bat', |
| 364 |
anchor: 'bat', |
| 365 |
linkAnchor: 'bat', |
| 366 |
'is-top-level-section': true, |
| 367 |
'is-parent-section': false, |
| 368 |
'array-sections': null |
| 369 |
} |
| 370 |
] ); |
| 371 |
|
| 372 |
const newToggleButton = ( document.querySelector( `#toc-bar .${ toc.TOGGLE_CLASS }` ) ); |
| 373 |
expect( newToggleButton ).not.toBeNull(); |
| 374 |
|
| 375 |
expect( newToggleButton.getAttribute( 'aria-expanded' ) ).toEqual( 'true' ); |
| 376 |
|
| 377 |
|
| 378 |
expect( document.body.innerHTML ).toMatchSnapshot(); |
| 379 |
} ); |
| 380 |
} ); |
| 381 |
} ); |