@@ -53,7 +53,7 @@ describe("pat-inject", function () {
5353
5454 describe ( "The loading-class argument" , function ( ) {
5555 it ( "has a default value of 'injecting' and gets added to the target while content is still loading" , async function ( ) {
56- jest . spyOn ( $ , "ajax" ) . mockImplementation ( ( ) => deferred ) ;
56+ const spy_ajax = jest . spyOn ( $ , "ajax" ) . mockImplementation ( ( ) => deferred ) ;
5757 var $a = $ ( '<a class="pat-inject" href="test.html#someid">link</a>' ) ;
5858 var $div = $ ( '<div id="someid" />' ) ;
5959 $ ( "#lab" ) . empty ( ) . append ( $a ) . append ( $div ) ;
@@ -73,10 +73,12 @@ describe("pat-inject", function () {
7373 await utils . timeout ( 1 ) ; // wait a tick for async to settle.
7474 expect ( $div . hasClass ( "injecting" ) ) . toBeFalsy ( ) ;
7575 expect ( callback ) . toHaveBeenCalled ( ) ;
76+
77+ spy_ajax . mockRestore ( ) ;
7678 } ) ;
7779
7880 it ( "can be set to another string value which then gets added to the target while content is still loading'" , async function ( ) {
79- jest . spyOn ( $ , "ajax" ) . mockImplementation ( ( ) => deferred ) ;
81+ const spy_ajax = jest . spyOn ( $ , "ajax" ) . mockImplementation ( ( ) => deferred ) ;
8082 var $a = $ (
8183 '<a class="pat-inject" data-pat-inject="loading-class: other-class;" href="test.html#someid">link</a>'
8284 ) ;
@@ -94,10 +96,12 @@ describe("pat-inject", function () {
9496 answer ( "<html><body>" + '<div id="someid">repl</div>' + "</body></html>" ) ;
9597 await utils . timeout ( 1 ) ; // wait a tick for async to settle.
9698 expect ( $div . hasClass ( "other-class" ) ) . toBeFalsy ( ) ;
99+
100+ spy_ajax . mockRestore ( ) ;
97101 } ) ;
98102
99103 it ( "can be set to an empty string value so that nothing gets added to the target while content is still loading'" , function ( ) {
100- jest . spyOn ( $ , "ajax" ) ;
104+ const spy_ajax = jest . spyOn ( $ , "ajax" ) ;
101105 var $a = $ (
102106 '<a class="pat-inject" data-pat-inject="loading-class: ;" href="test.html#someid">link</a>'
103107 ) ;
@@ -108,6 +112,8 @@ describe("pat-inject", function () {
108112 var cfgs = pattern . extractConfig ( $a , { } ) ;
109113 expect ( cfgs [ 0 ] . loadingClass ) . toBe ( "" ) ;
110114 expect ( $div [ 0 ] . className ) . toBe ( "" ) ;
115+
116+ spy_ajax . mockRestore ( ) ;
111117 } ) ;
112118 } ) ;
113119
@@ -116,7 +122,7 @@ describe("pat-inject", function () {
116122 var $a = $ ( '<a class="pat-inject" href="test.html#someid">link</a>' ) ;
117123 var $div = $ ( '<div id="someid" />' ) ;
118124 $ ( "#lab" ) . empty ( ) . append ( $a ) . append ( $div ) ;
119- jest . spyOn ( pattern , "onTrigger" ) ;
125+ const spy_onTrigger = jest . spyOn ( pattern , "onTrigger" ) ;
120126 var spy_confirm = jest
121127 . spyOn ( window , "confirm" )
122128 . mockImplementation ( ( ) => false ) ;
@@ -133,6 +139,9 @@ describe("pat-inject", function () {
133139 $div . addClass ( "is-dirty" ) ;
134140 $a . trigger ( "click" ) ;
135141 expect ( spy_confirm ) . toHaveBeenCalled ( ) ;
142+
143+ spy_onTrigger . mockRestore ( ) ;
144+ spy_confirm . mockRestore ( ) ;
136145 } ) ;
137146
138147 it ( "can be set to 'never' to never ask for confirmation" , function ( ) {
@@ -155,6 +164,9 @@ describe("pat-inject", function () {
155164 $a . trigger ( "click" ) ;
156165 expect ( spy_confirm ) . not . toHaveBeenCalled ( ) ;
157166 expect ( spy_onTrigger ) . toHaveBeenCalled ( ) ;
167+
168+ spy_onTrigger . mockRestore ( ) ;
169+ spy_confirm . mockRestore ( ) ;
158170 } ) ;
159171
160172 it ( "can be set to 'always' to always ask for confirmation before injecting" , function ( ) {
@@ -177,6 +189,9 @@ describe("pat-inject", function () {
177189 $a . trigger ( "click" ) ;
178190 expect ( spy_onTrigger ) . toHaveBeenCalled ( ) ;
179191 expect ( spy_confirm ) . toHaveBeenCalled ( ) ;
192+
193+ spy_onTrigger . mockRestore ( ) ;
194+ spy_confirm . mockRestore ( ) ;
180195 } ) ;
181196
182197 it ( "can be set to 'form-data' to ask for confirmation before injecting over form fields changed by the user" , function ( ) {
@@ -205,6 +220,9 @@ describe("pat-inject", function () {
205220 $a . trigger ( "click" ) ;
206221 expect ( window . confirm . mock . calls . length ) . toBe ( 1 ) ;
207222 expect ( spy_onTrigger ) . toHaveBeenCalled ( ) ;
223+
224+ spy_onTrigger . mockRestore ( ) ;
225+ spy_confirm . mockRestore ( ) ;
208226 } ) ;
209227
210228 describe ( "The confirm-message argument" , function ( ) {
@@ -214,8 +232,8 @@ describe("pat-inject", function () {
214232 ) ;
215233 var $div = $ ( '<div id="someid" />' ) ;
216234 $ ( "#lab" ) . empty ( ) . append ( $a ) . append ( $div ) ;
217- jest . spyOn ( pattern , "onTrigger" ) ;
218- var spy_confirm = jest
235+ const spy_onTrigger = jest . spyOn ( pattern , "onTrigger" ) ;
236+ const spy_confirm = jest
219237 . spyOn ( window , "confirm" )
220238 . mockImplementation ( ( ) => false ) ;
221239
@@ -228,6 +246,9 @@ describe("pat-inject", function () {
228246 $a . trigger ( "click" ) ;
229247 expect ( spy_confirm ) . toHaveBeenCalled ( ) ;
230248 expect ( spy_confirm ) . toHaveBeenCalledWith ( "Hello world" ) ;
249+
250+ spy_onTrigger . mockRestore ( ) ;
251+ spy_confirm . mockRestore ( ) ;
231252 } ) ;
232253 } ) ;
233254 } ) ;
@@ -260,6 +281,8 @@ describe("pat-inject", function () {
260281 "<a>This is a test</a>"
261282 ) ;
262283 expect ( spy_rebaseURL ) . not . toHaveBeenCalled ( ) ;
284+
285+ spy_rebaseURL . mockRestore ( ) ;
263286 } ) ;
264287
265288 it ( "Element with link attribute" , function ( ) {
@@ -500,7 +523,7 @@ describe("pat-inject", function () {
500523 beforeEach ( function ( ) { } ) ;
501524
502525 it ( "The pat-inject-success get triggered after successful injection" , async function ( ) {
503- jest . spyOn ( $ , "ajax" ) . mockImplementation ( ( ) => deferred ) ;
526+ const spy_ajax = jest . spyOn ( $ , "ajax" ) . mockImplementation ( ( ) => deferred ) ;
504527 document . body . innerHTML = `
505528 <a class="pat-inject" href="test.html">link</a>
506529 ` ;
@@ -512,11 +535,15 @@ describe("pat-inject", function () {
512535 answer ( "<html><body><div></div></body></html>" ) ;
513536 await utils . timeout ( 1 ) ; // wait a tick for async to settle.
514537 expect ( callback ) . toHaveBeenCalled ( ) ;
538+
539+ spy_ajax . mockRestore ( ) ;
515540 } ) ;
516541
517542 describe ( "The patterns-injected event" , function ( ) {
518543 it ( "gets triggered after injection has finished'" , async function ( ) {
519- jest . spyOn ( $ , "ajax" ) . mockImplementation ( ( ) => deferred ) ;
544+ const spy_ajax = jest
545+ . spyOn ( $ , "ajax" )
546+ . mockImplementation ( ( ) => deferred ) ;
520547 var $a = $ ( '<a class="pat-inject" href="test.html#someid">link</a>' ) ;
521548 var $div = $ ( '<div id="someid" />' ) ;
522549 $ ( "#lab" ) . empty ( ) . append ( $a ) . append ( $div ) ;
@@ -529,19 +556,26 @@ describe("pat-inject", function () {
529556 ) ;
530557 await utils . timeout ( 1 ) ; // wait a tick for async to settle.
531558 expect ( callback ) . toHaveBeenCalled ( ) ;
559+
560+ spy_ajax . mockRestore ( ) ;
532561 } ) ;
533562 } ) ;
534563
535564 describe ( "Injection on an anchor element" , function ( ) {
536565 var $a , $div ;
566+ let spy_ajax ;
537567
538568 beforeEach ( function ( ) {
539- jest . spyOn ( $ , "ajax" ) . mockImplementation ( ( ) => deferred ) ;
569+ spy_ajax = jest . spyOn ( $ , "ajax" ) . mockImplementation ( ( ) => deferred ) ;
540570 $a = $ ( '<a class="pat-inject" href="test.html#someid">link</a>' ) ;
541571 $div = $ ( '<div id="someid" />' ) ;
542572 $ ( "#lab" ) . append ( $a ) . append ( $div ) ;
543573 } ) ;
544574
575+ afterEach ( function ( ) {
576+ spy_ajax . mockRestore ( ) ;
577+ } ) ;
578+
545579 it ( "fetches url on click" , function ( ) {
546580 pattern . init ( $a ) ;
547581 $a . trigger ( "click" ) ;
@@ -591,14 +625,18 @@ describe("pat-inject", function () {
591625 // It is only needed here, because this is the only test case that makes two ajax() calls.
592626 deferred = new $ . Deferred ( ) ;
593627 $ . ajax . mockRestore ( ) ;
594- jest . spyOn ( $ , "ajax" ) . mockImplementation ( ( ) => deferred ) ;
628+ const spy_ajax2 = jest
629+ . spyOn ( $ , "ajax" )
630+ . mockImplementation ( ( ) => deferred ) ;
595631
596632 $a . trigger ( "click" ) ;
597633 answer (
598634 '<html><body><div id="someid">new replacement</div></body></html>'
599635 ) ;
600636 await utils . timeout ( 1 ) ; // wait a tick for async to settle.
601637 expect ( $div . html ( ) ) . toBe ( "new replacement" ) ;
638+
639+ spy_ajax2 . mockRestore ( ) ;
602640 } ) ;
603641
604642 it ( "takes multiple source-target pairs" , async function ( ) {
@@ -708,14 +746,19 @@ describe("pat-inject", function () {
708746
709747 describe ( "inject on forms" , function ( ) {
710748 var $form , $div ;
749+ let spy_ajax ;
711750
712751 beforeEach ( function ( ) {
713- jest . spyOn ( $ , "ajax" ) . mockImplementation ( ( ) => deferred ) ;
752+ spy_ajax = jest . spyOn ( $ , "ajax" ) . mockImplementation ( ( ) => deferred ) ;
714753 $form = $ ( '<form class="pat-inject" action="test.html#someid" />' ) ;
715754 $div = $ ( '<div id="someid" />' ) ;
716755 $ ( "#lab" ) . append ( $form ) . append ( $div ) ;
717756 } ) ;
718757
758+ afterEach ( function ( ) {
759+ spy_ajax . mockRestore ( ) ;
760+ } ) ;
761+
719762 it ( "trigger injection on submit" , async function ( ) {
720763 pattern . init ( $form ) ;
721764 $form . trigger ( "submit" ) ;
@@ -1040,9 +1083,10 @@ describe("pat-inject", function () {
10401083
10411084 describe ( "Error handling" , ( ) => {
10421085 let $a ;
1086+ let spy_ajax ;
10431087
10441088 beforeEach ( ( ) => {
1045- jest . spyOn ( $ , "ajax" ) . mockImplementation ( ( ) => deferred ) ;
1089+ spy_ajax = jest . spyOn ( $ , "ajax" ) . mockImplementation ( ( ) => deferred ) ;
10461090 document . body . innerhtml = `
10471091 <div id="lab">
10481092 <a class="pat-inject" href="test.html#someid">link</a>
@@ -1055,6 +1099,7 @@ describe("pat-inject", function () {
10551099 afterEach ( ( ) => {
10561100 document . head . innerHTML = "" ;
10571101 document . body . innerHTML = "" ;
1102+ spy_ajax . mockRestore ( ) ;
10581103 } ) ;
10591104
10601105 it ( "Adds on error a data-error-message to body in standard case." , async ( ) => {
@@ -1337,7 +1382,7 @@ describe("pat-inject", function () {
13371382 // becomes visible.
13381383 // Instead we test the setup, loading, unregistration, etc.
13391384
1340- jest . spyOn ( $ , "ajax" ) . mockImplementation ( ( ) => deferred ) ;
1385+ const spy_ajax = jest . spyOn ( $ , "ajax" ) . mockImplementation ( ( ) => deferred ) ;
13411386 answer ( "<html><body>OK</body></html>" ) ;
13421387
13431388 document . body . innerHTML = `
@@ -1382,10 +1427,12 @@ describe("pat-inject", function () {
13821427 expect ( observer . _cnt ) . toBe ( 3 ) ;
13831428 await utils . timeout ( 10 ) ; // wait for delay-time
13841429 expect ( el . textContent ) . toBe ( "NOT" ) ;
1430+
1431+ spy_ajax . mockRestore ( ) ;
13851432 } ) ;
13861433
13871434 it ( "Test trigger: autoload-visible, default delay time" , async ( ) => {
1388- jest . spyOn ( $ , "ajax" ) . mockImplementation ( ( ) => deferred ) ;
1435+ const spy_ajax = jest . spyOn ( $ , "ajax" ) . mockImplementation ( ( ) => deferred ) ;
13891436 answer ( "<html><body>OK</body></html>" ) ;
13901437
13911438 document . body . innerHTML = `
@@ -1412,6 +1459,8 @@ describe("pat-inject", function () {
14121459 expect ( el . textContent ) . toBe ( "test" ) ; // delay time not yet passed
14131460 await utils . timeout ( 100 ) ; // delay time passed, default is 200ms
14141461 expect ( el . textContent ) . toBe ( "OK" ) ;
1462+
1463+ spy_ajax . mockRestore ( ) ;
14151464 } ) ;
14161465 } ) ;
14171466
0 commit comments