Debian Codegraph @

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

raw symbols SCIP
1 // @ts-nocheck
2
3 const { addPortletLinkHandler } = require( '../../../resources/skins.vector.js/dropdownMenus.js' );
4
5 describe( 'addPortletLinkHandler', () => {
6 beforeEach( () => {
7 document.body.innerHTML = `
8 <div id="p-views" class="vector-menu">
9 <div class="vector-menu-content">
10 <ul class="vector-menu-content-list">
11 <li id="pt-userpage" class="mw-list-item">
12 <a href="#bar">
13 <span class="vector-icon mw-ui-icon-userAvatar mw-ui-icon-wikimedia-userAvatar"></span>
14 <span>Admin</span>
15 </a>
16 </li>
17 </ul>
18 </div>
19 </div>
20
21 <div id="p-personal" class="vector-menu">
22 <div class="vector-menu-content">
23 <ul class="vector-menu-content-list">
24 <li id="pt-userpage" class="mw-list-item">
25 <a href="#bar">
26 <span class="vector-icon mw-ui-icon-userAvatar mw-ui-icon-wikimedia-userAvatar"></span>
27 <span>Admin</span>
28 </a>
29 </li>
30 </ul>
31 </div>
32 </div>`;
33 } );
34
35 afterEach( () => {
36 jest.restoreAllMocks();
37 } );
38
39 test( 'Adds a span with icon class to menus', () => {
40
41 // <li> element is the assumed HTML output of mw.util.addPortlet
42 document.body.innerHTML = `
43 <ul class="vector-menu">
44 <li class="mw-list-item mw-list-item-js" id="test-id">
45 <a href="#test-href">
46 <span>
47 test link content
48 </span>
49 </a>
50 </li>
51 </ul>`;
52
53 const mockPortletItem = document.getElementById( 'test-id' );
54 addPortletLinkHandler( mockPortletItem, { id: 'test-id' } );
55 expect( document.body.innerHTML ).toMatchSnapshot();
56 } );
57
58 test( 'Does not add an icon when noicon class is present', () => {
59
60 // <li> element is the assumed HTML output of mw.util.addPortlet
61 document.body.innerHTML = `
62 <ul class="vector-menu">
63 <li class="mw-list-item mw-list-item-js" id="test-id">
64 <a href="#test-href">
65 <span>
66 test link content
67 </span>
68 </a>
69 </li>
70 </ul>`;
71
72 const mockPortletItem = document.getElementById( 'test-id' );
73 addPortletLinkHandler( mockPortletItem, { id: 'test-id' } );
74 expect( document.body.innerHTML ).toMatchSnapshot();
75 } );
76
77 test( 'JS portlet should be moved to more menu (#p-cactions) at narrow widths', () => {
78
79 // <li> element is the assumed HTML output of mw.util.addPortlet
80 document.body.innerHTML = `
81 <div class="mw-article-toolbar-container" style="width:1000px">
82
83 <div id="left-navigation">
84 <nav>
85 <div id="p-namespaces" style="width:1001px"></div>
86
87 <div id="p-variants"></div>
88
89 <div id="p-cactions">
90 <ul>
91 </ul>
92 </div>
93 </nav>
94 </div>
95
96 <div id="right-navigation">
97 <ul id="p-views" class="vector-menu">
98 <li class="mw-list-item mw-list-item-js" id="test-id">
99 <a href="#test-href">
100 <span>
101 test link content
102 </span>
103 </a>
104 </li>
105 </ul>
106 </div>
107 </div>`;
108
109 const mockPortletItem = document.getElementById( 'test-id' );
110 addPortletLinkHandler( mockPortletItem, { id: 'test-id' } );
111
112 expect( mockPortletItem.closest( '#p-cactions' ) ).toBeTruthy();
113 } );
114
115 test( 'addPortletLinkHandler only adds icon class to p-personal menu', () => {
116
117 // Assumed output of mw.util.addPortletLink from core.
118 const portletLinkItem = new DOMParser().parseFromString( `
119 <li id="foo-id" class="mw-list-item mw-list-item-js">
120 <a href="#foo">
121 <span>foo</span>
122 </a>
123 </li>`, 'text/html' ).body.firstElementChild;
124
125 // simulating addPortletLink appending to DOM from core.
126 document
127 .querySelector( '#p-personal .vector-menu-content-list' )
128 .appendChild( portletLinkItem );
129
130 // Running addPortletLink handler.
131 addPortletLinkHandler( portletLinkItem, { id: 'foo-id' } );
132
133 // Asserting that the icon classes were added.
134 expect( portletLinkItem.querySelectorAll( 'span.vector-icon.mw-ui-icon-vector-gadget-foo-id' ) ).toHaveLength( 1 );
135 } );
136
137 test( 'addPortletLinkHandler does not add icons to p-views menu', () => {
138
139 // Assumed output of mw.util.addPortletLink from core.
140 const portletLinkItem = new DOMParser().parseFromString( `
141 <li id="foo-id" class="mw-list-item mw-list-item-js">
142 <a href="#foo">
143 <span>foo</span>
144 </a>
145 </li>`, 'text/html' ).body.firstElementChild;
146
147 // simulating addPortletLink appending to DOM from core.
148 document
149 .querySelector( '#p-views .vector-menu-content-list' )
150 .appendChild( portletLinkItem );
151
152 // Running addPortletLink handler.
153 addPortletLinkHandler( portletLinkItem, { id: 'foo-id' } );
154
155 // Asserting that the icon classes were added.
156 expect( portletLinkItem.querySelectorAll( 'span.mw-ui-icon.mw-ui-icon-vector-gadget-foo-id' ) ).toHaveLength( 0 );
157 } );
158
159 test( 'addPortletLinkHandler only adds one icon when an ID is specified - T327369', () => {
160
161 // Assumed output of mw.util.addPortletLink from core.
162 const portletLinkItem1 = new DOMParser().parseFromString( `
163 <li class="mw-list-item mw-list-item-js">
164 <a href="#foo">
165 <span>foo</span>
166 </a>
167 </li>`, 'text/html' ).body.firstElementChild;
168
169 const portletLinkItem2 = new DOMParser().parseFromString( `
170 <li id="bar" class="mw-list-item mw-list-item-js">
171 <a href="#bar">
172 <span>bar</span>
173 </a>
174 </li>`, 'text/html' ).body.firstElementChild;
175
176 const portletLinkItem3 = new DOMParser().parseFromString( `
177 <li id="baz" class="mw-list-item mw-list-item-js">
178 <a href="#baz">
179 <span>baz</span>
180 </a>
181 </li>`, 'text/html' ).body.firstElementChild;
182
183 // simulating addPortletLink appending to DOM from core.
184 const contentList = document.querySelector( '#p-personal .vector-menu-content-list' );
185
186 contentList.appendChild( portletLinkItem1 );
187 contentList.appendChild( portletLinkItem2 );
188 contentList.appendChild( portletLinkItem3 );
189
190 // Running addPortletLink handler.
191 addPortletLinkHandler( portletLinkItem1, { id: null } );
192 addPortletLinkHandler( portletLinkItem2, { id: 'bar' } );
193 addPortletLinkHandler( portletLinkItem3, { id: 'baz' } );
194 // running portletLinkHandler on an item twice. Should be no-op.
195 addPortletLinkHandler( portletLinkItem3, { id: 'baz' } );
196
197 // Asserting that the icon classes were added where necessary
198 // and that only one icon class was added per item.
199 expect( portletLinkItem1.querySelectorAll( 'span.vector-icon.mw-ui-icon-vector-gadget-foo' ) ).toHaveLength( 0 );
200 expect( portletLinkItem2.querySelectorAll( 'span.vector-icon.mw-ui-icon-vector-gadget-bar' ) ).toHaveLength( 1 );
201 expect( portletLinkItem3.querySelectorAll( 'span.vector-icon.mw-ui-icon-vector-gadget-baz' ) ).toHaveLength( 1 );
202 } );
203 } );