Skip to content

Commit c98919b

Browse files
author
Gabriel Galvao da Gama
committed
Added unit test
1 parent 4314fd9 commit c98919b

File tree

2 files changed

+37
-16
lines changed

2 files changed

+37
-16
lines changed

app/code/Magento/Customer/view/frontend/web/js/customer-data.js

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -358,6 +358,25 @@ define([
358358
return deferred.promise();
359359
},
360360

361+
onAjaxComplete: function(event, xhr, settings) {
362+
var sections,
363+
redirects;
364+
365+
if (settings.type.match(/post|put|delete/i)) {
366+
sections = sectionConfig.getAffectedSections(settings.url);
367+
368+
if (sections && sections.length) {
369+
this.invalidate(sections);
370+
redirects = ['redirect', 'backUrl'];
371+
372+
if (_.isObject(xhr.responseJSON) && !_.isEmpty(_.pick(xhr.responseJSON, redirects))) { //eslint-disable-line
373+
return;
374+
}
375+
this.reload(sections, true);
376+
}
377+
}
378+
},
379+
361380
/**
362381
* @param {Object} settings
363382
* @constructor
@@ -376,22 +395,7 @@ define([
376395
* Events listener
377396
*/
378397
$(document).on('ajaxComplete', function (event, xhr, settings) {
379-
var sections,
380-
redirects;
381-
382-
if (settings.type.match(/post|put|delete/i)) {
383-
sections = sectionConfig.getAffectedSections(settings.url);
384-
385-
if (sections && sections.length) {
386-
customerData.invalidate(sections);
387-
redirects = ['redirect', 'backUrl'];
388-
389-
if (_.isObject(xhr.responseJSON) && !_.isEmpty(_.pick(xhr.responseJSON, redirects))) { //eslint-disable-line
390-
return;
391-
}
392-
customerData.reload(sections, true);
393-
}
394-
}
398+
customerData.onAjaxComplete(event, xhr, settings);
395399
});
396400

397401
/**

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -510,6 +510,23 @@ define([
510510
});
511511
});
512512

513+
describe('"onAjaxComplete" method', function () {
514+
it('Should not trigger reload if sections is empty', function () {
515+
var event, xhr, settings;
516+
517+
event = jasmine.createSpy().and.returnValue($.event);
518+
xhr = jasmine.createSpy();
519+
spyOn(sectionConfig, 'getAffectedSections').and.returnValue([]);
520+
spyOn(obj, 'reload');
521+
settings = {
522+
type: "POST",
523+
url: "http://test.local"
524+
};
525+
obj.onAjaxComplete(event, xhr, settings);
526+
expect(obj.reload).not.toHaveBeenCalled();
527+
});
528+
});
529+
513530
describe('"Magento_Customer/js/customer-data" method', function () {
514531
it('Should be defined', function () {
515532
expect(obj.hasOwnProperty('Magento_Customer/js/customer-data')).toBeDefined();

0 commit comments

Comments
 (0)