Skip to content

Commit

Permalink
Adjustments to customer-data.test.js based off code review.
Browse files Browse the repository at this point in the history
  • Loading branch information
Greg Harvell committed Sep 9, 2020
1 parent 6f9a274 commit 5de71f6
Showing 1 changed file with 45 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ define([

var injector = new Squire(),
obj,
_,
originaljQuery,
originalGetJSON,
originalReload,
originalIsEmpty,
Expand Down Expand Up @@ -103,20 +105,22 @@ define([
});

beforeEach(function (done) {
originalGetJSON = jQuery.getJSON;
originalGetJSON = $.getJSON;
sectionConfig['Magento_Customer/js/section-config'](sectionConfigSettings);

injector.require([
'underscore',
'Magento_Customer/js/customer-data'
], function (Constr) {
], function (underscore, Constr) {
_ = underscore;
obj = Constr;
done();
});
});

afterEach(function () {
try {
jQuery.getJSON = originalGetJSON;
$.getJSON = originalGetJSON;
clearLocalStorage();
injector.clean();
injector.remove();
Expand All @@ -125,14 +129,20 @@ define([
});

describe('"init" method', function () {
var storageInvalidation = {
keys: function () {
return ['section'];
}
};

beforeEach(function () {
originalReload = obj.reload;
originalIsEmpty = _.isEmpty;

spyOn(obj, 'reload').and.returnValue(true);

$.initNamespaceStorage('mage-cache-storage').localStorage;
$.initNamespaceStorage('mage-cache-storage-section-invalidation').localStorage;

spyOn(storageInvalidation, 'keys').and.returnValue(['section']);
});

afterEach(function () {
Expand Down Expand Up @@ -161,13 +171,15 @@ define([

it('Calls "reload" method when expired sections exist', function () {
spyOn(obj, 'getExpiredSectionNames').and.returnValue(['section']);
spyOn(obj, 'reload').and.returnValue(true);
obj.init();
expect(obj.reload).toHaveBeenCalled();
});

it('Calls "reload" method when expired sections do not exist', function () {
spyOn(obj, 'getExpiredSectionNames').and.returnValue([]);
_.isEmpty = jasmine.createSpy('_.isEmpty').and.returnValue(false);
spyOn(obj, 'reload').and.returnValue(true);
spyOn(_, 'isEmpty').and.returnValue(false);

obj.init();
expect(obj.reload).toHaveBeenCalled();
Expand All @@ -180,14 +192,14 @@ define([
}
});

jQuery.getJSON = jasmine.createSpy().and.callFake(function () {
$.getJSON = jasmine.createSpy().and.callFake(function () {
var deferred = $.Deferred();

return deferred.promise();
});

init();
expect(jQuery.getJSON).not.toHaveBeenCalled();
expect($.getJSON).not.toHaveBeenCalled();
});

it('Check it requests sections from the server if there are expired sections', function () {
Expand Down Expand Up @@ -246,6 +258,13 @@ define([
'content': {}
}
});

$.getJSON = jasmine.createSpy('$.getJSON').and.callFake(function () {
var deferred = $.Deferred();

return deferred.promise();
});

init();
expect(customerData.getExpiredSectionNames()).toEqual(['cart']);
});
Expand All @@ -268,6 +287,12 @@ define([
}
});

$.getJSON = jasmine.createSpy('$.getJSON').and.callFake(function () {
var deferred = $.Deferred();

return deferred.promise();
});

init();
expect(customerData.getExpiredSectionNames()).toEqual(['cart']);
});
Expand Down Expand Up @@ -320,7 +345,10 @@ define([

describe('"reload" method', function () {
beforeEach(function () {
jQuery.getJSON = jasmine.createSpy().and.callFake(function () {
originaljQuery = $;
$ = jQuery;

$.getJSON = jasmine.createSpy().and.callFake(function () {
var deferred = $.Deferred();

/**
Expand All @@ -339,6 +367,10 @@ define([
});
});

afterEach(function () {
$ = originaljQuery;
});

it('Should be defined', function () {
expect(obj.hasOwnProperty('reload')).toBeDefined();
});
Expand All @@ -354,7 +386,7 @@ define([

spyOn(sectionConfig, 'filterClientSideSections').and.returnValue(['section']);

jQuery.getJSON = jasmine.createSpy().and.callFake(function (url, parameters) {
$.getJSON = jasmine.createSpy().and.callFake(function (url, parameters) {
var deferred = $.Deferred();

/**
Expand Down Expand Up @@ -390,7 +422,7 @@ define([

spyOn(sectionConfig, 'filterClientSideSections').and.returnValue(['cart,customer,messages']);

jQuery.getJSON = jasmine.createSpy().and.callFake(function (url, parameters) {
$.getJSON = jasmine.createSpy().and.callFake(function (url, parameters) {
var deferred = $.Deferred();

expect(parameters).toEqual(jasmine.objectContaining({
Expand Down Expand Up @@ -428,7 +460,7 @@ define([
it('Check it returns all sections when passed wildcard string', function () {
var result;

jQuery.getJSON = jasmine.createSpy().and.callFake(function (url, parameters) {
$.getJSON = jasmine.createSpy().and.callFake(function (url, parameters) {
var deferred = $.Deferred();

expect(parameters).toEqual(jasmine.objectContaining({
Expand All @@ -454,7 +486,7 @@ define([

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

expect(jQuery.getJSON).toHaveBeenCalled();
expect($.getJSON).toHaveBeenCalled();
expect(result).toEqual(jasmine.objectContaining({
responseJSON: {
cart: {},
Expand Down

0 comments on commit 5de71f6

Please sign in to comment.