2022-02-15 22:43:04 +00:00
|
|
|
const mustache = require( 'mustache' );
|
|
|
|
const fs = require( 'fs' );
|
|
|
|
const tableOfContentsTemplate = fs.readFileSync( 'includes/templates/TableOfContents.mustache', 'utf8' );
|
2023-01-16 11:39:05 +00:00
|
|
|
const tableOfContentsContentsTemplate = fs.readFileSync( 'includes/templates/TableOfContents__list.mustache', 'utf8' );
|
2022-02-15 22:43:04 +00:00
|
|
|
const tableOfContentsLineTemplate = fs.readFileSync( 'includes/templates/TableOfContents__line.mustache', 'utf8' );
|
2022-12-20 22:58:44 +00:00
|
|
|
const pinnableElementOpenTemplate = fs.readFileSync( 'includes/templates/PinnableElement/Open.mustache', 'utf8' );
|
|
|
|
const pinnableElementCloseTemplate = fs.readFileSync( 'includes/templates/PinnableElement/Close.mustache', 'utf8' );
|
2022-11-21 20:46:04 +00:00
|
|
|
const pinnableHeaderTemplate = fs.readFileSync( 'includes/templates/PinnableHeader.mustache', 'utf8' );
|
2022-02-15 22:43:04 +00:00
|
|
|
const initTableOfContents = require( '../../resources/skins.vector.es6/tableOfContents.js' );
|
2022-02-07 19:20:17 +00:00
|
|
|
|
2022-09-20 21:32:53 +00:00
|
|
|
let /** @type {HTMLElement} */ container,
|
|
|
|
/** @type {HTMLElement} */ fooSection,
|
2022-02-14 18:55:50 +00:00
|
|
|
/** @type {HTMLElement} */ barSection,
|
|
|
|
/** @type {HTMLElement} */ bazSection,
|
|
|
|
/** @type {HTMLElement} */ quxSection,
|
|
|
|
/** @type {HTMLElement} */ quuxSection;
|
2022-02-15 22:43:04 +00:00
|
|
|
const onHeadingClick = jest.fn();
|
|
|
|
const onToggleClick = jest.fn();
|
2022-10-20 21:32:07 +00:00
|
|
|
const onTogglePinned = jest.fn();
|
2022-02-07 19:20:17 +00:00
|
|
|
|
2022-09-14 03:17:27 +00:00
|
|
|
const SECTIONS = [
|
|
|
|
{
|
|
|
|
toclevel: 1,
|
|
|
|
number: '1',
|
|
|
|
line: 'foo',
|
|
|
|
anchor: 'foo',
|
|
|
|
'is-top-level-section': true,
|
|
|
|
'is-parent-section': false,
|
|
|
|
'array-sections': null
|
|
|
|
}, {
|
|
|
|
toclevel: 1,
|
|
|
|
number: '2',
|
|
|
|
line: 'bar',
|
|
|
|
anchor: 'bar',
|
|
|
|
'is-top-level-section': true,
|
|
|
|
'is-parent-section': true,
|
|
|
|
'vector-button-label': 'Toggle bar subsection',
|
|
|
|
'array-sections': [ {
|
|
|
|
toclevel: 2,
|
|
|
|
number: '2.1',
|
|
|
|
line: 'baz',
|
|
|
|
anchor: 'baz',
|
|
|
|
'is-top-level-section': false,
|
|
|
|
'is-parent-section': true,
|
|
|
|
'array-sections': [ {
|
|
|
|
toclevel: 3,
|
|
|
|
number: '2.1.1',
|
|
|
|
line: 'qux',
|
|
|
|
anchor: 'qux',
|
|
|
|
'is-top-level-section': false,
|
|
|
|
'is-parent-section': false,
|
|
|
|
'array-sections': null
|
|
|
|
} ]
|
|
|
|
} ]
|
|
|
|
}, {
|
|
|
|
toclevel: 1,
|
|
|
|
number: '3',
|
|
|
|
line: 'quux',
|
|
|
|
anchor: 'quux',
|
|
|
|
'is-top-level-section': true,
|
|
|
|
'is-parent-section': false,
|
|
|
|
'array-sections': null
|
|
|
|
}
|
|
|
|
];
|
|
|
|
|
2022-02-18 00:19:50 +00:00
|
|
|
/**
|
|
|
|
* @param {Object} templateProps
|
|
|
|
* @return {string}
|
|
|
|
*/
|
|
|
|
function render( templateProps = {} ) {
|
|
|
|
const templateData = Object.assign( {
|
2022-03-15 16:34:59 +00:00
|
|
|
'is-vector-toc-beginning-enabled': true,
|
|
|
|
'msg-vector-toc-beginning': 'Beginning',
|
2022-02-18 00:19:50 +00:00
|
|
|
'vector-is-collapse-sections-enabled': false,
|
2022-11-21 20:46:04 +00:00
|
|
|
'array-sections': SECTIONS,
|
2022-12-20 22:58:44 +00:00
|
|
|
id: 'vector-toc',
|
2022-11-21 20:46:04 +00:00
|
|
|
'data-pinnable-header': {
|
|
|
|
'is-pinned': true,
|
|
|
|
label: 'Contents',
|
|
|
|
'label-tag-name': 'h2',
|
|
|
|
'pin-label': 'move to sidebar',
|
|
|
|
'unpin-label': 'hide',
|
|
|
|
'data-name': 'vector-toc'
|
|
|
|
}
|
2022-02-18 00:19:50 +00:00
|
|
|
}, templateProps );
|
2022-02-07 19:20:17 +00:00
|
|
|
|
2022-02-18 00:19:50 +00:00
|
|
|
return mustache.render( tableOfContentsTemplate, templateData, {
|
2022-12-20 22:58:44 +00:00
|
|
|
'PinnableElement/Open': pinnableElementOpenTemplate, // eslint-disable-line camelcase
|
|
|
|
'PinnableElement/Close': pinnableElementCloseTemplate, // eslint-disable-line camelcase
|
2022-11-21 20:46:04 +00:00
|
|
|
PinnableHeader: pinnableHeaderTemplate, // eslint-disable-line camelcase
|
2023-01-16 11:39:05 +00:00
|
|
|
TableOfContents__list: tableOfContentsContentsTemplate, // eslint-disable-line camelcase
|
2022-02-14 18:55:50 +00:00
|
|
|
TableOfContents__line: tableOfContentsLineTemplate // eslint-disable-line camelcase
|
2022-02-18 00:19:50 +00:00
|
|
|
} );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {Object} templateProps
|
2022-02-14 18:55:50 +00:00
|
|
|
* @return {module:TableOfContents~TableOfContents}
|
2022-02-18 00:19:50 +00:00
|
|
|
*/
|
|
|
|
function mount( templateProps = {} ) {
|
|
|
|
document.body.innerHTML = render( templateProps );
|
|
|
|
|
2022-12-20 22:58:44 +00:00
|
|
|
container = /** @type {HTMLElement} */ ( document.getElementById( 'vector-toc' ) );
|
2022-04-05 20:38:00 +00:00
|
|
|
fooSection = /** @type {HTMLElement} */ ( document.getElementById( 'toc-foo' ) );
|
|
|
|
barSection = /** @type {HTMLElement} */ ( document.getElementById( 'toc-bar' ) );
|
|
|
|
bazSection = /** @type {HTMLElement} */ ( document.getElementById( 'toc-baz' ) );
|
|
|
|
quxSection = /** @type {HTMLElement} */ ( document.getElementById( 'toc-qux' ) );
|
|
|
|
quuxSection = /** @type {HTMLElement} */ ( document.getElementById( 'toc-quux' ) );
|
2022-09-20 21:32:53 +00:00
|
|
|
|
|
|
|
return initTableOfContents( {
|
|
|
|
container,
|
|
|
|
onHeadingClick,
|
|
|
|
onToggleClick,
|
2022-10-20 21:32:07 +00:00
|
|
|
onTogglePinned
|
2022-09-20 21:32:53 +00:00
|
|
|
} );
|
2022-02-18 00:19:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
describe( 'Table of contents', () => {
|
2022-04-19 21:56:29 +00:00
|
|
|
beforeEach( () => {
|
|
|
|
// @ts-ignore
|
|
|
|
global.window.matchMedia = jest.fn( () => ( {} ) );
|
|
|
|
} );
|
|
|
|
|
2022-03-29 20:51:45 +00:00
|
|
|
describe( 'renders', () => {
|
|
|
|
test( 'when `vector-is-collapse-sections-enabled` is false', () => {
|
|
|
|
const toc = mount();
|
|
|
|
expect( document.body.innerHTML ).toMatchSnapshot();
|
|
|
|
expect( barSection.classList.contains( toc.EXPANDED_SECTION_CLASS ) ).toEqual( true );
|
|
|
|
} );
|
|
|
|
test( 'when `vector-is-collapse-sections-enabled` is true', () => {
|
|
|
|
const toc = mount( { 'vector-is-collapse-sections-enabled': true } );
|
|
|
|
expect( document.body.innerHTML ).toMatchSnapshot();
|
|
|
|
expect( barSection.classList.contains( toc.EXPANDED_SECTION_CLASS ) ).toEqual( false );
|
|
|
|
} );
|
|
|
|
test( 'toggles for top level parent sections', () => {
|
|
|
|
const toc = mount();
|
|
|
|
expect( fooSection.getElementsByClassName( toc.TOGGLE_CLASS ).length ).toEqual( 0 );
|
|
|
|
expect( barSection.getElementsByClassName( toc.TOGGLE_CLASS ).length ).toEqual( 1 );
|
|
|
|
expect( bazSection.getElementsByClassName( toc.TOGGLE_CLASS ).length ).toEqual( 0 );
|
|
|
|
expect( quxSection.getElementsByClassName( toc.TOGGLE_CLASS ).length ).toEqual( 0 );
|
|
|
|
expect( quuxSection.getElementsByClassName( toc.TOGGLE_CLASS ).length ).toEqual( 0 );
|
|
|
|
} );
|
|
|
|
} );
|
|
|
|
|
2022-02-18 00:19:50 +00:00
|
|
|
describe( 'binds event listeners', () => {
|
|
|
|
test( 'for onHeadingClick', () => {
|
|
|
|
const toc = mount();
|
2022-04-05 20:38:00 +00:00
|
|
|
const heading = /** @type {HTMLElement} */ ( document.querySelector( `#toc-foo .${toc.LINK_CLASS}` ) );
|
2022-02-18 00:19:50 +00:00
|
|
|
heading.click();
|
|
|
|
|
|
|
|
expect( onToggleClick ).not.toBeCalled();
|
|
|
|
expect( onHeadingClick ).toBeCalled();
|
|
|
|
} );
|
|
|
|
test( 'for onToggleClick', () => {
|
|
|
|
const toc = mount();
|
2022-04-05 20:38:00 +00:00
|
|
|
const toggle = /** @type {HTMLElement} */ ( document.querySelector( `#toc-bar .${toc.TOGGLE_CLASS}` ) );
|
2022-02-18 00:19:50 +00:00
|
|
|
toggle.click();
|
|
|
|
|
|
|
|
expect( onHeadingClick ).not.toBeCalled();
|
|
|
|
expect( onToggleClick ).toBeCalled();
|
|
|
|
} );
|
|
|
|
} );
|
2022-02-15 22:43:04 +00:00
|
|
|
|
2022-03-29 20:51:45 +00:00
|
|
|
describe( 'applies correct classes', () => {
|
|
|
|
test( 'when changing active sections', () => {
|
2022-02-18 00:19:50 +00:00
|
|
|
const toc = mount( { 'vector-is-collapse-sections-enabled': true } );
|
2022-09-20 21:32:53 +00:00
|
|
|
let activeSections;
|
|
|
|
let activeTopSections;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {string} id
|
|
|
|
* @param {HTMLElement} activeSection
|
|
|
|
* @param {HTMLElement} activeTopSection
|
|
|
|
*/
|
|
|
|
function testActiveClasses( id, activeSection, activeTopSection ) {
|
|
|
|
toc.changeActiveSection( id );
|
|
|
|
activeSections = container.querySelectorAll( `.${toc.ACTIVE_SECTION_CLASS}` );
|
|
|
|
activeTopSections = container.querySelectorAll( `.${toc.ACTIVE_TOP_SECTION_CLASS}` );
|
|
|
|
expect( activeSections.length ).toEqual( 1 );
|
|
|
|
expect( activeTopSections.length ).toEqual( 1 );
|
|
|
|
expect( activeSections[ 0 ] ).toEqual( activeSection );
|
|
|
|
expect( activeTopSections[ 0 ] ).toEqual( activeTopSection );
|
|
|
|
}
|
|
|
|
|
|
|
|
testActiveClasses( 'toc-foo', fooSection, fooSection );
|
|
|
|
testActiveClasses( 'toc-bar', barSection, barSection );
|
|
|
|
testActiveClasses( 'toc-baz', bazSection, barSection );
|
|
|
|
testActiveClasses( 'toc-qux', quxSection, barSection );
|
|
|
|
testActiveClasses( 'toc-quux', quuxSection, quuxSection );
|
2022-02-18 00:19:50 +00:00
|
|
|
} );
|
|
|
|
|
2022-03-29 20:51:45 +00:00
|
|
|
test( 'when expanding sections', () => {
|
2022-02-18 00:19:50 +00:00
|
|
|
const toc = mount();
|
2022-04-05 20:38:00 +00:00
|
|
|
toc.expandSection( 'toc-bar' );
|
2022-02-18 00:19:50 +00:00
|
|
|
expect( barSection.classList.contains( toc.EXPANDED_SECTION_CLASS ) ).toEqual( true );
|
|
|
|
} );
|
|
|
|
|
2022-03-29 20:51:45 +00:00
|
|
|
test( 'when toggling sections', () => {
|
2022-02-18 00:19:50 +00:00
|
|
|
const toc = mount();
|
2022-03-29 20:51:45 +00:00
|
|
|
toc.toggleExpandSection( 'toc-bar' );
|
|
|
|
expect( barSection.classList.contains( toc.EXPANDED_SECTION_CLASS ) ).toEqual( false );
|
|
|
|
toc.toggleExpandSection( 'toc-bar' );
|
|
|
|
expect( barSection.classList.contains( toc.EXPANDED_SECTION_CLASS ) ).toEqual( true );
|
2022-02-18 00:19:50 +00:00
|
|
|
} );
|
2022-02-15 22:43:04 +00:00
|
|
|
} );
|
|
|
|
|
2022-03-29 20:51:45 +00:00
|
|
|
describe( 'applies the correct aria attributes', () => {
|
|
|
|
test( 'when initialized', () => {
|
2022-08-16 21:09:02 +00:00
|
|
|
const spy = jest.spyOn( mw, 'hook' );
|
2022-03-29 20:51:45 +00:00
|
|
|
const toc = mount();
|
|
|
|
const toggleButton = /** @type {HTMLElement} */ ( barSection.querySelector( `.${toc.TOGGLE_CLASS}` ) );
|
|
|
|
|
|
|
|
expect( toggleButton.getAttribute( 'aria-expanded' ) ).toEqual( 'true' );
|
2022-08-16 21:09:02 +00:00
|
|
|
expect( spy ).toBeCalledWith( 'wikipage.tableOfContents' );
|
2022-02-18 00:19:50 +00:00
|
|
|
} );
|
|
|
|
|
2022-03-29 20:51:45 +00:00
|
|
|
test( 'when expanding sections', () => {
|
|
|
|
const toc = mount();
|
|
|
|
const toggleButton = /** @type {HTMLElement} */ ( barSection.querySelector( `.${toc.TOGGLE_CLASS}` ) );
|
2022-02-18 00:19:50 +00:00
|
|
|
|
2022-04-05 20:38:00 +00:00
|
|
|
toc.expandSection( 'toc-bar' );
|
2022-03-29 20:51:45 +00:00
|
|
|
expect( toggleButton.getAttribute( 'aria-expanded' ) ).toEqual( 'true' );
|
2022-02-18 00:19:50 +00:00
|
|
|
} );
|
|
|
|
|
2022-03-29 20:51:45 +00:00
|
|
|
test( 'when toggling sections', () => {
|
|
|
|
const toc = mount();
|
|
|
|
const toggleButton = /** @type {HTMLElement} */ ( barSection.querySelector( `.${toc.TOGGLE_CLASS}` ) );
|
2022-02-18 00:19:50 +00:00
|
|
|
|
2022-03-29 20:51:45 +00:00
|
|
|
toc.toggleExpandSection( 'toc-bar' );
|
|
|
|
expect( toggleButton.getAttribute( 'aria-expanded' ) ).toEqual( 'false' );
|
2022-02-18 00:19:50 +00:00
|
|
|
|
2022-03-29 20:51:45 +00:00
|
|
|
toc.toggleExpandSection( 'toc-bar' );
|
|
|
|
expect( toggleButton.getAttribute( 'aria-expanded' ) ).toEqual( 'true' );
|
|
|
|
} );
|
2022-02-15 22:43:04 +00:00
|
|
|
} );
|
2022-09-14 03:17:27 +00:00
|
|
|
|
|
|
|
describe( 're-rendering', () => {
|
|
|
|
const mockMwHook = () => {
|
|
|
|
/** @type {Function} */
|
|
|
|
let callback;
|
|
|
|
// @ts-ignore
|
|
|
|
jest.spyOn( mw, 'hook' ).mockImplementation( () => {
|
|
|
|
|
|
|
|
return {
|
|
|
|
add: function ( fn ) {
|
|
|
|
callback = fn;
|
|
|
|
|
|
|
|
return this;
|
|
|
|
},
|
|
|
|
fire: ( data ) => {
|
|
|
|
if ( callback ) {
|
|
|
|
callback( data );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
} );
|
|
|
|
};
|
|
|
|
|
|
|
|
afterEach( () => {
|
|
|
|
jest.restoreAllMocks();
|
|
|
|
} );
|
|
|
|
|
|
|
|
test( 'listens to wikipage.tableOfContents hook when mounted', () => {
|
|
|
|
const spy = jest.spyOn( mw, 'hook' );
|
|
|
|
mount();
|
|
|
|
expect( spy ).toHaveBeenCalledWith( 'wikipage.tableOfContents' );
|
|
|
|
} );
|
|
|
|
|
|
|
|
test( 're-renders toc when wikipage.tableOfContents hook is fired with empty sections', () => {
|
|
|
|
mockMwHook();
|
|
|
|
mount();
|
|
|
|
|
|
|
|
mw.hook( 'wikipage.tableOfContents' ).fire( [] );
|
|
|
|
|
|
|
|
expect( document.body.innerHTML ).toMatchSnapshot();
|
|
|
|
} );
|
|
|
|
|
|
|
|
test( 're-renders toc when wikipage.tableOfContents hook is fired with sections', async () => {
|
|
|
|
mockMwHook();
|
|
|
|
// @ts-ignore
|
|
|
|
// eslint-disable-next-line compat/compat
|
|
|
|
jest.spyOn( mw.loader, 'using' ).mockImplementation( () => Promise.resolve() );
|
|
|
|
// @ts-ignore
|
|
|
|
mw.template.getCompiler = () => {};
|
|
|
|
jest.spyOn( mw, 'message' ).mockImplementation( ( msg ) => {
|
|
|
|
const msgFactory = ( /** @type {string} */ text ) => {
|
|
|
|
return {
|
|
|
|
parse: () => '',
|
|
|
|
plain: () => '',
|
|
|
|
escaped: () => '',
|
|
|
|
exists: () => true,
|
|
|
|
text: () => text
|
|
|
|
};
|
|
|
|
};
|
|
|
|
switch ( msg ) {
|
|
|
|
case 'vector-toc-beginning':
|
|
|
|
return msgFactory( 'Beginning' );
|
|
|
|
default:
|
|
|
|
return msgFactory( '' );
|
|
|
|
}
|
|
|
|
|
|
|
|
} );
|
|
|
|
// @ts-ignore
|
|
|
|
jest.spyOn( mw.template, 'getCompiler' ).mockImplementation( () => {
|
|
|
|
return {
|
|
|
|
compile: () => {
|
|
|
|
return {
|
|
|
|
render: ( /** @type {Object} */ data ) => {
|
|
|
|
return {
|
|
|
|
html: () => {
|
|
|
|
return render( data );
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
};
|
|
|
|
} );
|
|
|
|
|
|
|
|
const toc = mount();
|
|
|
|
|
|
|
|
const toggleButton = /** @type {HTMLElement} */ ( barSection.querySelector( `.${toc.TOGGLE_CLASS}` ) );
|
|
|
|
// Collapse section.
|
|
|
|
toc.toggleExpandSection( 'toc-bar' );
|
|
|
|
expect( toggleButton.getAttribute( 'aria-expanded' ) ).toEqual( 'false' );
|
|
|
|
|
|
|
|
// wikipage.tableOfContents includes the nested sections in the top level
|
|
|
|
// of the array.
|
|
|
|
mw.hook( 'wikipage.tableOfContents' ).fire( [
|
|
|
|
// foo
|
|
|
|
SECTIONS[ 0 ],
|
|
|
|
// bar
|
|
|
|
SECTIONS[ 1 ],
|
|
|
|
// baz
|
|
|
|
// @ts-ignore
|
|
|
|
SECTIONS[ 1 ][ 'array-sections' ][ 0 ],
|
|
|
|
// qux
|
|
|
|
// @ts-ignore
|
|
|
|
SECTIONS[ 1 ][ 'array-sections' ][ 0 ][ 'array-sections' ][ 0 ],
|
|
|
|
// quux
|
|
|
|
SECTIONS[ 2 ],
|
|
|
|
// Add new section to see how the re-render performs.
|
|
|
|
{
|
|
|
|
toclevel: 1,
|
|
|
|
number: '4',
|
|
|
|
line: 'bat',
|
|
|
|
anchor: 'bat',
|
|
|
|
'is-top-level-section': true,
|
|
|
|
'is-parent-section': false,
|
|
|
|
'array-sections': null
|
|
|
|
}
|
|
|
|
] );
|
|
|
|
|
|
|
|
// Wait until the mw.loader.using promise has resolved so that we can
|
|
|
|
// check the DOM after it has been updated.
|
|
|
|
// eslint-disable-next-line compat/compat
|
|
|
|
await Promise.resolve();
|
|
|
|
|
|
|
|
const newToggleButton = /** @type {HTMLElement} */ ( document.querySelector( `#toc-bar .${toc.TOGGLE_CLASS}` ) );
|
|
|
|
expect( newToggleButton ).not.toBeNull();
|
|
|
|
// Check that the sections render in their expanded form.
|
|
|
|
expect( newToggleButton.getAttribute( 'aria-expanded' ) ).toEqual( 'true' );
|
|
|
|
|
|
|
|
// Verify newly rendered TOC html matches the expected html.
|
|
|
|
expect( document.body.innerHTML ).toMatchSnapshot();
|
|
|
|
} );
|
|
|
|
} );
|
2022-02-15 22:43:04 +00:00
|
|
|
} );
|