Skip to content

Commit 1505a36

Browse files
author
Greg Harvell
committed
Added Jasmine tests for Magento_Customer customer-data.js
1 parent 4f10b5d commit 1505a36

File tree

1 file changed

+272
-0
lines changed

1 file changed

+272
-0
lines changed
Lines changed: 272 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,272 @@
1+
/**
2+
* Copyright © Magento, Inc. All rights reserved.
3+
* See COPYING.txt for license details.
4+
*/
5+
6+
/* eslint max-nested-callbacks: 0 */
7+
8+
define([
9+
'squire'
10+
], function (Squire) {
11+
'use strict';
12+
13+
var injector = new Squire(),
14+
originalGetJSON,
15+
storage,
16+
storageInvalidation = {},
17+
obj;
18+
19+
beforeEach(function (done) {
20+
injector.require(['Magento_Customer/js/customer-data'], function (Constr) {
21+
originalGetJSON = $.getJSON;
22+
obj = Constr;
23+
done();
24+
});
25+
});
26+
27+
afterEach(function () {
28+
try {
29+
injector.clean();
30+
injector.remove();
31+
$.getJSON = originalGetJSON;
32+
} catch (e) {
33+
}
34+
});
35+
36+
describe('Magento_Customer/js/customer-data', function () {
37+
38+
describe('"init" method', function () {
39+
beforeEach(function () {
40+
spyOn(obj, "reload").and.returnValue(true);
41+
42+
storageInvalidation = {
43+
keys: function () {
44+
return ['section'];
45+
}
46+
}
47+
48+
var dataProvider = {
49+
getFromStorage: function (sections) {
50+
return ['section'];
51+
}
52+
};
53+
54+
storage = {
55+
keys: function () {
56+
return ['section'];
57+
}
58+
};
59+
60+
spyOn(dataProvider, "getFromStorage");
61+
spyOn(storage, "keys").and.returnValue(['section']);
62+
spyOn(storageInvalidation, "keys").and.returnValue(['section']);
63+
});
64+
65+
it('Should be defined', function () {
66+
expect(obj.hasOwnProperty('init')).toBeDefined();
67+
});
68+
69+
it('Does not throw before component is initialized', function () {
70+
expect(function () {
71+
obj.init();
72+
}).not.toThrow();
73+
});
74+
75+
it('Calls "getExpiredSectionNames" method', function () {
76+
spyOn(obj, "getExpiredSectionNames").and.returnValue([]);
77+
obj.init();
78+
expect(obj.getExpiredSectionNames).toHaveBeenCalled();
79+
});
80+
81+
it('Calls "reload" method when expired sections exist', function () {
82+
spyOn(obj, "getExpiredSectionNames").and.returnValue(['section']);
83+
obj.init();
84+
expect(obj.reload).toHaveBeenCalled();
85+
});
86+
87+
it('Calls "reload" method when expired sections do not exist', function () {
88+
spyOn(obj, "getExpiredSectionNames").and.returnValue([]);
89+
90+
_.isEmpty = jasmine.createSpy().and.returnValue(false);
91+
92+
obj.init();
93+
expect(obj.reload).toHaveBeenCalled();
94+
});
95+
});
96+
97+
describe('"getExpiredSectionNames" method', function () {
98+
it('Should be defined', function () {
99+
expect(obj.hasOwnProperty('getExpiredSectionNames')).toBeDefined();
100+
});
101+
102+
it('Does not throw before component is initialized', function () {
103+
expect(function () {
104+
obj.getExpiredSectionNames();
105+
}).not.toThrow();
106+
});
107+
});
108+
109+
describe('"get" method', function () {
110+
it('Should be defined', function () {
111+
expect(obj.hasOwnProperty('get')).toBeDefined();
112+
});
113+
114+
it('Does not throw before component is initialized', function () {
115+
expect(function () {
116+
obj.get();
117+
}).not.toThrow();
118+
});
119+
});
120+
121+
describe('"set" method', function () {
122+
it('Should be defined', function () {
123+
expect(obj.hasOwnProperty('set')).toBeDefined();
124+
});
125+
126+
it('Does not throw before component is initialized', function () {
127+
expect(function () {
128+
obj.set('cart', {});
129+
}).not.toThrow();
130+
});
131+
});
132+
133+
describe('"reload" method', function () {
134+
beforeEach(function () {
135+
$.getJSON = jasmine.createSpy().and.callFake(function (url, parameters) {
136+
var deferred = $.Deferred();
137+
138+
deferred.promise().done = function () {
139+
return {
140+
responseJSON: {
141+
section: {}
142+
}
143+
};
144+
};
145+
146+
return deferred.promise();
147+
});
148+
});
149+
150+
it('Should be defined', function () {
151+
expect(obj.hasOwnProperty('reload')).toBeDefined();
152+
});
153+
154+
it('Does not throw before component is initialized', function () {
155+
expect(function () {
156+
obj.reload();
157+
}).not.toThrow();
158+
});
159+
160+
it('Returns proper sections object when passed array with a single section name', function () {
161+
var result;
162+
163+
$.getJSON = jasmine.createSpy().and.callFake(function (url, parameters) {
164+
var deferred = $.Deferred();
165+
166+
deferred.promise().done = function () {
167+
return {
168+
responseJSON: {
169+
section: {}
170+
}
171+
};
172+
};
173+
174+
expect(parameters).toEqual(jasmine.objectContaining({
175+
"sections": "section"
176+
}));
177+
178+
return deferred.promise();
179+
});
180+
181+
result = obj.reload(['section'], true);
182+
183+
expect(result).toEqual(jasmine.objectContaining({
184+
responseJSON: {
185+
section: {}
186+
}
187+
}));
188+
});
189+
190+
it('Returns proper sections object when passed array with a multiple section names', function () {
191+
var result;
192+
193+
$.getJSON = jasmine.createSpy().and.callFake(function (url, parameters) {
194+
var deferred = $.Deferred();
195+
196+
expect(parameters).toEqual(jasmine.objectContaining({
197+
"sections": "cart,customer,messages"
198+
}));
199+
200+
deferred.promise().done = function () {
201+
return {
202+
responseJSON: {
203+
cart: {},
204+
customer: {},
205+
messages: {}
206+
}
207+
};
208+
};
209+
210+
return deferred.promise();
211+
});
212+
213+
result = obj.reload(['cart', 'customer', 'messages'], true);
214+
215+
expect(result).toEqual(jasmine.objectContaining({
216+
responseJSON: {
217+
cart: {},
218+
customer: {},
219+
messages: {}
220+
}
221+
}));
222+
});
223+
224+
it('Returns all sections when passed wildcard string', function () {
225+
var result;
226+
227+
$.getJSON = jasmine.createSpy().and.callFake(function (url, parameters) {
228+
var deferred = $.Deferred();
229+
230+
expect(parameters).toEqual(jasmine.objectContaining({
231+
"force_new_section_timestamp": true
232+
}));
233+
234+
deferred.promise().done = function () {
235+
return {
236+
responseJSON: {
237+
cart: {},
238+
customer: {},
239+
messages: {}
240+
}
241+
};
242+
};
243+
244+
return deferred.promise();
245+
});
246+
247+
result = obj.reload('*', true);
248+
249+
expect($.getJSON).toHaveBeenCalled();
250+
expect(result).toEqual(jasmine.objectContaining({
251+
responseJSON: {
252+
cart: {},
253+
customer: {},
254+
messages: {}
255+
}
256+
}));
257+
});
258+
});
259+
260+
describe('"invalidate" method', function () {
261+
it('Should be defined', function () {
262+
expect(obj.hasOwnProperty('invalidate')).toBeDefined();
263+
});
264+
});
265+
266+
describe('"Magento_Customer/js/customer-data" method', function () {
267+
it('Should be defined', function () {
268+
expect(obj.hasOwnProperty('Magento_Customer/js/customer-data')).toBeDefined();
269+
});
270+
});
271+
});
272+
});

0 commit comments

Comments
 (0)