Skip to content

Commit 8047497

Browse files
author
Greg Harvell
committed
Fixed eslint & jscs errors with customer-data.test.js, reseting methods to their original context after being mocked.
1 parent ff99146 commit 8047497

File tree

1 file changed

+64
-12
lines changed

1 file changed

+64
-12
lines changed

dev/tests/js/jasmine/tests/app/code/Magento/Customer/frontend/js/customer-data.test.js

Lines changed: 64 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
* See COPYING.txt for license details.
44
*/
55

6+
/* global _ */
67
/* eslint max-nested-callbacks: 0 */
7-
88
define([
99
'squire',
1010
'jquery',
@@ -14,6 +14,10 @@ define([
1414

1515
var injector = new Squire(),
1616
sectionConfig,
17+
originalGetJSON,
18+
originalReload,
19+
originalInitNamespaceStorage,
20+
originalEach,
1721
obj;
1822

1923
describe('Magento_Customer/js/customer-data', function () {
@@ -39,27 +43,46 @@ define([
3943

4044
describe('"init" method', function () {
4145
var storageInvalidation = {
46+
/**
47+
* Mock Keys Method
48+
* @returns array
49+
*/
4250
keys: function () {
4351
return ['section'];
4452
}
4553
},
4654
dataProvider = {
55+
/**
56+
* Mock getFromStorage Method
57+
* @returns array
58+
*/
4759
getFromStorage: function () {
4860
return ['section'];
4961
}
5062
},
5163
storage = {
64+
/**
65+
* Mock Keys Method
66+
* @returns array
67+
*/
5268
keys: function () {
5369
return ['section'];
5470
}
5571
};
5672

5773
beforeEach(function () {
58-
spyOn(obj, "reload").and.returnValue(true);
74+
originalReload = obj.reload;
75+
originalInitNamespaceStorage = $.initNamespaceStorage;
76+
spyOn(obj, 'reload').and.returnValue(true);
5977
spyOn($, 'initNamespaceStorage').and.returnValue(true);
60-
spyOn(dataProvider, "getFromStorage");
61-
spyOn(storage, "keys").and.returnValue(['section']);
62-
spyOn(storageInvalidation, "keys").and.returnValue(['section']);
78+
spyOn(dataProvider, 'getFromStorage');
79+
spyOn(storage, 'keys').and.returnValue(['section']);
80+
spyOn(storageInvalidation, 'keys').and.returnValue(['section']);
81+
});
82+
83+
afterEach(function () {
84+
obj.reload = originalReload;
85+
$.initNameSpaceStorage = originalInitNamespaceStorage;
6386
});
6487

6588
it('Should be defined', function () {
@@ -73,19 +96,19 @@ define([
7396
});
7497

7598
it('Calls "getExpiredSectionNames" method', function () {
76-
spyOn(obj, "getExpiredSectionNames").and.returnValue([]);
99+
spyOn(obj, 'getExpiredSectionNames').and.returnValue([]);
77100
obj.init();
78101
expect(obj.getExpiredSectionNames).toHaveBeenCalled();
79102
});
80103

81104
it('Calls "reload" method when expired sections exist', function () {
82-
spyOn(obj, "getExpiredSectionNames").and.returnValue(['section']);
105+
spyOn(obj, 'getExpiredSectionNames').and.returnValue(['section']);
83106
obj.init();
84107
expect(obj.reload).toHaveBeenCalled();
85108
});
86109

87110
it('Calls "reload" method when expired sections do not exist', function () {
88-
spyOn(obj, "getExpiredSectionNames").and.returnValue([]);
111+
spyOn(obj, 'getExpiredSectionNames').and.returnValue([]);
89112

90113
_.isEmpty = jasmine.createSpy().and.returnValue(false);
91114

@@ -119,6 +142,14 @@ define([
119142
});
120143

121144
describe('"set" method', function () {
145+
beforeEach(function () {
146+
originalEach = _.each;
147+
});
148+
149+
afterEach(function () {
150+
_.each = originalEach;
151+
});
152+
122153
it('Should be defined', function () {
123154
expect(obj.hasOwnProperty('set')).toBeDefined();
124155
});
@@ -134,9 +165,14 @@ define([
134165

135166
describe('"reload" method', function () {
136167
beforeEach(function () {
137-
jQuery.getJSON = jasmine.createSpy().and.callFake(function (url, parameters) {
168+
originalGetJSON = jQuery.getJSON;
169+
jQuery.getJSON = jasmine.createSpy().and.callFake(function () {
138170
var deferred = $.Deferred();
139171

172+
/**
173+
* Mock Done Method for getJSON
174+
* @returns object
175+
*/
140176
deferred.promise().done = function () {
141177
return {
142178
responseJSON: {
@@ -149,6 +185,10 @@ define([
149185
});
150186
});
151187

188+
afterEach(function () {
189+
jQuery.getJSON = originalGetJSON;
190+
});
191+
152192
it('Should be defined', function () {
153193
expect(obj.hasOwnProperty('reload')).toBeDefined();
154194
});
@@ -167,6 +207,10 @@ define([
167207
jQuery.getJSON = jasmine.createSpy().and.callFake(function (url, parameters) {
168208
var deferred = $.Deferred();
169209

210+
/**
211+
* Mock Done Method for getJSON
212+
* @returns object
213+
*/
170214
deferred.promise().done = function () {
171215
return {
172216
responseJSON: {
@@ -176,7 +220,7 @@ define([
176220
};
177221

178222
expect(parameters).toEqual(jasmine.objectContaining({
179-
"sections": "section"
223+
sections: 'section'
180224
}));
181225

182226
return deferred.promise();
@@ -200,9 +244,13 @@ define([
200244
var deferred = $.Deferred();
201245

202246
expect(parameters).toEqual(jasmine.objectContaining({
203-
"sections": "cart,customer,messages"
247+
sections: 'cart,customer,messages'
204248
}));
205249

250+
/**
251+
* Mock Done Method for getJSON
252+
* @returns object
253+
*/
206254
deferred.promise().done = function () {
207255
return {
208256
responseJSON: {
@@ -234,9 +282,13 @@ define([
234282
var deferred = $.Deferred();
235283

236284
expect(parameters).toEqual(jasmine.objectContaining({
237-
"force_new_section_timestamp": true
285+
'force_new_section_timestamp': true
238286
}));
239287

288+
/**
289+
* Mock Done Method for getJSON
290+
* @returns object
291+
*/
240292
deferred.promise().done = function () {
241293
return {
242294
responseJSON: {

0 commit comments

Comments
 (0)