From 4f10b5db85a2a3387af81978e616ed0d21a77c4b Mon Sep 17 00:00:00 2001 From: Greg Harvell Date: Sun, 12 Jul 2020 12:12:56 -0400 Subject: [PATCH 01/14] Fix for wildcard section reload being broken by section-config refactor in 2bd4cb5 --- app/code/Magento/Customer/view/frontend/web/js/customer-data.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/code/Magento/Customer/view/frontend/web/js/customer-data.js b/app/code/Magento/Customer/view/frontend/web/js/customer-data.js index 770ea47d754d3..1e4b9050dc2b3 100644 --- a/app/code/Magento/Customer/view/frontend/web/js/customer-data.js +++ b/app/code/Magento/Customer/view/frontend/web/js/customer-data.js @@ -86,7 +86,7 @@ define([ var parameters; sectionNames = sectionConfig.filterClientSideSections(sectionNames); - parameters = _.isArray(sectionNames) ? { + parameters = _.isArray(sectionNames) && sectionNames.indexOf('*') < 0 ? { sections: sectionNames.join(',') } : []; parameters['force_new_section_timestamp'] = forceNewSectionTimestamp; From 1505a36a8b21c0660e1cf519b7bd703e581789c1 Mon Sep 17 00:00:00 2001 From: Greg Harvell Date: Mon, 20 Jul 2020 15:08:18 -0400 Subject: [PATCH 02/14] Added Jasmine tests for Magento_Customer customer-data.js --- .../frontend/js/customer-data.test.js | 272 ++++++++++++++++++ 1 file changed, 272 insertions(+) create mode 100644 dev/tests/js/jasmine/tests/app/code/Magento/Customer/frontend/js/customer-data.test.js diff --git a/dev/tests/js/jasmine/tests/app/code/Magento/Customer/frontend/js/customer-data.test.js b/dev/tests/js/jasmine/tests/app/code/Magento/Customer/frontend/js/customer-data.test.js new file mode 100644 index 0000000000000..398d48f2e3a08 --- /dev/null +++ b/dev/tests/js/jasmine/tests/app/code/Magento/Customer/frontend/js/customer-data.test.js @@ -0,0 +1,272 @@ +/** + * Copyright © Magento, Inc. All rights reserved. + * See COPYING.txt for license details. + */ + +/* eslint max-nested-callbacks: 0 */ + +define([ + 'squire' +], function (Squire) { + 'use strict'; + + var injector = new Squire(), + originalGetJSON, + storage, + storageInvalidation = {}, + obj; + + beforeEach(function (done) { + injector.require(['Magento_Customer/js/customer-data'], function (Constr) { + originalGetJSON = $.getJSON; + obj = Constr; + done(); + }); + }); + + afterEach(function () { + try { + injector.clean(); + injector.remove(); + $.getJSON = originalGetJSON; + } catch (e) { + } + }); + + describe('Magento_Customer/js/customer-data', function () { + + describe('"init" method', function () { + beforeEach(function () { + spyOn(obj, "reload").and.returnValue(true); + + storageInvalidation = { + keys: function () { + return ['section']; + } + } + + var dataProvider = { + getFromStorage: function (sections) { + return ['section']; + } + }; + + storage = { + keys: function () { + return ['section']; + } + }; + + spyOn(dataProvider, "getFromStorage"); + spyOn(storage, "keys").and.returnValue(['section']); + spyOn(storageInvalidation, "keys").and.returnValue(['section']); + }); + + it('Should be defined', function () { + expect(obj.hasOwnProperty('init')).toBeDefined(); + }); + + it('Does not throw before component is initialized', function () { + expect(function () { + obj.init(); + }).not.toThrow(); + }); + + it('Calls "getExpiredSectionNames" method', function () { + spyOn(obj, "getExpiredSectionNames").and.returnValue([]); + obj.init(); + expect(obj.getExpiredSectionNames).toHaveBeenCalled(); + }); + + it('Calls "reload" method when expired sections exist', function () { + spyOn(obj, "getExpiredSectionNames").and.returnValue(['section']); + 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().and.returnValue(false); + + obj.init(); + expect(obj.reload).toHaveBeenCalled(); + }); + }); + + describe('"getExpiredSectionNames" method', function () { + it('Should be defined', function () { + expect(obj.hasOwnProperty('getExpiredSectionNames')).toBeDefined(); + }); + + it('Does not throw before component is initialized', function () { + expect(function () { + obj.getExpiredSectionNames(); + }).not.toThrow(); + }); + }); + + describe('"get" method', function () { + it('Should be defined', function () { + expect(obj.hasOwnProperty('get')).toBeDefined(); + }); + + it('Does not throw before component is initialized', function () { + expect(function () { + obj.get(); + }).not.toThrow(); + }); + }); + + describe('"set" method', function () { + it('Should be defined', function () { + expect(obj.hasOwnProperty('set')).toBeDefined(); + }); + + it('Does not throw before component is initialized', function () { + expect(function () { + obj.set('cart', {}); + }).not.toThrow(); + }); + }); + + describe('"reload" method', function () { + beforeEach(function () { + $.getJSON = jasmine.createSpy().and.callFake(function (url, parameters) { + var deferred = $.Deferred(); + + deferred.promise().done = function () { + return { + responseJSON: { + section: {} + } + }; + }; + + return deferred.promise(); + }); + }); + + it('Should be defined', function () { + expect(obj.hasOwnProperty('reload')).toBeDefined(); + }); + + it('Does not throw before component is initialized', function () { + expect(function () { + obj.reload(); + }).not.toThrow(); + }); + + it('Returns proper sections object when passed array with a single section name', function () { + var result; + + $.getJSON = jasmine.createSpy().and.callFake(function (url, parameters) { + var deferred = $.Deferred(); + + deferred.promise().done = function () { + return { + responseJSON: { + section: {} + } + }; + }; + + expect(parameters).toEqual(jasmine.objectContaining({ + "sections": "section" + })); + + return deferred.promise(); + }); + + result = obj.reload(['section'], true); + + expect(result).toEqual(jasmine.objectContaining({ + responseJSON: { + section: {} + } + })); + }); + + it('Returns proper sections object when passed array with a multiple section names', function () { + var result; + + $.getJSON = jasmine.createSpy().and.callFake(function (url, parameters) { + var deferred = $.Deferred(); + + expect(parameters).toEqual(jasmine.objectContaining({ + "sections": "cart,customer,messages" + })); + + deferred.promise().done = function () { + return { + responseJSON: { + cart: {}, + customer: {}, + messages: {} + } + }; + }; + + return deferred.promise(); + }); + + result = obj.reload(['cart', 'customer', 'messages'], true); + + expect(result).toEqual(jasmine.objectContaining({ + responseJSON: { + cart: {}, + customer: {}, + messages: {} + } + })); + }); + + it('Returns all sections when passed wildcard string', function () { + var result; + + $.getJSON = jasmine.createSpy().and.callFake(function (url, parameters) { + var deferred = $.Deferred(); + + expect(parameters).toEqual(jasmine.objectContaining({ + "force_new_section_timestamp": true + })); + + deferred.promise().done = function () { + return { + responseJSON: { + cart: {}, + customer: {}, + messages: {} + } + }; + }; + + return deferred.promise(); + }); + + result = obj.reload('*', true); + + expect($.getJSON).toHaveBeenCalled(); + expect(result).toEqual(jasmine.objectContaining({ + responseJSON: { + cart: {}, + customer: {}, + messages: {} + } + })); + }); + }); + + describe('"invalidate" method', function () { + it('Should be defined', function () { + expect(obj.hasOwnProperty('invalidate')).toBeDefined(); + }); + }); + + describe('"Magento_Customer/js/customer-data" method', function () { + it('Should be defined', function () { + expect(obj.hasOwnProperty('Magento_Customer/js/customer-data')).toBeDefined(); + }); + }); + }); +}); From ff99146565f414f23a4105b3f8926d31fe4a39e8 Mon Sep 17 00:00:00 2001 From: Greg Harvell Date: Thu, 23 Jul 2020 10:00:54 -0400 Subject: [PATCH 03/14] 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. --- .../frontend/js/customer-data.test.js | 86 +++++++++++-------- lib/web/jquery/jquery.storageapi.min.js | 2 +- 2 files changed, 50 insertions(+), 38 deletions(-) diff --git a/dev/tests/js/jasmine/tests/app/code/Magento/Customer/frontend/js/customer-data.test.js b/dev/tests/js/jasmine/tests/app/code/Magento/Customer/frontend/js/customer-data.test.js index 398d48f2e3a08..d973d3a1bd9c0 100644 --- a/dev/tests/js/jasmine/tests/app/code/Magento/Customer/frontend/js/customer-data.test.js +++ b/dev/tests/js/jasmine/tests/app/code/Magento/Customer/frontend/js/customer-data.test.js @@ -6,57 +6,57 @@ /* eslint max-nested-callbacks: 0 */ define([ - 'squire' -], function (Squire) { + 'squire', + 'jquery', + 'jquery/jquery-storageapi' +], function (Squire, $) { 'use strict'; var injector = new Squire(), - originalGetJSON, - storage, - storageInvalidation = {}, + sectionConfig, obj; - beforeEach(function (done) { - injector.require(['Magento_Customer/js/customer-data'], function (Constr) { - originalGetJSON = $.getJSON; - obj = Constr; - done(); - }); - }); + describe('Magento_Customer/js/customer-data', function () { - afterEach(function () { - try { - injector.clean(); - injector.remove(); - $.getJSON = originalGetJSON; - } catch (e) { - } - }); + beforeEach(function (done) { + injector.require([ + 'Magento_Customer/js/customer-data', + 'Magento_Customer/js/section-config' + ], function (Constr, sectionConfiguration) { + obj = Constr; + sectionConfig = sectionConfiguration; + done(); + }); + }); - describe('Magento_Customer/js/customer-data', function () { + afterEach(function () { + try { + injector.clean(); + injector.remove(); + } catch (e) { + } + }); describe('"init" method', function () { - beforeEach(function () { - spyOn(obj, "reload").and.returnValue(true); - - storageInvalidation = { + var storageInvalidation = { keys: function () { return ['section']; } - } - - var dataProvider = { - getFromStorage: function (sections) { + }, + dataProvider = { + getFromStorage: function () { return ['section']; } - }; - + }, storage = { keys: function () { return ['section']; } }; + beforeEach(function () { + spyOn(obj, "reload").and.returnValue(true); + spyOn($, 'initNamespaceStorage').and.returnValue(true); spyOn(dataProvider, "getFromStorage"); spyOn(storage, "keys").and.returnValue(['section']); spyOn(storageInvalidation, "keys").and.returnValue(['section']); @@ -124,15 +124,17 @@ define([ }); it('Does not throw before component is initialized', function () { + _.each = jasmine.createSpy().and.returnValue(true); + expect(function () { - obj.set('cart', {}); + obj.set(); }).not.toThrow(); }); }); describe('"reload" method', function () { beforeEach(function () { - $.getJSON = jasmine.createSpy().and.callFake(function (url, parameters) { + jQuery.getJSON = jasmine.createSpy().and.callFake(function (url, parameters) { var deferred = $.Deferred(); deferred.promise().done = function () { @@ -160,7 +162,9 @@ define([ it('Returns proper sections object when passed array with a single section name', function () { var result; - $.getJSON = jasmine.createSpy().and.callFake(function (url, parameters) { + spyOn(sectionConfig, 'filterClientSideSections').and.returnValue(['section']); + + jQuery.getJSON = jasmine.createSpy().and.callFake(function (url, parameters) { var deferred = $.Deferred(); deferred.promise().done = function () { @@ -190,7 +194,9 @@ define([ it('Returns proper sections object when passed array with a multiple section names', function () { var result; - $.getJSON = jasmine.createSpy().and.callFake(function (url, parameters) { + spyOn(sectionConfig, 'filterClientSideSections').and.returnValue(['cart,customer,messages']); + + jQuery.getJSON = jasmine.createSpy().and.callFake(function (url, parameters) { var deferred = $.Deferred(); expect(parameters).toEqual(jasmine.objectContaining({ @@ -224,7 +230,7 @@ define([ it('Returns all sections when passed wildcard string', function () { var result; - $.getJSON = jasmine.createSpy().and.callFake(function (url, parameters) { + jQuery.getJSON = jasmine.createSpy().and.callFake(function (url, parameters) { var deferred = $.Deferred(); expect(parameters).toEqual(jasmine.objectContaining({ @@ -246,7 +252,7 @@ define([ result = obj.reload('*', true); - expect($.getJSON).toHaveBeenCalled(); + expect(jQuery.getJSON).toHaveBeenCalled(); expect(result).toEqual(jasmine.objectContaining({ responseJSON: { cart: {}, @@ -261,6 +267,12 @@ define([ it('Should be defined', function () { expect(obj.hasOwnProperty('invalidate')).toBeDefined(); }); + + it('Does not throw before component is initialized', function () { + expect(function () { + obj.invalidate(); + }).not.toThrow(); + }); }); describe('"Magento_Customer/js/customer-data" method', function () { diff --git a/lib/web/jquery/jquery.storageapi.min.js b/lib/web/jquery/jquery.storageapi.min.js index 886c3d847ed3b..5f39773343663 100644 --- a/lib/web/jquery/jquery.storageapi.min.js +++ b/lib/web/jquery/jquery.storageapi.min.js @@ -1,2 +1,2 @@ /* jQuery Storage API Plugin 1.7.3 https://github.com/julien-maurel/jQuery-Storage-API */ -!function(e){"function"==typeof define&&define.amd?define(["jquery", "jquery/jquery.cookie"],e):e("object"==typeof exports?require("jquery"):jQuery)}(function(e){function t(t){var r,i,n,o=arguments.length,s=window[t],a=arguments,u=a[1];if(2>o)throw Error("Minimum 2 arguments must be given");if(e.isArray(u)){i={};for(var f in u){r=u[f];try{i[r]=JSON.parse(s.getItem(r))}catch(c){i[r]=s.getItem(r)}}return i}if(2!=o){try{i=JSON.parse(s.getItem(u))}catch(c){throw new ReferenceError(u+" is not defined in this storage")}for(var f=2;o-1>f;f++)if(i=i[a[f]],void 0===i)throw new ReferenceError([].slice.call(a,1,f+1).join(".")+" is not defined in this storage");if(e.isArray(a[f])){n=i,i={};for(var m in a[f])i[a[f][m]]=n[a[f][m]];return i}return i[a[f]]}try{return JSON.parse(s.getItem(u))}catch(c){return s.getItem(u)}}function r(t){var r,i,n=arguments.length,o=window[t],s=arguments,a=s[1],u=s[2],f={};if(2>n||!e.isPlainObject(a)&&3>n)throw Error("Minimum 3 arguments must be given or second parameter must be an object");if(e.isPlainObject(a)){for(var c in a)r=a[c],e.isPlainObject(r)?o.setItem(c,JSON.stringify(r)):o.setItem(c,r);return a}if(3==n)return"object"==typeof u?o.setItem(a,JSON.stringify(u)):o.setItem(a,u),u;try{i=o.getItem(a),null!=i&&(f=JSON.parse(i))}catch(m){}i=f;for(var c=2;n-2>c;c++)r=s[c],i[r]&&e.isPlainObject(i[r])||(i[r]={}),i=i[r];return i[s[c]]=s[c+1],o.setItem(a,JSON.stringify(f)),f}function i(t){var r,i,n=arguments.length,o=window[t],s=arguments,a=s[1];if(2>n)throw Error("Minimum 2 arguments must be given");if(e.isArray(a)){for(var u in a)o.removeItem(a[u]);return!0}if(2==n)return o.removeItem(a),!0;try{r=i=JSON.parse(o.getItem(a))}catch(f){throw new ReferenceError(a+" is not defined in this storage")}for(var u=2;n-1>u;u++)if(i=i[s[u]],void 0===i)throw new ReferenceError([].slice.call(s,1,u).join(".")+" is not defined in this storage");if(e.isArray(s[u]))for(var c in s[u])delete i[s[u][c]];else delete i[s[u]];return o.setItem(a,JSON.stringify(r)),!0}function n(t,r){var n=a(t);for(var o in n)i(t,n[o]);if(r)for(var o in e.namespaceStorages)u(o)}function o(r){var i=arguments.length,n=arguments,s=(window[r],n[1]);if(1==i)return 0==a(r).length;if(e.isArray(s)){for(var u=0;ui)throw Error("Minimum 2 arguments must be given");if(e.isArray(o)){for(var a=0;a1?t.apply(this,o):n,a._cookie)for(var u in e.cookie())""!=u&&s.push(u.replace(a._prefix,""));else for(var f in a)s.push(f);return s}function u(t){if(!t||"string"!=typeof t)throw Error("First parameter must be a string");g?(window.localStorage.getItem(t)||window.localStorage.setItem(t,"{}"),window.sessionStorage.getItem(t)||window.sessionStorage.setItem(t,"{}")):(window.localCookieStorage.getItem(t)||window.localCookieStorage.setItem(t,"{}"),window.sessionCookieStorage.getItem(t)||window.sessionCookieStorage.setItem(t,"{}"));var r={localStorage:e.extend({},e.localStorage,{_ns:t}),sessionStorage:e.extend({},e.sessionStorage,{_ns:t})};return e.cookie&&(window.cookieStorage.getItem(t)||window.cookieStorage.setItem(t,"{}"),r.cookieStorage=e.extend({},e.cookieStorage,{_ns:t})),e.namespaceStorages[t]=r,r}function f(e){if(!window[e])return!1;var t="jsapi";try{return window[e].setItem(t,t),window[e].removeItem(t),!0}catch(r){return!1}}var c="ls_",m="ss_",g=f("localStorage"),h={_type:"",_ns:"",_callMethod:function(e,t){var r=[this._type],t=Array.prototype.slice.call(t),i=t[0];return this._ns&&r.push(this._ns),"string"==typeof i&&-1!==i.indexOf(".")&&(t.shift(),[].unshift.apply(t,i.split("."))),[].push.apply(r,t),e.apply(this,r)},get:function(){return this._callMethod(t,arguments)},set:function(){var t=arguments.length,i=arguments,n=i[0];if(1>t||!e.isPlainObject(n)&&2>t)throw Error("Minimum 2 arguments must be given or first parameter must be an object");if(e.isPlainObject(n)&&this._ns){for(var o in n)r(this._type,this._ns,o,n[o]);return n}var s=this._callMethod(r,i);return this._ns?s[n.split(".")[0]]:s},remove:function(){if(arguments.length<1)throw Error("Minimum 1 argument must be given");return this._callMethod(i,arguments)},removeAll:function(e){return this._ns?(r(this._type,this._ns,{}),!0):n(this._type,e)},isEmpty:function(){return this._callMethod(o,arguments)},isSet:function(){if(arguments.length<1)throw Error("Minimum 1 argument must be given");return this._callMethod(s,arguments)},keys:function(){return this._callMethod(a,arguments)}};if(e.cookie){window.name||(window.name=Math.floor(1e8*Math.random()));var l={_cookie:!0,_prefix:"",_expires:null,_path:null,_domain:null,setItem:function(t,r){e.cookie(this._prefix+t,r,{expires:this._expires,path:this._path,domain:this._domain})},getItem:function(t){return e.cookie(this._prefix+t)},removeItem:function(t){return e.removeCookie(this._prefix+t)},clear:function(){for(var t in e.cookie())""!=t&&(!this._prefix&&-1===t.indexOf(c)&&-1===t.indexOf(m)||this._prefix&&0===t.indexOf(this._prefix))&&e.removeCookie(t)},setExpires:function(e){return this._expires=e,this},setPath:function(e){return this._path=e,this},setDomain:function(e){return this._domain=e,this},setConf:function(e){return e.path&&(this._path=e.path),e.domain&&(this._domain=e.domain),e.expires&&(this._expires=e.expires),this},setDefaultConf:function(){this._path=this._domain=this._expires=null}};g||(window.localCookieStorage=e.extend({},l,{_prefix:c,_expires:3650}),window.sessionCookieStorage=e.extend({},l,{_prefix:m+window.name+"_"})),window.cookieStorage=e.extend({},l),e.cookieStorage=e.extend({},h,{_type:"cookieStorage",setExpires:function(e){return window.cookieStorage.setExpires(e),this},setPath:function(e){return window.cookieStorage.setPath(e),this},setDomain:function(e){return window.cookieStorage.setDomain(e),this},setConf:function(e){return window.cookieStorage.setConf(e),this},setDefaultConf:function(){return window.cookieStorage.setDefaultConf(),this}})}e.initNamespaceStorage=function(e){return u(e)},g?(e.localStorage=e.extend({},h,{_type:"localStorage"}),e.sessionStorage=e.extend({},h,{_type:"sessionStorage"})):(e.localStorage=e.extend({},h,{_type:"localCookieStorage"}),e.sessionStorage=e.extend({},h,{_type:"sessionCookieStorage"})),e.namespaceStorages={},e.removeAllStorages=function(t){e.localStorage.removeAll(t),e.sessionStorage.removeAll(t),e.cookieStorage&&e.cookieStorage.removeAll(t),t||(e.namespaceStorages={})}}); \ No newline at end of file +!function(e){"function"==typeof define&&define.amd?define(["jquery", "jquery/jquery.cookie"],e):e("object"==typeof exports?require("jquery"):jQuery)}(function(e){function t(t){var r,i,n,o=arguments.length,s=window[t],a=arguments,u=a[1];if(2>o)throw Error("Minimum 2 arguments must be given");if(e.isArray(u)){i={};for(var f in u){r=u[f];try{i[r]=JSON.parse(s.getItem(r))}catch(c){i[r]=s.getItem(r)}}return i}if(2!=o){try{i=JSON.parse(s.getItem(u))}catch(c){throw new ReferenceError(u+" is not defined in this storage")}for(var f=2;o-1>f;f++)if(i=i[a[f]],void 0===i)throw new ReferenceError([].slice.call(a,1,f+1).join(".")+" is not defined in this storage");if(e.isArray(a[f])){n=i,i={};for(var m in a[f])i[a[f][m]]=n[a[f][m]];return i}return i[a[f]]}try{return JSON.parse(s.getItem(u))}catch(c){return s.getItem(u)}}function r(t){var r,i,n=arguments.length,o=window[t],s=arguments,a=s[1],u=s[2],f={};if(2>n||!e.isPlainObject(a)&&3>n)throw Error("Minimum 3 arguments must be given or second parameter must be an object");if(e.isPlainObject(a)){for(var c in a)r=a[c],e.isPlainObject(r)?o.setItem(c,JSON.stringify(r)):o.setItem(c,r);return a}if(3==n)return"object"==typeof u?o.setItem(a,JSON.stringify(u)):o.setItem(a,u),u;try{i=o.getItem(a),null!=i&&(f=JSON.parse(i))}catch(m){}i=f;for(var c=2;n-2>c;c++)r=s[c],i[r]&&e.isPlainObject(i[r])||(i[r]={}),i=i[r];return i[s[c]]=s[c+1],o.setItem(a,JSON.stringify(f)),f}function i(t){var r,i,n=arguments.length,o=window[t],s=arguments,a=s[1];if(2>n)throw Error("Minimum 2 arguments must be given");if(e.isArray(a)){for(var u in a)o.removeItem(a[u]);return!0}if(2==n)return o.removeItem(a),!0;try{r=i=JSON.parse(o.getItem(a))}catch(f){throw new ReferenceError(a+" is not defined in this storage")}for(var u=2;n-1>u;u++)if(i=i[s[u]],void 0===i)throw new ReferenceError([].slice.call(s,1,u).join(".")+" is not defined in this storage");if(e.isArray(s[u]))for(var c in s[u])delete i[s[u][c]];else delete i[s[u]];return o.setItem(a,JSON.stringify(r)),!0}function n(t,r){var n=a(t);for(var o in n)i(t,n[o]);if(r)for(var o in e.namespaceStorages)u(o)}function o(r){var i=arguments.length,n=arguments,s=(window[r],n[1]);if(1==i)return 0==a(r).length;if(e.isArray(s)){for(var u=0;ui)throw Error("Minimum 2 arguments must be given");if(e.isArray(o)){for(var a=0;a1?t.apply(this,o):n,a&&a._cookie)for(var u in e.cookie())""!=u&&s.push(u.replace(a._prefix,""));else for(var f in a)s.push(f);return s}function u(t){if(!t||"string"!=typeof t)throw Error("First parameter must be a string");g?(window.localStorage.getItem(t)||window.localStorage.setItem(t,"{}"),window.sessionStorage.getItem(t)||window.sessionStorage.setItem(t,"{}")):(window.localCookieStorage.getItem(t)||window.localCookieStorage.setItem(t,"{}"),window.sessionCookieStorage.getItem(t)||window.sessionCookieStorage.setItem(t,"{}"));var r={localStorage:e.extend({},e.localStorage,{_ns:t}),sessionStorage:e.extend({},e.sessionStorage,{_ns:t})};return e.cookie&&(window.cookieStorage.getItem(t)||window.cookieStorage.setItem(t,"{}"),r.cookieStorage=e.extend({},e.cookieStorage,{_ns:t})),e.namespaceStorages[t]=r,r}function f(e){if(!window[e])return!1;var t="jsapi";try{return window[e].setItem(t,t),window[e].removeItem(t),!0}catch(r){return!1}}var c="ls_",m="ss_",g=f("localStorage"),h={_type:"",_ns:"",_callMethod:function(e,t){var r=[this._type],t=Array.prototype.slice.call(t),i=t[0];return this._ns&&r.push(this._ns),"string"==typeof i&&-1!==i.indexOf(".")&&(t.shift(),[].unshift.apply(t,i.split("."))),[].push.apply(r,t),e.apply(this,r)},get:function(){return this._callMethod(t,arguments)},set:function(){var t=arguments.length,i=arguments,n=i[0];if(1>t||!e.isPlainObject(n)&&2>t)throw Error("Minimum 2 arguments must be given or first parameter must be an object");if(e.isPlainObject(n)&&this._ns){for(var o in n)r(this._type,this._ns,o,n[o]);return n}var s=this._callMethod(r,i);return this._ns?s[n.split(".")[0]]:s},remove:function(){if(arguments.length<1)throw Error("Minimum 1 argument must be given");return this._callMethod(i,arguments)},removeAll:function(e){return this._ns?(r(this._type,this._ns,{}),!0):n(this._type,e)},isEmpty:function(){return this._callMethod(o,arguments)},isSet:function(){if(arguments.length<1)throw Error("Minimum 1 argument must be given");return this._callMethod(s,arguments)},keys:function(){return this._callMethod(a,arguments)}};if(e.cookie){window.name||(window.name=Math.floor(1e8*Math.random()));var l={_cookie:!0,_prefix:"",_expires:null,_path:null,_domain:null,setItem:function(t,r){e.cookie(this._prefix+t,r,{expires:this._expires,path:this._path,domain:this._domain})},getItem:function(t){return e.cookie(this._prefix+t)},removeItem:function(t){return e.removeCookie(this._prefix+t)},clear:function(){for(var t in e.cookie())""!=t&&(!this._prefix&&-1===t.indexOf(c)&&-1===t.indexOf(m)||this._prefix&&0===t.indexOf(this._prefix))&&e.removeCookie(t)},setExpires:function(e){return this._expires=e,this},setPath:function(e){return this._path=e,this},setDomain:function(e){return this._domain=e,this},setConf:function(e){return e.path&&(this._path=e.path),e.domain&&(this._domain=e.domain),e.expires&&(this._expires=e.expires),this},setDefaultConf:function(){this._path=this._domain=this._expires=null}};g||(window.localCookieStorage=e.extend({},l,{_prefix:c,_expires:3650}),window.sessionCookieStorage=e.extend({},l,{_prefix:m+window.name+"_"})),window.cookieStorage=e.extend({},l),e.cookieStorage=e.extend({},h,{_type:"cookieStorage",setExpires:function(e){return window.cookieStorage.setExpires(e),this},setPath:function(e){return window.cookieStorage.setPath(e),this},setDomain:function(e){return window.cookieStorage.setDomain(e),this},setConf:function(e){return window.cookieStorage.setConf(e),this},setDefaultConf:function(){return window.cookieStorage.setDefaultConf(),this}})}e.initNamespaceStorage=function(e){return u(e)},g?(e.localStorage=e.extend({},h,{_type:"localStorage"}),e.sessionStorage=e.extend({},h,{_type:"sessionStorage"})):(e.localStorage=e.extend({},h,{_type:"localCookieStorage"}),e.sessionStorage=e.extend({},h,{_type:"sessionCookieStorage"})),e.namespaceStorages={},e.removeAllStorages=function(t){e.localStorage.removeAll(t),e.sessionStorage.removeAll(t),e.cookieStorage&&e.cookieStorage.removeAll(t),t||(e.namespaceStorages={})}}); From 8047497701d5a332ad692844f12af11f344d9854 Mon Sep 17 00:00:00 2001 From: Greg Harvell Date: Fri, 24 Jul 2020 10:41:48 -0400 Subject: [PATCH 04/14] Fixed eslint & jscs errors with customer-data.test.js, reseting methods to their original context after being mocked. --- .../frontend/js/customer-data.test.js | 76 ++++++++++++++++--- 1 file changed, 64 insertions(+), 12 deletions(-) diff --git a/dev/tests/js/jasmine/tests/app/code/Magento/Customer/frontend/js/customer-data.test.js b/dev/tests/js/jasmine/tests/app/code/Magento/Customer/frontend/js/customer-data.test.js index d973d3a1bd9c0..613b70f4c4838 100644 --- a/dev/tests/js/jasmine/tests/app/code/Magento/Customer/frontend/js/customer-data.test.js +++ b/dev/tests/js/jasmine/tests/app/code/Magento/Customer/frontend/js/customer-data.test.js @@ -3,8 +3,8 @@ * See COPYING.txt for license details. */ +/* global _ */ /* eslint max-nested-callbacks: 0 */ - define([ 'squire', 'jquery', @@ -14,6 +14,10 @@ define([ var injector = new Squire(), sectionConfig, + originalGetJSON, + originalReload, + originalInitNamespaceStorage, + originalEach, obj; describe('Magento_Customer/js/customer-data', function () { @@ -39,27 +43,46 @@ define([ describe('"init" method', function () { var storageInvalidation = { + /** + * Mock Keys Method + * @returns array + */ keys: function () { return ['section']; } }, dataProvider = { + /** + * Mock getFromStorage Method + * @returns array + */ getFromStorage: function () { return ['section']; } }, storage = { + /** + * Mock Keys Method + * @returns array + */ keys: function () { return ['section']; } }; beforeEach(function () { - spyOn(obj, "reload").and.returnValue(true); + originalReload = obj.reload; + originalInitNamespaceStorage = $.initNamespaceStorage; + spyOn(obj, 'reload').and.returnValue(true); spyOn($, 'initNamespaceStorage').and.returnValue(true); - spyOn(dataProvider, "getFromStorage"); - spyOn(storage, "keys").and.returnValue(['section']); - spyOn(storageInvalidation, "keys").and.returnValue(['section']); + spyOn(dataProvider, 'getFromStorage'); + spyOn(storage, 'keys').and.returnValue(['section']); + spyOn(storageInvalidation, 'keys').and.returnValue(['section']); + }); + + afterEach(function () { + obj.reload = originalReload; + $.initNameSpaceStorage = originalInitNamespaceStorage; }); it('Should be defined', function () { @@ -73,19 +96,19 @@ define([ }); it('Calls "getExpiredSectionNames" method', function () { - spyOn(obj, "getExpiredSectionNames").and.returnValue([]); + spyOn(obj, 'getExpiredSectionNames').and.returnValue([]); obj.init(); expect(obj.getExpiredSectionNames).toHaveBeenCalled(); }); it('Calls "reload" method when expired sections exist', function () { - spyOn(obj, "getExpiredSectionNames").and.returnValue(['section']); + spyOn(obj, 'getExpiredSectionNames').and.returnValue(['section']); obj.init(); expect(obj.reload).toHaveBeenCalled(); }); it('Calls "reload" method when expired sections do not exist', function () { - spyOn(obj, "getExpiredSectionNames").and.returnValue([]); + spyOn(obj, 'getExpiredSectionNames').and.returnValue([]); _.isEmpty = jasmine.createSpy().and.returnValue(false); @@ -119,6 +142,14 @@ define([ }); describe('"set" method', function () { + beforeEach(function () { + originalEach = _.each; + }); + + afterEach(function () { + _.each = originalEach; + }); + it('Should be defined', function () { expect(obj.hasOwnProperty('set')).toBeDefined(); }); @@ -134,9 +165,14 @@ define([ describe('"reload" method', function () { beforeEach(function () { - jQuery.getJSON = jasmine.createSpy().and.callFake(function (url, parameters) { + originalGetJSON = jQuery.getJSON; + jQuery.getJSON = jasmine.createSpy().and.callFake(function () { var deferred = $.Deferred(); + /** + * Mock Done Method for getJSON + * @returns object + */ deferred.promise().done = function () { return { responseJSON: { @@ -149,6 +185,10 @@ define([ }); }); + afterEach(function () { + jQuery.getJSON = originalGetJSON; + }); + it('Should be defined', function () { expect(obj.hasOwnProperty('reload')).toBeDefined(); }); @@ -167,6 +207,10 @@ define([ jQuery.getJSON = jasmine.createSpy().and.callFake(function (url, parameters) { var deferred = $.Deferred(); + /** + * Mock Done Method for getJSON + * @returns object + */ deferred.promise().done = function () { return { responseJSON: { @@ -176,7 +220,7 @@ define([ }; expect(parameters).toEqual(jasmine.objectContaining({ - "sections": "section" + sections: 'section' })); return deferred.promise(); @@ -200,9 +244,13 @@ define([ var deferred = $.Deferred(); expect(parameters).toEqual(jasmine.objectContaining({ - "sections": "cart,customer,messages" + sections: 'cart,customer,messages' })); + /** + * Mock Done Method for getJSON + * @returns object + */ deferred.promise().done = function () { return { responseJSON: { @@ -234,9 +282,13 @@ define([ var deferred = $.Deferred(); expect(parameters).toEqual(jasmine.objectContaining({ - "force_new_section_timestamp": true + 'force_new_section_timestamp': true })); + /** + * Mock Done Method for getJSON + * @returns object + */ deferred.promise().done = function () { return { responseJSON: { From dd862a21c29de46c362a050638b277c7deda34c9 Mon Sep 17 00:00:00 2001 From: Greg Harvell Date: Tue, 28 Jul 2020 08:15:42 -0400 Subject: [PATCH 05/14] Reverting jQuery StorageApi back to original version. --- lib/web/jquery/jquery.storageapi.min.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/web/jquery/jquery.storageapi.min.js b/lib/web/jquery/jquery.storageapi.min.js index 5f39773343663..fcf296a384bce 100644 --- a/lib/web/jquery/jquery.storageapi.min.js +++ b/lib/web/jquery/jquery.storageapi.min.js @@ -1,2 +1,2 @@ /* jQuery Storage API Plugin 1.7.3 https://github.com/julien-maurel/jQuery-Storage-API */ -!function(e){"function"==typeof define&&define.amd?define(["jquery", "jquery/jquery.cookie"],e):e("object"==typeof exports?require("jquery"):jQuery)}(function(e){function t(t){var r,i,n,o=arguments.length,s=window[t],a=arguments,u=a[1];if(2>o)throw Error("Minimum 2 arguments must be given");if(e.isArray(u)){i={};for(var f in u){r=u[f];try{i[r]=JSON.parse(s.getItem(r))}catch(c){i[r]=s.getItem(r)}}return i}if(2!=o){try{i=JSON.parse(s.getItem(u))}catch(c){throw new ReferenceError(u+" is not defined in this storage")}for(var f=2;o-1>f;f++)if(i=i[a[f]],void 0===i)throw new ReferenceError([].slice.call(a,1,f+1).join(".")+" is not defined in this storage");if(e.isArray(a[f])){n=i,i={};for(var m in a[f])i[a[f][m]]=n[a[f][m]];return i}return i[a[f]]}try{return JSON.parse(s.getItem(u))}catch(c){return s.getItem(u)}}function r(t){var r,i,n=arguments.length,o=window[t],s=arguments,a=s[1],u=s[2],f={};if(2>n||!e.isPlainObject(a)&&3>n)throw Error("Minimum 3 arguments must be given or second parameter must be an object");if(e.isPlainObject(a)){for(var c in a)r=a[c],e.isPlainObject(r)?o.setItem(c,JSON.stringify(r)):o.setItem(c,r);return a}if(3==n)return"object"==typeof u?o.setItem(a,JSON.stringify(u)):o.setItem(a,u),u;try{i=o.getItem(a),null!=i&&(f=JSON.parse(i))}catch(m){}i=f;for(var c=2;n-2>c;c++)r=s[c],i[r]&&e.isPlainObject(i[r])||(i[r]={}),i=i[r];return i[s[c]]=s[c+1],o.setItem(a,JSON.stringify(f)),f}function i(t){var r,i,n=arguments.length,o=window[t],s=arguments,a=s[1];if(2>n)throw Error("Minimum 2 arguments must be given");if(e.isArray(a)){for(var u in a)o.removeItem(a[u]);return!0}if(2==n)return o.removeItem(a),!0;try{r=i=JSON.parse(o.getItem(a))}catch(f){throw new ReferenceError(a+" is not defined in this storage")}for(var u=2;n-1>u;u++)if(i=i[s[u]],void 0===i)throw new ReferenceError([].slice.call(s,1,u).join(".")+" is not defined in this storage");if(e.isArray(s[u]))for(var c in s[u])delete i[s[u][c]];else delete i[s[u]];return o.setItem(a,JSON.stringify(r)),!0}function n(t,r){var n=a(t);for(var o in n)i(t,n[o]);if(r)for(var o in e.namespaceStorages)u(o)}function o(r){var i=arguments.length,n=arguments,s=(window[r],n[1]);if(1==i)return 0==a(r).length;if(e.isArray(s)){for(var u=0;ui)throw Error("Minimum 2 arguments must be given");if(e.isArray(o)){for(var a=0;a1?t.apply(this,o):n,a&&a._cookie)for(var u in e.cookie())""!=u&&s.push(u.replace(a._prefix,""));else for(var f in a)s.push(f);return s}function u(t){if(!t||"string"!=typeof t)throw Error("First parameter must be a string");g?(window.localStorage.getItem(t)||window.localStorage.setItem(t,"{}"),window.sessionStorage.getItem(t)||window.sessionStorage.setItem(t,"{}")):(window.localCookieStorage.getItem(t)||window.localCookieStorage.setItem(t,"{}"),window.sessionCookieStorage.getItem(t)||window.sessionCookieStorage.setItem(t,"{}"));var r={localStorage:e.extend({},e.localStorage,{_ns:t}),sessionStorage:e.extend({},e.sessionStorage,{_ns:t})};return e.cookie&&(window.cookieStorage.getItem(t)||window.cookieStorage.setItem(t,"{}"),r.cookieStorage=e.extend({},e.cookieStorage,{_ns:t})),e.namespaceStorages[t]=r,r}function f(e){if(!window[e])return!1;var t="jsapi";try{return window[e].setItem(t,t),window[e].removeItem(t),!0}catch(r){return!1}}var c="ls_",m="ss_",g=f("localStorage"),h={_type:"",_ns:"",_callMethod:function(e,t){var r=[this._type],t=Array.prototype.slice.call(t),i=t[0];return this._ns&&r.push(this._ns),"string"==typeof i&&-1!==i.indexOf(".")&&(t.shift(),[].unshift.apply(t,i.split("."))),[].push.apply(r,t),e.apply(this,r)},get:function(){return this._callMethod(t,arguments)},set:function(){var t=arguments.length,i=arguments,n=i[0];if(1>t||!e.isPlainObject(n)&&2>t)throw Error("Minimum 2 arguments must be given or first parameter must be an object");if(e.isPlainObject(n)&&this._ns){for(var o in n)r(this._type,this._ns,o,n[o]);return n}var s=this._callMethod(r,i);return this._ns?s[n.split(".")[0]]:s},remove:function(){if(arguments.length<1)throw Error("Minimum 1 argument must be given");return this._callMethod(i,arguments)},removeAll:function(e){return this._ns?(r(this._type,this._ns,{}),!0):n(this._type,e)},isEmpty:function(){return this._callMethod(o,arguments)},isSet:function(){if(arguments.length<1)throw Error("Minimum 1 argument must be given");return this._callMethod(s,arguments)},keys:function(){return this._callMethod(a,arguments)}};if(e.cookie){window.name||(window.name=Math.floor(1e8*Math.random()));var l={_cookie:!0,_prefix:"",_expires:null,_path:null,_domain:null,setItem:function(t,r){e.cookie(this._prefix+t,r,{expires:this._expires,path:this._path,domain:this._domain})},getItem:function(t){return e.cookie(this._prefix+t)},removeItem:function(t){return e.removeCookie(this._prefix+t)},clear:function(){for(var t in e.cookie())""!=t&&(!this._prefix&&-1===t.indexOf(c)&&-1===t.indexOf(m)||this._prefix&&0===t.indexOf(this._prefix))&&e.removeCookie(t)},setExpires:function(e){return this._expires=e,this},setPath:function(e){return this._path=e,this},setDomain:function(e){return this._domain=e,this},setConf:function(e){return e.path&&(this._path=e.path),e.domain&&(this._domain=e.domain),e.expires&&(this._expires=e.expires),this},setDefaultConf:function(){this._path=this._domain=this._expires=null}};g||(window.localCookieStorage=e.extend({},l,{_prefix:c,_expires:3650}),window.sessionCookieStorage=e.extend({},l,{_prefix:m+window.name+"_"})),window.cookieStorage=e.extend({},l),e.cookieStorage=e.extend({},h,{_type:"cookieStorage",setExpires:function(e){return window.cookieStorage.setExpires(e),this},setPath:function(e){return window.cookieStorage.setPath(e),this},setDomain:function(e){return window.cookieStorage.setDomain(e),this},setConf:function(e){return window.cookieStorage.setConf(e),this},setDefaultConf:function(){return window.cookieStorage.setDefaultConf(),this}})}e.initNamespaceStorage=function(e){return u(e)},g?(e.localStorage=e.extend({},h,{_type:"localStorage"}),e.sessionStorage=e.extend({},h,{_type:"sessionStorage"})):(e.localStorage=e.extend({},h,{_type:"localCookieStorage"}),e.sessionStorage=e.extend({},h,{_type:"sessionCookieStorage"})),e.namespaceStorages={},e.removeAllStorages=function(t){e.localStorage.removeAll(t),e.sessionStorage.removeAll(t),e.cookieStorage&&e.cookieStorage.removeAll(t),t||(e.namespaceStorages={})}}); +!function(e){"function"==typeof define&&define.amd?define(["jquery", "jquery/jquery.cookie"],e):e("object"==typeof exports?require("jquery"):jQuery)}(function(e){function t(t){var r,i,n,o=arguments.length,s=window[t],a=arguments,u=a[1];if(2>o)throw Error("Minimum 2 arguments must be given");if(e.isArray(u)){i={};for(var f in u){r=u[f];try{i[r]=JSON.parse(s.getItem(r))}catch(c){i[r]=s.getItem(r)}}return i}if(2!=o){try{i=JSON.parse(s.getItem(u))}catch(c){throw new ReferenceError(u+" is not defined in this storage")}for(var f=2;o-1>f;f++)if(i=i[a[f]],void 0===i)throw new ReferenceError([].slice.call(a,1,f+1).join(".")+" is not defined in this storage");if(e.isArray(a[f])){n=i,i={};for(var m in a[f])i[a[f][m]]=n[a[f][m]];return i}return i[a[f]]}try{return JSON.parse(s.getItem(u))}catch(c){return s.getItem(u)}}function r(t){var r,i,n=arguments.length,o=window[t],s=arguments,a=s[1],u=s[2],f={};if(2>n||!e.isPlainObject(a)&&3>n)throw Error("Minimum 3 arguments must be given or second parameter must be an object");if(e.isPlainObject(a)){for(var c in a)r=a[c],e.isPlainObject(r)?o.setItem(c,JSON.stringify(r)):o.setItem(c,r);return a}if(3==n)return"object"==typeof u?o.setItem(a,JSON.stringify(u)):o.setItem(a,u),u;try{i=o.getItem(a),null!=i&&(f=JSON.parse(i))}catch(m){}i=f;for(var c=2;n-2>c;c++)r=s[c],i[r]&&e.isPlainObject(i[r])||(i[r]={}),i=i[r];return i[s[c]]=s[c+1],o.setItem(a,JSON.stringify(f)),f}function i(t){var r,i,n=arguments.length,o=window[t],s=arguments,a=s[1];if(2>n)throw Error("Minimum 2 arguments must be given");if(e.isArray(a)){for(var u in a)o.removeItem(a[u]);return!0}if(2==n)return o.removeItem(a),!0;try{r=i=JSON.parse(o.getItem(a))}catch(f){throw new ReferenceError(a+" is not defined in this storage")}for(var u=2;n-1>u;u++)if(i=i[s[u]],void 0===i)throw new ReferenceError([].slice.call(s,1,u).join(".")+" is not defined in this storage");if(e.isArray(s[u]))for(var c in s[u])delete i[s[u][c]];else delete i[s[u]];return o.setItem(a,JSON.stringify(r)),!0}function n(t,r){var n=a(t);for(var o in n)i(t,n[o]);if(r)for(var o in e.namespaceStorages)u(o)}function o(r){var i=arguments.length,n=arguments,s=(window[r],n[1]);if(1==i)return 0==a(r).length;if(e.isArray(s)){for(var u=0;ui)throw Error("Minimum 2 arguments must be given");if(e.isArray(o)){for(var a=0;a1?t.apply(this,o):n,a._cookie)for(var u in e.cookie())""!=u&&s.push(u.replace(a._prefix,""));else for(var f in a)s.push(f);return s}function u(t){if(!t||"string"!=typeof t)throw Error("First parameter must be a string");g?(window.localStorage.getItem(t)||window.localStorage.setItem(t,"{}"),window.sessionStorage.getItem(t)||window.sessionStorage.setItem(t,"{}")):(window.localCookieStorage.getItem(t)||window.localCookieStorage.setItem(t,"{}"),window.sessionCookieStorage.getItem(t)||window.sessionCookieStorage.setItem(t,"{}"));var r={localStorage:e.extend({},e.localStorage,{_ns:t}),sessionStorage:e.extend({},e.sessionStorage,{_ns:t})};return e.cookie&&(window.cookieStorage.getItem(t)||window.cookieStorage.setItem(t,"{}"),r.cookieStorage=e.extend({},e.cookieStorage,{_ns:t})),e.namespaceStorages[t]=r,r}function f(e){if(!window[e])return!1;var t="jsapi";try{return window[e].setItem(t,t),window[e].removeItem(t),!0}catch(r){return!1}}var c="ls_",m="ss_",g=f("localStorage"),h={_type:"",_ns:"",_callMethod:function(e,t){var r=[this._type],t=Array.prototype.slice.call(t),i=t[0];return this._ns&&r.push(this._ns),"string"==typeof i&&-1!==i.indexOf(".")&&(t.shift(),[].unshift.apply(t,i.split("."))),[].push.apply(r,t),e.apply(this,r)},get:function(){return this._callMethod(t,arguments)},set:function(){var t=arguments.length,i=arguments,n=i[0];if(1>t||!e.isPlainObject(n)&&2>t)throw Error("Minimum 2 arguments must be given or first parameter must be an object");if(e.isPlainObject(n)&&this._ns){for(var o in n)r(this._type,this._ns,o,n[o]);return n}var s=this._callMethod(r,i);return this._ns?s[n.split(".")[0]]:s},remove:function(){if(arguments.length<1)throw Error("Minimum 1 argument must be given");return this._callMethod(i,arguments)},removeAll:function(e){return this._ns?(r(this._type,this._ns,{}),!0):n(this._type,e)},isEmpty:function(){return this._callMethod(o,arguments)},isSet:function(){if(arguments.length<1)throw Error("Minimum 1 argument must be given");return this._callMethod(s,arguments)},keys:function(){return this._callMethod(a,arguments)}};if(e.cookie){window.name||(window.name=Math.floor(1e8*Math.random()));var l={_cookie:!0,_prefix:"",_expires:null,_path:null,_domain:null,setItem:function(t,r){e.cookie(this._prefix+t,r,{expires:this._expires,path:this._path,domain:this._domain})},getItem:function(t){return e.cookie(this._prefix+t)},removeItem:function(t){return e.removeCookie(this._prefix+t)},clear:function(){for(var t in e.cookie())""!=t&&(!this._prefix&&-1===t.indexOf(c)&&-1===t.indexOf(m)||this._prefix&&0===t.indexOf(this._prefix))&&e.removeCookie(t)},setExpires:function(e){return this._expires=e,this},setPath:function(e){return this._path=e,this},setDomain:function(e){return this._domain=e,this},setConf:function(e){return e.path&&(this._path=e.path),e.domain&&(this._domain=e.domain),e.expires&&(this._expires=e.expires),this},setDefaultConf:function(){this._path=this._domain=this._expires=null}};g||(window.localCookieStorage=e.extend({},l,{_prefix:c,_expires:3650}),window.sessionCookieStorage=e.extend({},l,{_prefix:m+window.name+"_"})),window.cookieStorage=e.extend({},l),e.cookieStorage=e.extend({},h,{_type:"cookieStorage",setExpires:function(e){return window.cookieStorage.setExpires(e),this},setPath:function(e){return window.cookieStorage.setPath(e),this},setDomain:function(e){return window.cookieStorage.setDomain(e),this},setConf:function(e){return window.cookieStorage.setConf(e),this},setDefaultConf:function(){return window.cookieStorage.setDefaultConf(),this}})}e.initNamespaceStorage=function(e){return u(e)},g?(e.localStorage=e.extend({},h,{_type:"localStorage"}),e.sessionStorage=e.extend({},h,{_type:"sessionStorage"})):(e.localStorage=e.extend({},h,{_type:"localCookieStorage"}),e.sessionStorage=e.extend({},h,{_type:"sessionCookieStorage"})),e.namespaceStorages={},e.removeAllStorages=function(t){e.localStorage.removeAll(t),e.sessionStorage.removeAll(t),e.cookieStorage&&e.cookieStorage.removeAll(t),t||(e.namespaceStorages={})}}); From cdfc6814958dc07fe47c41725b6a0715fce76309 Mon Sep 17 00:00:00 2001 From: Ihor Sviziev Date: Fri, 14 Aug 2020 10:24:27 +0300 Subject: [PATCH 06/14] Fix js unit test failures --- .../Customer/frontend/js/customer-data.test.js | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/dev/tests/js/jasmine/tests/app/code/Magento/Customer/frontend/js/customer-data.test.js b/dev/tests/js/jasmine/tests/app/code/Magento/Customer/frontend/js/customer-data.test.js index 613b70f4c4838..55340bfc6fe60 100644 --- a/dev/tests/js/jasmine/tests/app/code/Magento/Customer/frontend/js/customer-data.test.js +++ b/dev/tests/js/jasmine/tests/app/code/Magento/Customer/frontend/js/customer-data.test.js @@ -74,7 +74,16 @@ define([ originalReload = obj.reload; originalInitNamespaceStorage = $.initNamespaceStorage; spyOn(obj, 'reload').and.returnValue(true); - spyOn($, 'initNamespaceStorage').and.returnValue(true); + spyOn($, 'initNamespaceStorage').and.callFake(function (name) { + let ns = { + localStorage: {cookie: false, _ns: name}, + sessionStorage: {cookie: false, _ns: name} + }; + + $.namespaceStorages[name] = ns; + + return ns; + }); spyOn(dataProvider, 'getFromStorage'); spyOn(storage, 'keys').and.returnValue(['section']); spyOn(storageInvalidation, 'keys').and.returnValue(['section']); @@ -83,6 +92,7 @@ define([ afterEach(function () { obj.reload = originalReload; $.initNameSpaceStorage = originalInitNamespaceStorage; + $.namespaceStorages={}; }); it('Should be defined', function () { From c2e254c868fec04e6d356c25c6b963ea8e02518a Mon Sep 17 00:00:00 2001 From: Ihor Sviziev Date: Fri, 14 Aug 2020 11:40:17 +0300 Subject: [PATCH 07/14] Revert not related changes From c4900734f8608cbadd51bb1032c2a8bfad2d0870 Mon Sep 17 00:00:00 2001 From: Ihor Sviziev Date: Fri, 14 Aug 2020 13:10:50 +0300 Subject: [PATCH 08/14] Fix static test --- .../Customer/frontend/js/customer-data.test.js | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/dev/tests/js/jasmine/tests/app/code/Magento/Customer/frontend/js/customer-data.test.js b/dev/tests/js/jasmine/tests/app/code/Magento/Customer/frontend/js/customer-data.test.js index 55340bfc6fe60..3e5e274315058 100644 --- a/dev/tests/js/jasmine/tests/app/code/Magento/Customer/frontend/js/customer-data.test.js +++ b/dev/tests/js/jasmine/tests/app/code/Magento/Customer/frontend/js/customer-data.test.js @@ -76,8 +76,14 @@ define([ spyOn(obj, 'reload').and.returnValue(true); spyOn($, 'initNamespaceStorage').and.callFake(function (name) { let ns = { - localStorage: {cookie: false, _ns: name}, - sessionStorage: {cookie: false, _ns: name} + localStorage: { + cookie: false, + _ns: name + }, + sessionStorage: { + cookie: false, + _ns: name + } }; $.namespaceStorages[name] = ns; @@ -92,7 +98,7 @@ define([ afterEach(function () { obj.reload = originalReload; $.initNameSpaceStorage = originalInitNamespaceStorage; - $.namespaceStorages={}; + $.namespaceStorages = {}; }); it('Should be defined', function () { From 176013fd10f724e2a33998b02c93294ec1772a40 Mon Sep 17 00:00:00 2001 From: Ihor Sviziev Date: Fri, 14 Aug 2020 17:38:57 +0300 Subject: [PATCH 09/14] Fix static tests --- .../app/code/Magento/Customer/frontend/js/customer-data.test.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dev/tests/js/jasmine/tests/app/code/Magento/Customer/frontend/js/customer-data.test.js b/dev/tests/js/jasmine/tests/app/code/Magento/Customer/frontend/js/customer-data.test.js index 3e5e274315058..e2caa08617ef2 100644 --- a/dev/tests/js/jasmine/tests/app/code/Magento/Customer/frontend/js/customer-data.test.js +++ b/dev/tests/js/jasmine/tests/app/code/Magento/Customer/frontend/js/customer-data.test.js @@ -75,7 +75,7 @@ define([ originalInitNamespaceStorage = $.initNamespaceStorage; spyOn(obj, 'reload').and.returnValue(true); spyOn($, 'initNamespaceStorage').and.callFake(function (name) { - let ns = { + var ns = { localStorage: { cookie: false, _ns: name From 2922c1c293e41fa1d056d868c816b6f0f2e5c160 Mon Sep 17 00:00:00 2001 From: Ihor Sviziev Date: Fri, 14 Aug 2020 18:56:47 +0300 Subject: [PATCH 10/14] Fix failing unit test --- .../frontend/js/customer-data.test.js | 20 +++---------------- 1 file changed, 3 insertions(+), 17 deletions(-) diff --git a/dev/tests/js/jasmine/tests/app/code/Magento/Customer/frontend/js/customer-data.test.js b/dev/tests/js/jasmine/tests/app/code/Magento/Customer/frontend/js/customer-data.test.js index e2caa08617ef2..20be2cae41be5 100644 --- a/dev/tests/js/jasmine/tests/app/code/Magento/Customer/frontend/js/customer-data.test.js +++ b/dev/tests/js/jasmine/tests/app/code/Magento/Customer/frontend/js/customer-data.test.js @@ -16,7 +16,6 @@ define([ sectionConfig, originalGetJSON, originalReload, - originalInitNamespaceStorage, originalEach, obj; @@ -72,24 +71,12 @@ define([ beforeEach(function () { originalReload = obj.reload; - originalInitNamespaceStorage = $.initNamespaceStorage; spyOn(obj, 'reload').and.returnValue(true); - spyOn($, 'initNamespaceStorage').and.callFake(function (name) { - var ns = { - localStorage: { - cookie: false, - _ns: name - }, - sessionStorage: { - cookie: false, - _ns: name - } - }; - $.namespaceStorages[name] = ns; + //Init storage api library + $.initNamespaceStorage('mage-cache-storage').localStorage; + $.initNamespaceStorage('mage-cache-storage-section-invalidation').localStorage; - return ns; - }); spyOn(dataProvider, 'getFromStorage'); spyOn(storage, 'keys').and.returnValue(['section']); spyOn(storageInvalidation, 'keys').and.returnValue(['section']); @@ -97,7 +84,6 @@ define([ afterEach(function () { obj.reload = originalReload; - $.initNameSpaceStorage = originalInitNamespaceStorage; $.namespaceStorages = {}; }); From 3b397e34ae8e05fbf08b01395bd733f08cf9bb21 Mon Sep 17 00:00:00 2001 From: Greg Harvell Date: Thu, 27 Aug 2020 16:18:20 -0400 Subject: [PATCH 11/14] Fix for customer-data.test failing due to storage not existing yet. --- .../app/code/Magento/Customer/frontend/js/customer-data.test.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/dev/tests/js/jasmine/tests/app/code/Magento/Customer/frontend/js/customer-data.test.js b/dev/tests/js/jasmine/tests/app/code/Magento/Customer/frontend/js/customer-data.test.js index 20be2cae41be5..ae497004e59de 100644 --- a/dev/tests/js/jasmine/tests/app/code/Magento/Customer/frontend/js/customer-data.test.js +++ b/dev/tests/js/jasmine/tests/app/code/Magento/Customer/frontend/js/customer-data.test.js @@ -92,6 +92,8 @@ define([ }); it('Does not throw before component is initialized', function () { + obj.initStorage(); + expect(function () { obj.init(); }).not.toThrow(); From 5de71f6075d312d78dfe2509b412646665919d8f Mon Sep 17 00:00:00 2001 From: Greg Harvell Date: Wed, 9 Sep 2020 11:36:40 -0400 Subject: [PATCH 12/14] Adjustments to customer-data.test.js based off code review. --- .../frontend/js/customer-data.test.js | 58 ++++++++++++++----- 1 file changed, 45 insertions(+), 13 deletions(-) diff --git a/dev/tests/js/jasmine/tests/app/code/Magento/Customer/frontend/js/customer-data.test.js b/dev/tests/js/jasmine/tests/app/code/Magento/Customer/frontend/js/customer-data.test.js index e82b2261861d4..60c7bdea6a36e 100644 --- a/dev/tests/js/jasmine/tests/app/code/Magento/Customer/frontend/js/customer-data.test.js +++ b/dev/tests/js/jasmine/tests/app/code/Magento/Customer/frontend/js/customer-data.test.js @@ -18,6 +18,8 @@ define([ var injector = new Squire(), obj, + _, + originaljQuery, originalGetJSON, originalReload, originalIsEmpty, @@ -103,12 +105,14 @@ 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(); }); @@ -116,7 +120,7 @@ define([ afterEach(function () { try { - jQuery.getJSON = originalGetJSON; + $.getJSON = originalGetJSON; clearLocalStorage(); injector.clean(); injector.remove(); @@ -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 () { @@ -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(); @@ -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 () { @@ -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']); }); @@ -268,6 +287,12 @@ define([ } }); + $.getJSON = jasmine.createSpy('$.getJSON').and.callFake(function () { + var deferred = $.Deferred(); + + return deferred.promise(); + }); + init(); expect(customerData.getExpiredSectionNames()).toEqual(['cart']); }); @@ -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(); /** @@ -339,6 +367,10 @@ define([ }); }); + afterEach(function () { + $ = originaljQuery; + }); + it('Should be defined', function () { expect(obj.hasOwnProperty('reload')).toBeDefined(); }); @@ -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(); /** @@ -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({ @@ -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({ @@ -454,7 +486,7 @@ define([ result = obj.reload('*', true); - expect(jQuery.getJSON).toHaveBeenCalled(); + expect($.getJSON).toHaveBeenCalled(); expect(result).toEqual(jasmine.objectContaining({ responseJSON: { cart: {}, From fd4a2c7f1759d03f54faa228b66efc7af51dd5a2 Mon Sep 17 00:00:00 2001 From: Greg Harvell Date: Thu, 10 Sep 2020 07:31:49 -0400 Subject: [PATCH 13/14] Fixing a broken multi-shipping test. --- .../Multishipping/frontend/js/multi-shipping.test.js | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/dev/tests/js/jasmine/tests/app/code/Magento/Multishipping/frontend/js/multi-shipping.test.js b/dev/tests/js/jasmine/tests/app/code/Magento/Multishipping/frontend/js/multi-shipping.test.js index 65ee180476f3a..a8ae8ab65e378 100644 --- a/dev/tests/js/jasmine/tests/app/code/Magento/Multishipping/frontend/js/multi-shipping.test.js +++ b/dev/tests/js/jasmine/tests/app/code/Magento/Multishipping/frontend/js/multi-shipping.test.js @@ -68,9 +68,11 @@ define([ var addNewAddressBtn, addressflag, canContinueBtn, - canContinueFlag; + canContinueFlag, + originalGetJSON; beforeEach(function () { + originalGetJSON = $.getJSON; addNewAddressBtn = $('