Skip to content
This repository has been archived by the owner on Apr 29, 2019. It is now read-only.

Commit

Permalink
Merge pull request #3230 from magento-trigger/MC-4277
Browse files Browse the repository at this point in the history
[trigger] MC-4277: Address Product Page slowness due to 'product_data_storage' handling
  • Loading branch information
Joan He authored Oct 5, 2018
2 parents 32219a9 + 8e5d2a0 commit 3001311
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,21 @@ define([
};
}

/**
* Set data to localStorage with support check.
*
* @param {String} namespace
* @param {Object} data
*/
function setLocalStorageItem(namespace, data) {
try {
window.localStorage.setItem(namespace, JSON.stringify(data));
} catch (e) {
console.warn('localStorage is unavailable - skipping local caching of product data');
console.error(e);
}
}

return {

/**
Expand Down Expand Up @@ -118,7 +133,7 @@ define([
if (_.isEmpty(data)) {
this.localStorage.removeAll();
} else {
this.localStorage.set(data);
setLocalStorageItem(this.namespace, data);
}
},

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,21 @@ define([
], function ($, _, ko, utils) {
'use strict';

/**
* Set data to localStorage with support check.
*
* @param {String} namespace
* @param {Object} data
*/
function setLocalStorageItem(namespace, data) {
try {
window.localStorage.setItem(namespace, JSON.stringify(data));
} catch (e) {
console.warn('localStorage is unavailable - skipping local caching of product data');
console.error(e);
}
}

return {

/**
Expand Down Expand Up @@ -94,11 +109,7 @@ define([
* Initializes handler to "data" property update
*/
internalDataHandler: function (data) {
var localStorage = this.localStorage.get();

if (!utils.compare(data, localStorage).equal) {
this.localStorage.set(data);
}
setLocalStorageItem(this.namespace, data);
},

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ define([
* @param {*} data
*/
add: function (data) {
if (!_.isEmpty(data) && !utils.compare(data, this.data()).equal) {
if (!_.isEmpty(data)) {
this.data(_.extend(utils.copy(this.data()), data));
}
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ define([
injector.clean();
injector.remove();
} catch (e) {}
window.localStorage.clear();
});

describe('Magento_Catalog/js/product/storage/data-storage', function () {
Expand Down Expand Up @@ -88,18 +89,20 @@ define([
};
});

afterEach(function () {
window.localStorage.clear();
});

it('check calls "dataHandler" method with data', function () {
var data = {
property: 'value'
};

obj.dataHandler(data);
expect(obj.localStorage.set).toHaveBeenCalledWith(data);
expect(obj.localStorage.removeAll).not.toHaveBeenCalled();
expect(window.localStorage.getItem(obj.namespace)).toBe(JSON.stringify(data));
});
it('check calls "dataHandler" method with empty data', function () {
obj.dataHandler({});
expect(obj.localStorage.set).not.toHaveBeenCalled();
expect(obj.localStorage.removeAll).toHaveBeenCalled();
});
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ define([
injector.clean();
injector.remove();
} catch (e) {}
window.localStorage.clear();
});

describe('Magento_Catalog/js/product/storage/ids-storage', function () {
Expand All @@ -40,13 +41,6 @@ define([
expect(obj.localStorage.get).toHaveBeenCalled();
});
});
describe('"cachesDataFromLocalStorage" method', function () {
it('check calls localStorage get method', function () {
obj.getDataFromLocalStorage = jasmine.createSpy().and.returnValue({});

expect(obj.localStorage.get).toHaveBeenCalled();
});
});
describe('"initLocalStorage" method', function () {
it('check returned value', function () {
obj.namespace = 'test';
Expand Down Expand Up @@ -80,17 +74,16 @@ define([
it('check calls with data that equal with data in localStorage', function () {
obj.internalDataHandler(data);

expect(obj.localStorage.get).toHaveBeenCalled();
expect(obj.localStorage.set).not.toHaveBeenCalled();
expect(window.localStorage.getItem(obj.namespace)).toBe(JSON.stringify(data));
});

it('check calls with data that not equal with data in localStorage', function () {
var emptyData = {};

obj.internalDataHandler(data);
obj.internalDataHandler(emptyData);

expect(obj.localStorage.get).toHaveBeenCalled();
expect(obj.localStorage.set).toHaveBeenCalledWith(emptyData);
expect(window.localStorage.getItem(obj.namespace)).toBe(JSON.stringify(emptyData));
});
});
describe('"externalDataHandler" method', function () {
Expand Down

0 comments on commit 3001311

Please sign in to comment.