2022-12-07 22:15:31 +00:00
|
|
|
jest.mock( '../../resources/skins.vector.es6/features.js' );
|
|
|
|
|
|
|
|
const features = require( '../../resources/skins.vector.es6/features.js' );
|
2022-10-19 20:43:40 +00:00
|
|
|
const mustache = require( 'mustache' );
|
|
|
|
const fs = require( 'fs' );
|
|
|
|
const pinnableHeaderTemplate = fs.readFileSync( 'includes/templates/PinnableHeader.mustache', 'utf8' );
|
2022-11-28 21:08:40 +00:00
|
|
|
const pinnableElement = require( '../../resources/skins.vector.es6/pinnableElement.js' );
|
2022-10-19 20:43:40 +00:00
|
|
|
|
2023-01-11 14:52:45 +00:00
|
|
|
/**
|
|
|
|
* Mock for matchMedia, which is not included in JSDOM.
|
|
|
|
* https://jestjs.io/docs/26.x/manual-mocks#mocking-methods-which-are-not-implemented-in-jsdom
|
|
|
|
*/
|
|
|
|
Object.defineProperty( window, 'matchMedia', {
|
|
|
|
writable: true,
|
|
|
|
value: jest.fn().mockImplementation( query => ( {
|
|
|
|
matches: false,
|
|
|
|
media: query,
|
|
|
|
onchange: null,
|
|
|
|
addListener: jest.fn(), // deprecated
|
|
|
|
removeListener: jest.fn(), // deprecated
|
|
|
|
addEventListener: jest.fn(),
|
|
|
|
removeEventListener: jest.fn(),
|
|
|
|
dispatchEvent: jest.fn()
|
|
|
|
} ) )
|
|
|
|
} );
|
|
|
|
|
2022-10-19 20:43:40 +00:00
|
|
|
const simpleData = {
|
|
|
|
'is-pinned': false,
|
|
|
|
'data-name': 'simple',
|
|
|
|
'data-pinnable-element-id': 'pinnable-element',
|
|
|
|
label: 'simple pinnable element',
|
2022-11-14 18:24:43 +00:00
|
|
|
'label-tag-name': 'div',
|
2022-10-19 20:43:40 +00:00
|
|
|
'pin-label': 'pin',
|
|
|
|
'unpin-label': 'unpin'
|
|
|
|
};
|
|
|
|
|
|
|
|
const movableData = {
|
|
|
|
'is-pinned': false,
|
|
|
|
'data-name': 'movable',
|
|
|
|
'data-pinnable-element-id': 'pinnable-element',
|
|
|
|
'data-pinned-container-id': 'pinned-container',
|
|
|
|
'data-unpinned-container-id': 'unpinned-container',
|
|
|
|
label: 'moveable pinnable element',
|
2022-11-14 18:24:43 +00:00
|
|
|
'label-tag-name': 'div',
|
2022-10-19 20:43:40 +00:00
|
|
|
'pin-label': 'pin',
|
|
|
|
'unpin-label': 'unpin'
|
|
|
|
};
|
|
|
|
|
|
|
|
// @ts-ignore
|
|
|
|
const initializeHTML = ( headerData ) => {
|
|
|
|
const pinnableHeaderHTML = mustache.render( pinnableHeaderTemplate, headerData );
|
|
|
|
const pinnableElementHTML = `<div id="pinnable-element"> ${ pinnableHeaderHTML } </div>`;
|
|
|
|
document.body.innerHTML = `
|
|
|
|
<div id="pinned-container">
|
|
|
|
${ headerData[ 'is-pinned' ] ? pinnableElementHTML : '' }
|
|
|
|
</div>
|
2023-02-01 20:31:53 +00:00
|
|
|
<div class="vector-dropdown">
|
|
|
|
<input type="checkbox" id="checkbox" class="vector-menu-checkbox">
|
|
|
|
<label for="checkbox" class="vector-menu-heading ">
|
|
|
|
<span class="vector-menu-heading-label">Dropdown</span>
|
|
|
|
</label>
|
|
|
|
<div class="vector-menu-content">
|
|
|
|
<div id="unpinned-container">
|
|
|
|
${ !headerData[ 'is-pinned' ] ? pinnableElementHTML : '' }
|
|
|
|
</div>
|
|
|
|
</div>
|
2022-10-19 20:43:40 +00:00
|
|
|
</div>
|
|
|
|
`;
|
2022-12-07 22:15:31 +00:00
|
|
|
|
|
|
|
if ( headerData[ 'data-feature-name' ] ) {
|
|
|
|
// Return early if the persistent option is enabled as features.js will
|
|
|
|
// manage the body classes instead of pinnableElement.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-10-19 20:43:40 +00:00
|
|
|
if ( headerData[ 'is-pinned' ] ) {
|
|
|
|
document.body.classList.add( `${headerData[ 'data-name' ]}-pinned` );
|
|
|
|
document.body.classList.remove( `${headerData[ 'data-name' ]}-unpinned` );
|
|
|
|
} else {
|
|
|
|
document.body.classList.remove( `${headerData[ 'data-name' ]}-pinned` );
|
|
|
|
document.body.classList.add( `${headerData[ 'data-name' ]}-unpinned` );
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
describe( 'Pinnable header', () => {
|
|
|
|
test( 'renders', () => {
|
|
|
|
initializeHTML( simpleData );
|
|
|
|
expect( document.body.innerHTML ).toMatchSnapshot();
|
|
|
|
} );
|
|
|
|
|
|
|
|
test( 'updates pinnable classes when toggle is pressed', () => {
|
|
|
|
initializeHTML( simpleData );
|
2022-11-28 21:08:40 +00:00
|
|
|
pinnableElement.initPinnableElement();
|
2022-10-19 20:43:40 +00:00
|
|
|
const pinButton = /** @type {HTMLElement} */ ( document.querySelector( '.vector-pinnable-header-pin-button' ) );
|
|
|
|
const unpinButton = /** @type {HTMLElement} */ ( document.querySelector( '.vector-pinnable-header-unpin-button' ) );
|
2022-10-20 21:32:07 +00:00
|
|
|
const header = /** @type {HTMLElement} */ ( document.querySelector( `.${simpleData[ 'data-name' ]}-pinnable-header` ) );
|
2022-10-19 20:43:40 +00:00
|
|
|
|
2022-11-28 21:08:40 +00:00
|
|
|
expect( header.classList.contains( pinnableElement.PINNED_HEADER_CLASS ) ).toBe( false );
|
2022-10-19 20:43:40 +00:00
|
|
|
expect( document.body.classList.contains( `${simpleData[ 'data-name' ]}-pinned` ) ).toBe( false );
|
2022-11-28 21:08:40 +00:00
|
|
|
expect( header.classList.contains( pinnableElement.UNPINNED_HEADER_CLASS ) ).toBe( true );
|
2022-10-19 20:43:40 +00:00
|
|
|
expect( document.body.classList.contains( `${simpleData[ 'data-name' ]}-unpinned` ) ).toBe( true );
|
|
|
|
pinButton.click();
|
2022-11-28 21:08:40 +00:00
|
|
|
expect( header.classList.contains( pinnableElement.PINNED_HEADER_CLASS ) ).toBe( true );
|
2022-10-19 20:43:40 +00:00
|
|
|
expect( document.body.classList.contains( `${simpleData[ 'data-name' ]}-pinned` ) ).toBe( true );
|
2022-11-28 21:08:40 +00:00
|
|
|
expect( header.classList.contains( pinnableElement.UNPINNED_HEADER_CLASS ) ).toBe( false );
|
2022-10-19 20:43:40 +00:00
|
|
|
expect( document.body.classList.contains( `${simpleData[ 'data-name' ]}-unpinned` ) ).toBe( false );
|
|
|
|
unpinButton.click();
|
2022-11-28 21:08:40 +00:00
|
|
|
expect( header.classList.contains( pinnableElement.PINNED_HEADER_CLASS ) ).toBe( false );
|
2022-10-19 20:43:40 +00:00
|
|
|
expect( document.body.classList.contains( `${simpleData[ 'data-name' ]}-pinned` ) ).toBe( false );
|
2022-11-28 21:08:40 +00:00
|
|
|
expect( header.classList.contains( pinnableElement.UNPINNED_HEADER_CLASS ) ).toBe( true );
|
2022-10-19 20:43:40 +00:00
|
|
|
expect( document.body.classList.contains( `${simpleData[ 'data-name' ]}-unpinned` ) ).toBe( true );
|
|
|
|
} );
|
|
|
|
|
|
|
|
test( 'doesnt move pinnable element when data attributes arent defined', () => {
|
|
|
|
initializeHTML( simpleData );
|
2022-11-28 21:08:40 +00:00
|
|
|
pinnableElement.initPinnableElement();
|
2022-10-19 20:43:40 +00:00
|
|
|
const pinButton = /** @type {HTMLElement} */ ( document.querySelector( '.vector-pinnable-header-pin-button' ) );
|
|
|
|
const unpinButton = /** @type {HTMLElement} */ ( document.querySelector( '.vector-pinnable-header-unpin-button' ) );
|
|
|
|
const pinnableElem = /** @type {HTMLElement} */ ( document.getElementById( simpleData[ 'data-pinnable-element-id' ] ) );
|
|
|
|
|
|
|
|
/* eslint-disable no-restricted-properties */
|
|
|
|
expect( pinnableElem.parentElement && pinnableElem.parentElement.id ).toBe( 'unpinned-container' );
|
|
|
|
pinButton.click();
|
|
|
|
expect( pinnableElem.parentElement && pinnableElem.parentElement.id ).toBe( 'unpinned-container' );
|
|
|
|
unpinButton.click();
|
|
|
|
expect( pinnableElem.parentElement && pinnableElem.parentElement.id ).toBe( 'unpinned-container' );
|
|
|
|
/* eslint-enable no-restricted-properties */
|
|
|
|
} );
|
|
|
|
|
|
|
|
test( 'moves pinnable element when data attributes are defined', () => {
|
|
|
|
initializeHTML( movableData );
|
2022-11-28 21:08:40 +00:00
|
|
|
pinnableElement.initPinnableElement();
|
2022-10-19 20:43:40 +00:00
|
|
|
const pinButton = /** @type {HTMLElement} */ ( document.querySelector( '.vector-pinnable-header-pin-button' ) );
|
|
|
|
const unpinButton = /** @type {HTMLElement} */ ( document.querySelector( '.vector-pinnable-header-unpin-button' ) );
|
|
|
|
const pinnableElem = /** @type {HTMLElement} */ ( document.getElementById( movableData[ 'data-pinnable-element-id' ] ) );
|
|
|
|
|
|
|
|
/* eslint-disable no-restricted-properties */
|
|
|
|
expect( pinnableElem.parentElement && pinnableElem.parentElement.id ).toBe( 'unpinned-container' );
|
|
|
|
pinButton.click();
|
|
|
|
expect( pinnableElem.parentElement && pinnableElem.parentElement.id ).toBe( 'pinned-container' );
|
|
|
|
unpinButton.click();
|
|
|
|
expect( pinnableElem.parentElement && pinnableElem.parentElement.id ).toBe( 'unpinned-container' );
|
|
|
|
/* eslint-enable no-restricted-properties */
|
|
|
|
} );
|
2022-12-07 22:15:31 +00:00
|
|
|
|
|
|
|
test( 'calls features.js when data-feature-name is set', () => {
|
|
|
|
initializeHTML( {
|
|
|
|
...simpleData,
|
|
|
|
'data-name': 'vector-page-tools',
|
|
|
|
'data-feature-name': 'page-tools-pinned'
|
|
|
|
} );
|
|
|
|
pinnableElement.initPinnableElement();
|
|
|
|
const pinButton = /** @type {HTMLElement} */ ( document.querySelector( '.vector-pinnable-header-pin-button' ) );
|
|
|
|
const unpinButton = /** @type {HTMLElement} */ ( document.querySelector( '.vector-pinnable-header-unpin-button' ) );
|
|
|
|
|
|
|
|
pinButton.click();
|
|
|
|
|
|
|
|
expect( features.toggle ).toHaveBeenCalledTimes( 1 );
|
|
|
|
expect( features.toggle ).toHaveBeenCalledWith( 'page-tools-pinned' );
|
|
|
|
|
|
|
|
// @ts-ignore
|
|
|
|
features.toggle.mockClear();
|
|
|
|
unpinButton.click();
|
|
|
|
|
|
|
|
expect( features.toggle ).toHaveBeenCalledTimes( 1 );
|
|
|
|
expect( features.toggle ).toHaveBeenCalledWith( 'page-tools-pinned' );
|
|
|
|
} );
|
|
|
|
|
|
|
|
test( 'isPinned() returns whether the element is pinned or not', () => {
|
|
|
|
initializeHTML( simpleData );
|
|
|
|
pinnableElement.initPinnableElement();
|
|
|
|
const pinButton = /** @type {HTMLElement} */ ( document.querySelector( '.vector-pinnable-header-pin-button' ) );
|
|
|
|
const unpinButton = /** @type {HTMLElement} */ ( document.querySelector( '.vector-pinnable-header-unpin-button' ) );
|
|
|
|
const header = /** @type {HTMLElement} */ ( document.querySelector( `.${simpleData[ 'data-name' ]}-pinnable-header` ) );
|
|
|
|
|
|
|
|
pinButton.click();
|
|
|
|
|
|
|
|
expect( pinnableElement.isPinned( header ) ).toBe( true );
|
|
|
|
|
|
|
|
unpinButton.click();
|
|
|
|
|
|
|
|
expect( pinnableElement.isPinned( header ) ).toBe( false );
|
|
|
|
} );
|
2023-02-01 20:31:53 +00:00
|
|
|
|
|
|
|
test( 'setFocusAfterToggle() sets focus on appropriate element after pinnableElement is toggled', () => {
|
|
|
|
initializeHTML( movableData );
|
|
|
|
pinnableElement.initPinnableElement();
|
|
|
|
const dropdownCheckbox = /** @type {HTMLElement} */ ( document.getElementById( 'checkbox' ) );
|
|
|
|
const pinButton = /** @type {HTMLElement} */ ( document.querySelector( '.vector-pinnable-header-pin-button' ) );
|
|
|
|
const unpinButton = /** @type {HTMLElement} */ ( document.querySelector( '.vector-pinnable-header-unpin-button' ) );
|
|
|
|
|
|
|
|
pinButton.click();
|
|
|
|
expect( document.activeElement ).toBe( unpinButton );
|
|
|
|
unpinButton.click();
|
|
|
|
expect( document.activeElement ).toBe( dropdownCheckbox );
|
|
|
|
} );
|
2022-10-19 20:43:40 +00:00
|
|
|
} );
|