@@ -10,7 +10,7 @@ describe("pat-collapsible", function () {
1010 jest . restoreAllMocks ( ) ;
1111 } ) ;
1212
13- it ( "1- wraps the collapsible within a div.panel-content" , async function ( ) {
13+ it ( "1 - wraps the collapsible within a div.panel-content" , async function ( ) {
1414 document . body . innerHTML = `
1515 <div class="pat-collapsible">
1616 <h3>Trigger header</h3>
@@ -128,16 +128,27 @@ describe("pat-collapsible", function () {
128128 } ) ;
129129
130130 describe ( "8 - scrolling" , function ( ) {
131+ beforeEach ( function ( ) {
132+ // polyfill window.scrollTo for jsdom, which just runs but does not scroll.
133+ this . spy_scrollTo = jest
134+ . spyOn ( window , "scrollTo" )
135+ . mockImplementation ( ( ) => null ) ;
136+ } ) ;
137+
138+ afterEach ( function ( ) {
139+ this . spy_scrollTo . mockRestore ( ) ;
140+ } ) ;
141+
131142 it ( "8.1 - can scroll to itself when opened." , async function ( ) {
132143 document . body . innerHTML = `
133144 <div class="pat-collapsible closed" data-pat-collapsible="scroll-selector: self">
134145 <p>Collapsible content</p>
135146 </div>
136- ` ;
147+ ` ;
137148 const collapsible = document . querySelector ( ".pat-collapsible" ) ;
138149 const instance = new Pattern ( collapsible , { transition : "none" } ) ;
139- await events . await_pattern_init ( instance ) ;
140150 const spy_scroll = jest . spyOn ( instance , "_scroll" ) ;
151+ await events . await_pattern_init ( instance ) ;
141152
142153 instance . toggle ( ) ;
143154 await utils . timeout ( 10 ) ;
@@ -153,8 +164,8 @@ describe("pat-collapsible", function () {
153164 ` ;
154165 const collapsible = document . querySelector ( ".pat-collapsible" ) ;
155166 const instance = new Pattern ( collapsible , { transition : "none" } ) ;
156- await events . await_pattern_init ( instance ) ;
157167 const spy_scroll = jest . spyOn ( instance , "_scroll" ) ;
168+ await events . await_pattern_init ( instance ) ;
158169
159170 instance . toggle ( ) ;
160171 await utils . timeout ( 10 ) ;
@@ -182,7 +193,6 @@ describe("pat-collapsible", function () {
182193 await events . await_pattern_init ( instance2 ) ;
183194 const instance3 = new Pattern ( document . querySelector ( ".c3" ) ) ;
184195 await events . await_pattern_init ( instance3 ) ;
185- const spy_animate = jest . spyOn ( $ . fn , "animate" ) ;
186196
187197 document . querySelector ( "#open" ) . click ( ) ;
188198 await utils . timeout ( 30 ) ;
@@ -192,7 +202,7 @@ describe("pat-collapsible", function () {
192202 expect ( document . querySelector ( ".c3" ) . classList . contains ( "open" ) ) . toBeTruthy ( ) ; // prettier-ignore
193203
194204 // Other calls to _scroll resp. jQuery.animate should have been canceled.
195- expect ( spy_animate ) . toHaveBeenCalledTimes ( 1 ) ;
205+ expect ( this . spy_scrollTo ) . toHaveBeenCalledTimes ( 1 ) ;
196206 } ) ;
197207
198208 it ( "8.4 - can scroll to itself when opened with an offset." , async function ( ) {
@@ -204,13 +214,12 @@ describe("pat-collapsible", function () {
204214 const collapsible = document . querySelector ( ".pat-collapsible" ) ;
205215 const instance = new Pattern ( collapsible ) ;
206216 await events . await_pattern_init ( instance ) ;
207- const spy_animate = jest . spyOn ( $ . fn , "animate" ) ;
208217
209218 instance . toggle ( ) ;
210219 await utils . timeout ( 10 ) ;
211220
212- const arg_1 = spy_animate . mock . calls [ 0 ] [ 0 ] ;
213- expect ( arg_1 . scrollTop ) . toBe ( - 40 ) ; // the offset is substracted from the scroll position to stop BEFORE the target position.
221+ const arg_1 = this . spy_scrollTo . mock . calls [ 0 ] [ 0 ] ;
222+ expect ( arg_1 . top ) . toBe ( - 40 ) ; // the offset is substracted from the scroll position to stop BEFORE the target position.
214223 } ) ;
215224
216225 it ( "8.5 - can scroll to itself when opened with a negative offset." , async function ( ) {
@@ -222,13 +231,12 @@ describe("pat-collapsible", function () {
222231 const collapsible = document . querySelector ( ".pat-collapsible" ) ;
223232 const instance = new Pattern ( collapsible ) ;
224233 await events . await_pattern_init ( instance ) ;
225- const spy_animate = jest . spyOn ( $ . fn , "animate" ) ;
226234
227235 instance . toggle ( ) ;
228236 await utils . timeout ( 10 ) ;
229237
230- const arg_1 = spy_animate . mock . calls [ 0 ] [ 0 ] ;
231- expect ( arg_1 . scrollTop ) . toBe ( 40 ) ; // the offset is substracted from the scroll position, so a negative offset is added to the scroll position and stops AFTER the target position.
238+ const arg_1 = this . spy_scrollTo . mock . calls [ 0 ] [ 0 ] ;
239+ expect ( arg_1 . top ) . toBe ( 40 ) ; // the offset is substracted from the scroll position, so a negative offset is added to the scroll position and stops AFTER the target position.
232240 } ) ;
233241 } ) ;
234242
0 commit comments