Skip to content

Commit ff99146

Browse files
author
Greg Harvell
committed
Updated customerData jasmine test to work when whole test suite is run and fixed an issue with jquery/jquery-storageapi where object may not exist yet.
1 parent 1505a36 commit ff99146

File tree

2 files changed

+50
-38
lines changed

2 files changed

+50
-38
lines changed

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

Lines changed: 49 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -6,57 +6,57 @@
66
/* eslint max-nested-callbacks: 0 */
77

88
define([
9-
'squire'
10-
], function (Squire) {
9+
'squire',
10+
'jquery',
11+
'jquery/jquery-storageapi'
12+
], function (Squire, $) {
1113
'use strict';
1214

1315
var injector = new Squire(),
14-
originalGetJSON,
15-
storage,
16-
storageInvalidation = {},
16+
sectionConfig,
1717
obj;
1818

19-
beforeEach(function (done) {
20-
injector.require(['Magento_Customer/js/customer-data'], function (Constr) {
21-
originalGetJSON = $.getJSON;
22-
obj = Constr;
23-
done();
24-
});
25-
});
19+
describe('Magento_Customer/js/customer-data', function () {
2620

27-
afterEach(function () {
28-
try {
29-
injector.clean();
30-
injector.remove();
31-
$.getJSON = originalGetJSON;
32-
} catch (e) {
33-
}
34-
});
21+
beforeEach(function (done) {
22+
injector.require([
23+
'Magento_Customer/js/customer-data',
24+
'Magento_Customer/js/section-config'
25+
], function (Constr, sectionConfiguration) {
26+
obj = Constr;
27+
sectionConfig = sectionConfiguration;
28+
done();
29+
});
30+
});
3531

36-
describe('Magento_Customer/js/customer-data', function () {
32+
afterEach(function () {
33+
try {
34+
injector.clean();
35+
injector.remove();
36+
} catch (e) {
37+
}
38+
});
3739

3840
describe('"init" method', function () {
39-
beforeEach(function () {
40-
spyOn(obj, "reload").and.returnValue(true);
41-
42-
storageInvalidation = {
41+
var storageInvalidation = {
4342
keys: function () {
4443
return ['section'];
4544
}
46-
}
47-
48-
var dataProvider = {
49-
getFromStorage: function (sections) {
45+
},
46+
dataProvider = {
47+
getFromStorage: function () {
5048
return ['section'];
5149
}
52-
};
53-
50+
},
5451
storage = {
5552
keys: function () {
5653
return ['section'];
5754
}
5855
};
5956

57+
beforeEach(function () {
58+
spyOn(obj, "reload").and.returnValue(true);
59+
spyOn($, 'initNamespaceStorage').and.returnValue(true);
6060
spyOn(dataProvider, "getFromStorage");
6161
spyOn(storage, "keys").and.returnValue(['section']);
6262
spyOn(storageInvalidation, "keys").and.returnValue(['section']);
@@ -124,15 +124,17 @@ define([
124124
});
125125

126126
it('Does not throw before component is initialized', function () {
127+
_.each = jasmine.createSpy().and.returnValue(true);
128+
127129
expect(function () {
128-
obj.set('cart', {});
130+
obj.set();
129131
}).not.toThrow();
130132
});
131133
});
132134

133135
describe('"reload" method', function () {
134136
beforeEach(function () {
135-
$.getJSON = jasmine.createSpy().and.callFake(function (url, parameters) {
137+
jQuery.getJSON = jasmine.createSpy().and.callFake(function (url, parameters) {
136138
var deferred = $.Deferred();
137139

138140
deferred.promise().done = function () {
@@ -160,7 +162,9 @@ define([
160162
it('Returns proper sections object when passed array with a single section name', function () {
161163
var result;
162164

163-
$.getJSON = jasmine.createSpy().and.callFake(function (url, parameters) {
165+
spyOn(sectionConfig, 'filterClientSideSections').and.returnValue(['section']);
166+
167+
jQuery.getJSON = jasmine.createSpy().and.callFake(function (url, parameters) {
164168
var deferred = $.Deferred();
165169

166170
deferred.promise().done = function () {
@@ -190,7 +194,9 @@ define([
190194
it('Returns proper sections object when passed array with a multiple section names', function () {
191195
var result;
192196

193-
$.getJSON = jasmine.createSpy().and.callFake(function (url, parameters) {
197+
spyOn(sectionConfig, 'filterClientSideSections').and.returnValue(['cart,customer,messages']);
198+
199+
jQuery.getJSON = jasmine.createSpy().and.callFake(function (url, parameters) {
194200
var deferred = $.Deferred();
195201

196202
expect(parameters).toEqual(jasmine.objectContaining({
@@ -224,7 +230,7 @@ define([
224230
it('Returns all sections when passed wildcard string', function () {
225231
var result;
226232

227-
$.getJSON = jasmine.createSpy().and.callFake(function (url, parameters) {
233+
jQuery.getJSON = jasmine.createSpy().and.callFake(function (url, parameters) {
228234
var deferred = $.Deferred();
229235

230236
expect(parameters).toEqual(jasmine.objectContaining({
@@ -246,7 +252,7 @@ define([
246252

247253
result = obj.reload('*', true);
248254

249-
expect($.getJSON).toHaveBeenCalled();
255+
expect(jQuery.getJSON).toHaveBeenCalled();
250256
expect(result).toEqual(jasmine.objectContaining({
251257
responseJSON: {
252258
cart: {},
@@ -261,6 +267,12 @@ define([
261267
it('Should be defined', function () {
262268
expect(obj.hasOwnProperty('invalidate')).toBeDefined();
263269
});
270+
271+
it('Does not throw before component is initialized', function () {
272+
expect(function () {
273+
obj.invalidate();
274+
}).not.toThrow();
275+
});
264276
});
265277

266278
describe('"Magento_Customer/js/customer-data" method', function () {

lib/web/jquery/jquery.storageapi.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)