| 1 |
const portlets = require( '../../../resources/skins.vector.legacy.js/portlets.js' ); |
| 2 |
const mustache = require( 'mustache' ); |
| 3 |
const fs = require( 'fs' ); |
| 4 |
const menuTemplate = fs.readFileSync( 'includes/templates/LegacyMenu.mustache', 'utf8' ); |
| 5 |
const menuContentsTemplate = fs.readFileSync( 'includes/templates/MenuContents.mustache', 'utf8' ); |
| 6 |
|
| 7 |
describe( 'portlets', () => { |
| 8 |
test( 'portlets that go through the hook method should match the menu template HTML (non-dropdown version)', () => { |
| 9 |
const id = 'foo'; |
| 10 |
const label = 'label text'; |
| 11 |
const portletHTML = mustache.render( menuTemplate, { |
| 12 |
id, class: '', label |
| 13 |
}, { |
| 14 |
MenuContents: menuContentsTemplate |
| 15 |
} ); |
| 16 |
|
| 17 |
const element = document.createElement( 'div' ); |
| 18 |
element.id = id; |
| 19 |
if ( label ) { |
| 20 |
const labelElement = document.createElement( 'label' ); |
| 21 |
labelElement.textContent = label; |
| 22 |
element.appendChild( labelElement ); |
| 23 |
} |
| 24 |
element.appendChild( document.createElement( 'ul' ) ); |
| 25 |
|
| 26 |
const actual = portlets.addPortletHandler( element ).outerHTML.replace( /[\s\n]/gi, '' ) |
| 27 |
|
| 28 |
|
| 29 |
.replace( /role="navigation"/g, '' ); |
| 30 |
const expected = portletHTML.replace( /[\s\n]/gi, '' ) |
| 31 |
|
| 32 |
.replace( /<nav/g, '<div' ) |
| 33 |
.replace( /nav>/g, 'div>' ) |
| 34 |
|
| 35 |
|
| 36 |
.replace( /<spanclass="vector-menu-heading-label">/g, '' ) |
| 37 |
.replace( /<h3/g, '<div' ) |
| 38 |
.replace( /<\/span><\/h3>/g, '</div>' ); |
| 39 |
expect( actual ).toBe( expected ); |
| 40 |
} ); |
| 41 |
|
| 42 |
test( 'portlet dropdowns that go through the hook method should match the menu template HTML (dropdown version)', () => { |
| 43 |
const id = 'foo'; |
| 44 |
const label = 'label text'; |
| 45 |
const portletHTML = mustache.render( menuTemplate, { |
| 46 |
id, class: 'vector-menu-dropdown', label, |
| 47 |
'is-dropdown': true |
| 48 |
}, { |
| 49 |
MenuContents: menuContentsTemplate |
| 50 |
} ); |
| 51 |
|
| 52 |
const rNav = document.createElement( 'div' ); |
| 53 |
rNav.id = 'right-navigation'; |
| 54 |
const element = document.createElement( 'div' ); |
| 55 |
element.id = id; |
| 56 |
if ( label ) { |
| 57 |
const labelElement = document.createElement( 'label' ); |
| 58 |
labelElement.textContent = label; |
| 59 |
element.appendChild( labelElement ); |
| 60 |
} |
| 61 |
element.appendChild( document.createElement( 'ul' ) ); |
| 62 |
rNav.appendChild( element ); |
| 63 |
|
| 64 |
const actual = portlets.addPortletHandler( element ).outerHTML.replace( /[\s\n]/gi, '' ) |
| 65 |
|
| 66 |
|
| 67 |
.replace( /role="navigation"/g, '' ); |
| 68 |
const expected = portletHTML.replace( /[\s\n]/gi, '' ) |
| 69 |
|
| 70 |
.replace( /<nav/g, '<div' ) |
| 71 |
.replace( /nav>/g, 'div>' ) |
| 72 |
|
| 73 |
|
| 74 |
.replace( /<spanclass="vector-menu-heading-label">/g, '' ) |
| 75 |
.replace( /<label/g, '<div' ) |
| 76 |
.replace( /<\/span><\/label>/g, '</div>' ); |
| 77 |
expect( actual ).toBe( expected ); |
| 78 |
} ); |
| 79 |
} ); |