Debian Codegraph @

skins/Vector/tests/jest/stickyHeader.test.js

raw symbols SCIP
1 const mustache = require( 'mustache' );
2 const fs = require( 'fs' );
3 const stickyHeaderTemplate = fs.readFileSync( 'includes/templates/StickyHeader.mustache', 'utf8' );
4 const buttonTemplate = fs.readFileSync( 'includes/templates/Button.mustache', 'utf8' );
5 const iconTemplate = fs.readFileSync( 'includes/templates/Icon.mustache', 'utf8' );
6 const sticky = require( '../../resources/skins.vector.js/stickyHeader.js' );
7 const { userLinksHTML, dropdownPartials } = require( './userLinksData.js' );
8
9 const defaultButtonsTemplateData = [ {
10 href: '#',
11 id: 'ca-talk-sticky-header',
12 icon: 'speechBubbles',
13 class: 'cdx-button cdx-button--weight-quiet cdx-button--icon-only sticky-header-icon',
14 'array-attributes': [ {
15 key: 'data-event-name',
16 value: 'talk-sticky-header'
17 }, {
18 key: 'tabindex',
19 value: '-1'
20 } ]
21 }, {
22 href: '#',
23 id: 'ca-history-sticky-header',
24 icon: 'history',
25 class: 'cdx-button cdx-button--weight-quiet cdx-button--icon-only sticky-header-icon',
26 'array-attributes': [ {
27 key: 'data-event-name',
28 value: 'history-sticky-header'
29 }, {
30 key: 'tabindex',
31 value: '-1'
32 } ]
33 }, {
34 href: '#',
35 id: 'ca-watchstar-sticky-header',
36 icon: 'star',
37 class: 'cdx-button cdx-button--weight-quiet cdx-button--icon-only sticky-header-icon mw-watchlink',
38 'array-attributes': [ {
39 key: 'data-event-name',
40 value: 'watch-sticky-header'
41 }, {
42 key: 'tabindex',
43 value: '-1'
44 } ]
45 } ];
46
47 const editButtonsTemplateData = [ {
48 href: '#',
49 id: 'ca-ve-edit-sticky-header',
50 icon: 'edit',
51 class: 'cdx-button cdx-button--weight-quiet cdx-button--icon-only sticky-header-icon',
52 'array-attributes': [ {
53 key: 'data-event-name',
54 value: 've-edit-sticky-header'
55 }, {
56 key: 'tabindex',
57 value: '-1'
58 } ]
59 }, {
60 href: '#',
61 id: 'ca-edit-sticky-header',
62 icon: 'wikiText',
63 class: 'cdx-button cdx-button--weight-quiet cdx-button--icon-only sticky-header-icon',
64 'array-attributes': [ {
65 key: 'data-event-name',
66 value: 'wikitext-edit-sticky-header'
67 }, {
68 key: 'tabindex',
69 value: '-1'
70 } ]
71 }, {
72 href: '#',
73 id: 'ca-viewsource-sticky-header',
74 icon: 'star',
75 class: 'cdx-button cdx-button--weight-quiet cdx-button--icon-only sticky-header-icon',
76 'array-attributes': [ {
77 key: 'data-event-name',
78 value: 'editLock'
79 }, {
80 key: 'tabindex',
81 value: '-1'
82 } ]
83 } ];
84
85 const templateData = {
86 'data-toc': {
87 'array-sections': []
88 },
89 'data-sticky-header-toc-dropdown': {
90 id: 'vector-sticky-header-toc',
91 class: 'mw-portlet mw-portlet-sticky-header-toc vector-sticky-header-toc',
92 'html-items': '',
93 'html-vector-menu-checkbox-attributes': 'tabindex="-1"',
94 'html-vector-menu-label-attributes': 'tabindex="-1"',
95 'label-class': 'cdx-button cdx-button--weight-quiet cdx-button--icon-only'
96 },
97 'array-buttons': [ {
98 label: '0 languages',
99 id: 'p-lang-btn-sticky-header',
100 class: 'cdx-button cdx-button--weight-quiet mw-interlanguage-selector',
101 'array-attributes': [ {
102 key: 'data-event-name',
103 value: 'ui.dropdown-p-lang-btn-sticky-header'
104 }, {
105 key: 'tabindex',
106 value: '-1'
107 } ]
108 } ],
109 'data-button-start': {
110 label: 'search',
111 icon: 'search',
112 class: 'cdx-button cdx-button--weight-quiet cdx-button--icon-only vector-sticky-header-search-toggle',
113 'array-attributes': [ {
114 key: 'data-event-name',
115 value: 'ui.vector-sticky-search-form.icon'
116 }, {
117 key: 'tabindex',
118 value: '-1'
119 } ]
120 },
121 'data-search': {},
122 'array-icon-buttons': defaultButtonsTemplateData.concat( editButtonsTemplateData )
123 };
124
125 const renderedHTML = mustache.render(
126 stickyHeaderTemplate, templateData, Object.assign( {}, dropdownPartials, {
127 Button: buttonTemplate,
128 Icon: iconTemplate,
129 SearchBox: '<div> </div>' // ignore SearchBox for this test
130 } ) );
131
132 beforeEach( () => {
133 document.body.innerHTML = renderedHTML;
134 } );
135
136 test( 'Sticky header renders', () => {
137 expect( document.body.innerHTML ).toMatchSnapshot();
138 } );
139
140 describe( 'sticky header', () => {
141 test( 'prepareUserLinksDropdown removes gadgets from dropdown', async () => {
142 const menu = document.createElement( 'div' );
143 menu.innerHTML = userLinksHTML;
144 const userLinksDropdown = /** @type {Element} */ ( menu.querySelector( '#' + sticky.USER_LINKS_DROPDOWN_ID ) );
145 const newMenu = sticky.prepareUserLinksDropdown( userLinksDropdown );
146 // check classes have been updated and removed.
147 expect( userLinksDropdown.querySelectorAll( '.user-links-collapsible-item' ).length > 0 ).toBeTruthy();
148 expect( userLinksDropdown.querySelectorAll( '.mw-list-item-js' ).length > 0 ).toBeTruthy();
149 expect( newMenu.querySelectorAll( '.user-links-collapsible-item' ).length ).toBe( 0 );
150 expect( newMenu.querySelectorAll( '.mw-list-item-js' ).length ).toBe( 0 );
151 } );
152 } );