// @ts-nocheck const { addPortletLinkHandler } = require( '../../../resources/skins.vector.js/dropdownMenus.js' ); describe( 'addPortletLinkHandler', () => { beforeEach( () => { document.body.innerHTML = `
`; } ); afterEach( () => { jest.restoreAllMocks(); } ); test( 'Adds a span with icon class to dropdown menus', () => { //
  • element is the assumed HTML output of mw.util.addPortlet document.body.innerHTML = ` `; const mockPortletItem = document.getElementById( 'test-id' ); addPortletLinkHandler( mockPortletItem, { id: 'test-id' } ); expect( document.body.innerHTML ).toMatchSnapshot(); } ); test( 'Does not add an icon when noicon class is present', () => { //
  • element is the assumed HTML output of mw.util.addPortlet document.body.innerHTML = ` `; const mockPortletItem = document.getElementById( 'test-id' ); addPortletLinkHandler( mockPortletItem, { id: 'test-id' } ); expect( document.body.innerHTML ).toMatchSnapshot(); } ); test( 'JS portlet should be moved to more menu (#p-cactions) at narrow widths', () => { //
  • element is the assumed HTML output of mw.util.addPortlet document.body.innerHTML = `
    `; const mockPortletItem = document.getElementById( 'test-id' ); addPortletLinkHandler( mockPortletItem, { id: 'test-id' } ); expect( mockPortletItem.closest( '#p-cactions' ) ).toBeTruthy(); } ); test( 'addPortletLinkHandler only adds icon class to p-personal menu', () => { // Assumed output of mw.util.addPortletLink from core. const portletLinkItem = new DOMParser().parseFromString( `
  • foo
  • `, 'text/html' ).body.firstElementChild; // simulating addPortletLink appending to DOM from core. document .querySelector( '#p-personal .vector-menu-content-list' ) .appendChild( portletLinkItem ); // Running addPortletLink handler. addPortletLinkHandler( portletLinkItem, { id: 'foo-id' } ); // Asserting that the icon classes were added. expect( portletLinkItem.querySelectorAll( 'span.mw-ui-icon.mw-ui-icon-vector-gadget-foo-id' ) ).toHaveLength( 1 ); } ); test( 'addPortletLinkHandler does not add icons to p-views menu', () => { // Assumed output of mw.util.addPortletLink from core. const portletLinkItem = new DOMParser().parseFromString( `
  • foo
  • `, 'text/html' ).body.firstElementChild; // simulating addPortletLink appending to DOM from core. document .querySelector( '#p-views .vector-menu-content-list' ) .appendChild( portletLinkItem ); // Running addPortletLink handler. addPortletLinkHandler( portletLinkItem, { id: 'foo-id' } ); // Asserting that the icon classes were added. expect( portletLinkItem.querySelectorAll( 'span.mw-ui-icon.mw-ui-icon-vector-gadget-foo-id' ) ).toHaveLength( 0 ); } ); test( 'addPortletLinkHandler only adds one icon when an ID is specified - T327369', () => { // Assumed output of mw.util.addPortletLink from core. const portletLinkItem1 = new DOMParser().parseFromString( `
  • foo
  • `, 'text/html' ).body.firstElementChild; const portletLinkItem2 = new DOMParser().parseFromString( `
  • bar
  • `, 'text/html' ).body.firstElementChild; const portletLinkItem3 = new DOMParser().parseFromString( `
  • baz
  • `, 'text/html' ).body.firstElementChild; // simulating addPortletLink appending to DOM from core. const contentList = document.querySelector( '#p-personal .vector-menu-content-list' ); contentList.appendChild( portletLinkItem1 ); contentList.appendChild( portletLinkItem2 ); contentList.appendChild( portletLinkItem3 ); // Running addPortletLink handler. addPortletLinkHandler( portletLinkItem1, { id: null } ); addPortletLinkHandler( portletLinkItem2, { id: 'bar' } ); addPortletLinkHandler( portletLinkItem3, { id: 'baz' } ); // running portletLinkHandler on an item twice. Should be no-op. addPortletLinkHandler( portletLinkItem3, { id: 'baz' } ); // Asserting that the icon classes were added where necessary // and that only one icon class was added per item. expect( portletLinkItem1.querySelectorAll( 'span.mw-ui-icon.mw-ui-icon-vector-gadget-foo' ) ).toHaveLength( 0 ); expect( portletLinkItem2.querySelectorAll( 'span.mw-ui-icon.mw-ui-icon-vector-gadget-bar' ) ).toHaveLength( 1 ); expect( portletLinkItem3.querySelectorAll( 'span.mw-ui-icon.mw-ui-icon-vector-gadget-baz' ) ).toHaveLength( 1 ); } ); } );