| 1 | const clientPreferences = require( '../../resources/skins.vector.clientPreferences/clientPreferences.js' ); |
| 2 | |
| 3 | let cp; |
| 4 | describe( 'clientPreferences', () => { |
| 5 | beforeEach( () => { |
| 6 | document.body.innerHTML = ''; |
| 7 | cp = document.createElement( 'div' ); |
| 8 | cp.id = 'cp'; |
| 9 | document.body.appendChild( cp ); |
| 10 | mw.requestIdleCallback = ( callback ) => callback(); |
| 11 | mw.user.clientPrefs = { |
| 12 | get: jest.fn( () => '1' ) |
| 13 | }; |
| 14 | const portlet = document.createElement( 'div' ); |
| 15 | portlet.innerHTML = ` |
| 16 | <div class="vector-menu-heading">Width</div> |
| 17 | <div class="vector-menu-content"> |
| 18 | <ul class="vector-menu-content-list"></ul> |
| 19 | </div> |
| 20 | `; |
| 21 | mw.util.addPortlet = jest.fn( () => portlet ); |
| 22 | mw.util.addPortletLink = jest.fn( () => { |
| 23 | const li = document.createElement( 'li' ); |
| 24 | const a = document.createElement( 'a' ); |
| 25 | li.appendChild( a ); |
| 26 | portlet.appendChild( li ); |
| 27 | return li; |
| 28 | } ); |
| 29 | mw.message = jest.fn( ( key ) => ( { |
| 30 | text: () => `msg:${ key }`, |
| 31 | exists: () => true |
| 32 | } ) ); |
| 33 | } ); |
| 34 | |
| 35 | test( 'render empty', () => clientPreferences.render( '#cp', {} ).then( () => { |
| 36 | expect( cp.innerHTML ).toMatchSnapshot(); |
| 37 | } ) ); |
| 38 | |
| 39 | test( 'render font size', () => { |
| 40 | document.documentElement.setAttribute( 'class', 'vector-feature-custom-font-size-clientpref-2' ); |
| 41 | return clientPreferences.render( '#cp', { |
| 42 | 'vector-feature-custom-font-size': { |
| 43 | options: [ '0', '1', '2' ], |
| 44 | preferenceKey: 'vector-font-size' |
| 45 | } |
| 46 | } ).then( () => { |
| 47 | expect( cp.innerHTML ).toMatchSnapshot(); |
| 48 | } ); |
| 49 | } ); |
| 50 | |
| 51 | test( 'doesnt render exclusion notice if the msg key doesnt exist', () => { |
| 52 | mw.message = jest.fn( ( key ) => ( { |
| 53 | text: () => `msg:${ key }`, |
| 54 | exists: () => key !== 'vector-feature-limited-width-exclusion-notice' |
| 55 | } ) ); |
| 56 | document.documentElement.setAttribute( 'class', 'vector-feature-limited-width-clientpref-0' ); |
| 57 | return clientPreferences.render( '#cp', { |
| 58 | 'vector-feature-limited-width': { |
| 59 | options: [ '1', '0' ], |
| 60 | preferenceKey: 'vector-limited-width' |
| 61 | } |
| 62 | } ).then( () => { |
| 63 | expect( cp.innerHTML ).toMatchSnapshot(); |
| 64 | } ); |
| 65 | } ); |
| 66 | |
| 67 | test( 'render toggle', () => { |
| 68 | document.documentElement.setAttribute( 'class', 'expandAll-clientpref-1' ); |
| 69 | return clientPreferences.render( '#cp', { |
| 70 | expandAll: { |
| 71 | options: [ '0', '1' ], |
| 72 | preferenceKey: 'expandAll', |
| 73 | type: 'switch' |
| 74 | } |
| 75 | } ).then( () => { |
| 76 | expect( cp.innerHTML ).toMatchSnapshot(); |
| 77 | } ); |
| 78 | } ); |
| 79 | } ); |