Debian Codegraph @

skins/Vector/tests/jest/skins.vector.js/portlets.test.js

raw symbols SCIP
1 const portlets = require( '../../../resources/skins.vector.js/portlets.js' );
2 const mustache = require( 'mustache' );
3 const fs = require( 'fs' );
4 const menuTemplate = fs.readFileSync( 'includes/templates/Menu.mustache', 'utf8' );
5 const menuContentsTemplate = fs.readFileSync( 'includes/templates/MenuContents.mustache', 'utf8' );
6 const dropdownOpen = fs.readFileSync( 'includes/templates/Dropdown/Open.mustache', 'utf8' );
7 const dropdownClose = fs.readFileSync( 'includes/templates/Dropdown/Close.mustache', 'utf8' );
8
9 describe( 'portlets', () => {
10 beforeEach( () => {
11 const marker = document.querySelector( '#vector-page-tools-dropdown' );
12 if ( marker ) {
13 marker.remove();
14 }
15 } );
16 test( 'portlets that go through the hook method should match the menu template HTML', () => {
17 const id = 'foo';
18 const label = 'label text';
19 const portletHTML = mustache.render( menuTemplate, {
20 id, class: '', label
21 }, {
22 MenuContents: menuContentsTemplate
23 } );
24
25 const element = document.createElement( 'div' );
26 element.id = id;
27 const labelElement = document.createElement( 'label' );
28 labelElement.textContent = label;
29 element.appendChild( labelElement );
30 element.appendChild( document.createElement( 'ul' ) );
31 expect( portlets.addPortletHandler( element ).outerHTML.replace( /[\s\n]/gi, '' ) )
32 .toBe( portletHTML.replace( /[\s\n]/gi, '' ) );
33 } );
34
35 test( 'portlets that go through the hook method should match the menu template HTML (dropdowns)', () => {
36 const id = 'foo';
37 const label = 'dropdown label text';
38 const marker = document.createElement( 'div' );
39 marker.id = 'vector-page-tools-dropdown';
40 document.body.appendChild( marker );
41 const dropdownHTML = mustache.render( dropdownOpen, {
42 id: `${ id }-dropdown`,
43 class: 'foo-dropdown',
44 label
45 }, {} ) + mustache.render( dropdownClose, {}, {} );
46
47 const element = document.createElement( 'div' );
48 element.id = id;
49 const labelElement = document.createElement( 'label' );
50 labelElement.textContent = label;
51 element.appendChild( labelElement );
52 element.appendChild( document.createElement( 'ul' ) );
53 document.body.appendChild( element );
54
55 const transformedPortlet = portlets.addPortletHandler( element, '#p-cactions' );
56 const dropdown = transformedPortlet.closest( '.vector-dropdown' );
57 // removing the portlet should give us an empty dropdown
58 transformedPortlet.remove();
59
60 expect( dropdown.outerHTML.replace( /[\s\n]/gi, '' ) )
61 .toBe( dropdownHTML.replace( /[\s\n]/gi, '' ) );
62 } );
63 } );