@@ -18,6 +18,8 @@ define([
18
18
19
19
var injector = new Squire ( ) ,
20
20
obj ,
21
+ _ ,
22
+ originaljQuery ,
21
23
originalGetJSON ,
22
24
originalReload ,
23
25
originalIsEmpty ,
@@ -103,20 +105,22 @@ define([
103
105
} ) ;
104
106
105
107
beforeEach ( function ( done ) {
106
- originalGetJSON = jQuery . getJSON ;
108
+ originalGetJSON = $ . getJSON ;
107
109
sectionConfig [ 'Magento_Customer/js/section-config' ] ( sectionConfigSettings ) ;
108
110
109
111
injector . require ( [
112
+ 'underscore' ,
110
113
'Magento_Customer/js/customer-data'
111
- ] , function ( Constr ) {
114
+ ] , function ( underscore , Constr ) {
115
+ _ = underscore ;
112
116
obj = Constr ;
113
117
done ( ) ;
114
118
} ) ;
115
119
} ) ;
116
120
117
121
afterEach ( function ( ) {
118
122
try {
119
- jQuery . getJSON = originalGetJSON ;
123
+ $ . getJSON = originalGetJSON ;
120
124
clearLocalStorage ( ) ;
121
125
injector . clean ( ) ;
122
126
injector . remove ( ) ;
@@ -125,14 +129,20 @@ define([
125
129
} ) ;
126
130
127
131
describe ( '"init" method' , function ( ) {
132
+ var storageInvalidation = {
133
+ keys : function ( ) {
134
+ return [ 'section' ] ;
135
+ }
136
+ } ;
137
+
128
138
beforeEach ( function ( ) {
129
139
originalReload = obj . reload ;
130
140
originalIsEmpty = _ . isEmpty ;
131
141
132
- spyOn ( obj , 'reload' ) . and . returnValue ( true ) ;
133
-
134
142
$ . initNamespaceStorage ( 'mage-cache-storage' ) . localStorage ;
135
143
$ . initNamespaceStorage ( 'mage-cache-storage-section-invalidation' ) . localStorage ;
144
+
145
+ spyOn ( storageInvalidation , 'keys' ) . and . returnValue ( [ 'section' ] ) ;
136
146
} ) ;
137
147
138
148
afterEach ( function ( ) {
@@ -161,13 +171,15 @@ define([
161
171
162
172
it ( 'Calls "reload" method when expired sections exist' , function ( ) {
163
173
spyOn ( obj , 'getExpiredSectionNames' ) . and . returnValue ( [ 'section' ] ) ;
174
+ spyOn ( obj , 'reload' ) . and . returnValue ( true ) ;
164
175
obj . init ( ) ;
165
176
expect ( obj . reload ) . toHaveBeenCalled ( ) ;
166
177
} ) ;
167
178
168
179
it ( 'Calls "reload" method when expired sections do not exist' , function ( ) {
169
180
spyOn ( obj , 'getExpiredSectionNames' ) . and . returnValue ( [ ] ) ;
170
- _ . isEmpty = jasmine . createSpy ( '_.isEmpty' ) . and . returnValue ( false ) ;
181
+ spyOn ( obj , 'reload' ) . and . returnValue ( true ) ;
182
+ spyOn ( _ , 'isEmpty' ) . and . returnValue ( false ) ;
171
183
172
184
obj . init ( ) ;
173
185
expect ( obj . reload ) . toHaveBeenCalled ( ) ;
@@ -180,14 +192,14 @@ define([
180
192
}
181
193
} ) ;
182
194
183
- jQuery . getJSON = jasmine . createSpy ( ) . and . callFake ( function ( ) {
195
+ $ . getJSON = jasmine . createSpy ( ) . and . callFake ( function ( ) {
184
196
var deferred = $ . Deferred ( ) ;
185
197
186
198
return deferred . promise ( ) ;
187
199
} ) ;
188
200
189
201
init ( ) ;
190
- expect ( jQuery . getJSON ) . not . toHaveBeenCalled ( ) ;
202
+ expect ( $ . getJSON ) . not . toHaveBeenCalled ( ) ;
191
203
} ) ;
192
204
193
205
it ( 'Check it requests sections from the server if there are expired sections' , function ( ) {
@@ -246,6 +258,13 @@ define([
246
258
'content' : { }
247
259
}
248
260
} ) ;
261
+
262
+ $ . getJSON = jasmine . createSpy ( '$.getJSON' ) . and . callFake ( function ( ) {
263
+ var deferred = $ . Deferred ( ) ;
264
+
265
+ return deferred . promise ( ) ;
266
+ } ) ;
267
+
249
268
init ( ) ;
250
269
expect ( customerData . getExpiredSectionNames ( ) ) . toEqual ( [ 'cart' ] ) ;
251
270
} ) ;
@@ -268,6 +287,12 @@ define([
268
287
}
269
288
} ) ;
270
289
290
+ $ . getJSON = jasmine . createSpy ( '$.getJSON' ) . and . callFake ( function ( ) {
291
+ var deferred = $ . Deferred ( ) ;
292
+
293
+ return deferred . promise ( ) ;
294
+ } ) ;
295
+
271
296
init ( ) ;
272
297
expect ( customerData . getExpiredSectionNames ( ) ) . toEqual ( [ 'cart' ] ) ;
273
298
} ) ;
@@ -320,7 +345,10 @@ define([
320
345
321
346
describe ( '"reload" method' , function ( ) {
322
347
beforeEach ( function ( ) {
323
- jQuery . getJSON = jasmine . createSpy ( ) . and . callFake ( function ( ) {
348
+ originaljQuery = $ ;
349
+ $ = jQuery ;
350
+
351
+ $ . getJSON = jasmine . createSpy ( ) . and . callFake ( function ( ) {
324
352
var deferred = $ . Deferred ( ) ;
325
353
326
354
/**
@@ -339,6 +367,10 @@ define([
339
367
} ) ;
340
368
} ) ;
341
369
370
+ afterEach ( function ( ) {
371
+ $ = originaljQuery ;
372
+ } ) ;
373
+
342
374
it ( 'Should be defined' , function ( ) {
343
375
expect ( obj . hasOwnProperty ( 'reload' ) ) . toBeDefined ( ) ;
344
376
} ) ;
@@ -354,7 +386,7 @@ define([
354
386
355
387
spyOn ( sectionConfig , 'filterClientSideSections' ) . and . returnValue ( [ 'section' ] ) ;
356
388
357
- jQuery . getJSON = jasmine . createSpy ( ) . and . callFake ( function ( url , parameters ) {
389
+ $ . getJSON = jasmine . createSpy ( ) . and . callFake ( function ( url , parameters ) {
358
390
var deferred = $ . Deferred ( ) ;
359
391
360
392
/**
@@ -390,7 +422,7 @@ define([
390
422
391
423
spyOn ( sectionConfig , 'filterClientSideSections' ) . and . returnValue ( [ 'cart,customer,messages' ] ) ;
392
424
393
- jQuery . getJSON = jasmine . createSpy ( ) . and . callFake ( function ( url , parameters ) {
425
+ $ . getJSON = jasmine . createSpy ( ) . and . callFake ( function ( url , parameters ) {
394
426
var deferred = $ . Deferred ( ) ;
395
427
396
428
expect ( parameters ) . toEqual ( jasmine . objectContaining ( {
@@ -428,7 +460,7 @@ define([
428
460
it ( 'Check it returns all sections when passed wildcard string' , function ( ) {
429
461
var result ;
430
462
431
- jQuery . getJSON = jasmine . createSpy ( ) . and . callFake ( function ( url , parameters ) {
463
+ $ . getJSON = jasmine . createSpy ( ) . and . callFake ( function ( url , parameters ) {
432
464
var deferred = $ . Deferred ( ) ;
433
465
434
466
expect ( parameters ) . toEqual ( jasmine . objectContaining ( {
@@ -454,7 +486,7 @@ define([
454
486
455
487
result = obj . reload ( '*' , true ) ;
456
488
457
- expect ( jQuery . getJSON ) . toHaveBeenCalled ( ) ;
489
+ expect ( $ . getJSON ) . toHaveBeenCalled ( ) ;
458
490
expect ( result ) . toEqual ( jasmine . objectContaining ( {
459
491
responseJSON : {
460
492
cart : { } ,
0 commit comments