From 8047497701d5a332ad692844f12af11f344d9854 Mon Sep 17 00:00:00 2001 From: Greg Harvell Date: Fri, 24 Jul 2020 10:41:48 -0400 Subject: [PATCH] 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: {