Debian Codegraph @

skins/Vector/tests/jest/skins.vector.es6/main.test.js

raw symbols SCIP
1 window.matchMedia = window.matchMedia || function () {
2 return {
3 matches: false,
4 onchange: () => {}
5 };
6 };
7
8 const { test } = require( '../../../resources/skins.vector.js/setupIntersectionObservers.js' );
9 const {
10 STICKY_HEADER_EXPERIMENT_NAME
11 } = require( '../../../resources/skins.vector.js/stickyHeader.js' );
12 describe( 'main.js', () => {
13 it( 'getHeadingIntersectionHandler', () => {
14 const content = document.createElement( 'div' );
15 content.setAttribute( 'class', 'mw-body-content' );
16 content.setAttribute( 'id', 'mw-content-text' );
17 const parserOutput = document.createElement( 'div' );
18 parserOutput.setAttribute( 'class', 'mw-parser-output' );
19 content.appendChild( parserOutput );
20
21 const headingOld = document.createElement( 'h2' );
22 const headlineOld = document.createElement( 'span' );
23 headlineOld.classList.add( 'mw-headline' );
24 headlineOld.setAttribute( 'id', 'headline-old' );
25 headingOld.appendChild( headlineOld );
26 parserOutput.appendChild( headingOld );
27
28 const headingNew = document.createElement( 'div' );
29 headingNew.classList.add( 'mw-heading' );
30 const headlineNew = document.createElement( 'h2' );
31 headlineNew.setAttribute( 'id', 'headline-new' );
32 headingNew.appendChild( headlineNew );
33 parserOutput.appendChild( headingNew );
34
35 [
36 [ content, 'toc-mw-content-text' ],
37 [ headingOld, 'toc-headline-old' ],
38 [ headingNew, 'toc-headline-new' ]
39 ].forEach( ( testCase ) => {
40 const node = /** @type {HTMLElement} */ ( testCase[ 0 ] );
41 const fn = jest.fn();
42 const handler = test.getHeadingIntersectionHandler( fn );
43 handler( node );
44 expect( fn ).toHaveBeenCalledWith( testCase[ 1 ] );
45 } );
46 } );
47
48 it( 'initStickyHeaderABTests', () => {
49 const STICKY_HEADER_AB = {
50 name: STICKY_HEADER_EXPERIMENT_NAME,
51 enabled: true
52 };
53 [
54 {
55 abConfig: STICKY_HEADER_AB,
56 isEnabled: false, // sticky header unavailable
57 isUserInTreatmentBucket: false, // not in treatment bucket
58 expectedResult: {
59 showStickyHeader: false
60 }
61 },
62 {
63 abConfig: STICKY_HEADER_AB,
64 isEnabled: true, // sticky header available
65 isUserInTreatmentBucket: false, // not in treatment bucket
66 expectedResult: {
67 showStickyHeader: false
68 }
69 },
70 {
71 abConfig: STICKY_HEADER_AB,
72 isEnabled: false, // sticky header is not available
73 isUserInTreatmentBucket: true, // but the user is in the treatment bucket
74 expectedResult: {
75 showStickyHeader: false
76 }
77 },
78 {
79 abConfig: STICKY_HEADER_AB,
80 isEnabled: true,
81 isUserInTreatmentBucket: true,
82 expectedResult: {
83 showStickyHeader: true
84 }
85 }
86 ].forEach(
87 ( {
88 abConfig,
89 isEnabled,
90 isUserInTreatmentBucket,
91 expectedResult
92 } ) => {
93 document.documentElement.classList.add( 'vector-sticky-header-enabled' );
94 const result = test.initStickyHeaderABTests(
95 abConfig,
96 // isStickyHeaderFeatureAllowed
97 isEnabled,
98 ( experiment ) => ( {
99 name: experiment.name,
100 isInBucket: () => true,
101 isInSample: () => true,
102 getBucket: () => 'bucket',
103 isInTreatmentBucket: () => isUserInTreatmentBucket
104 } )
105 );
106 expect( result ).toMatchObject( expectedResult );
107 // Check that there are no side effects
108 expect(
109 document.documentElement.classList.contains( 'vector-sticky-header-enabled' )
110 ).toBe( true );
111 } );
112 } );
113 } );
114
115 const sectionObserverFn = () => ( {
116 pause: () => {},
117 resume: () => {},
118 mount: () => {},
119 unmount: () => {},
120 setElements: () => {},
121 calcIntersection: () => {}
122 } );
123
124 describe( 'Table of contents re-rendering', () => {
125 const mockMwHook = () => {
126 /** @type {Object.<string, Function>} */
127 const callback = {};
128 jest.spyOn( mw, 'hook' ).mockImplementation( ( name ) => ( {
129 add: function ( fn ) {
130 callback[ name ] = fn;
131
132 return this;
133 },
134 fire: ( data ) => {
135 if ( callback[ name ] ) {
136 callback[ name ]( data );
137 }
138 }
139 } ) );
140 };
141
142 afterEach( () => {
143 jest.restoreAllMocks();
144 } );
145
146 it( 'listens to wikipage.tableOfContents hook when mounted', () => {
147 mockMwHook();
148 const spy = jest.spyOn( mw, 'hook' );
149 const tocElement = document.createElement( 'div' );
150 const bodyContent = document.createElement( 'article' );
151 const toc = test.setupTableOfContents( tocElement, bodyContent, sectionObserverFn );
152 expect( toc ).not.toBe( null );
153 expect( spy ).toHaveBeenCalledWith( 'wikipage.tableOfContents' );
154 expect( spy ).not.toHaveBeenCalledWith( 'wikipage.tableOfContents.vector' );
155 } );
156
157 it( 'Firing wikipage.tableOfContents triggers reloadTableOfContents', async () => {
158 mockMwHook();
159 const tocElement = document.createElement( 'div' );
160 const bodyContent = document.createElement( 'article' );
161 const toc = test.setupTableOfContents( tocElement, bodyContent, sectionObserverFn );
162 if ( !toc ) {
163 // something went wrong
164 expect( true ).toBe( false );
165 return;
166 }
167 const spy = jest.spyOn( toc, 'reloadTableOfContents' ).mockImplementation( () => Promise.resolve() );
168
169 mw.hook( 'wikipage.tableOfContents' ).fire( [
170 // Add new section to see how the re-render performs.
171 {
172 toclevel: 1,
173 number: '4',
174 line: 'bat',
175 anchor: 'bat',
176 'is-top-level-section': true,
177 'is-parent-section': false,
178 'array-sections': null
179 }
180 ] );
181
182 expect( spy ).toHaveBeenCalled();
183 } );
184 } );