diff --git a/app/code/Magento/Ui/view/base/web/js/grid/data-storage.js b/app/code/Magento/Ui/view/base/web/js/grid/data-storage.js index 547cdab16cdf1..aa9989251b656 100644 --- a/app/code/Magento/Ui/view/base/web/js/grid/data-storage.js +++ b/app/code/Magento/Ui/view/base/web/js/grid/data-storage.js @@ -188,7 +188,8 @@ define([ result = { items: this.getByIds(request.ids), - totalRecords: request.totalRecords + totalRecords: request.totalRecords, + errorMessage: request.errorMessage }; delay ? @@ -216,7 +217,8 @@ define([ this._requests.push({ ids: this.getIds(data.items), params: params, - totalRecords: data.totalRecords + totalRecords: data.totalRecords, + errorMessage: data.errorMessage }); return this; diff --git a/dev/tests/js/jasmine/tests/app/code/Magento/Ui/base/js/grid/data-storage.test.js b/dev/tests/js/jasmine/tests/app/code/Magento/Ui/base/js/grid/data-storage.test.js index 5f8bfc2c98cc2..b53f49bd6103d 100644 --- a/dev/tests/js/jasmine/tests/app/code/Magento/Ui/base/js/grid/data-storage.test.js +++ b/dev/tests/js/jasmine/tests/app/code/Magento/Ui/base/js/grid/data-storage.test.js @@ -6,63 +6,190 @@ /*eslint max-nested-callbacks: 0*/ /*jscs:disable requireCamelCaseOrUpperCaseIdentifiers*/ define([ - 'mageUtils', + 'jquery', 'Magento_Ui/js/grid/data-storage' -], function (utils, DataStorage) { +], function ($, DataStorage) { 'use strict'; describe('Magento_Ui/js/grid/data-storage', function () { - describe('costructor', function () { - it('converts dataScope property to array', function () { + + describe('"initConfig" method', function () { + + it('returns self', function () { var model = new DataStorage({ dataScope: 'magento' }); - expect(model.dataScope).toEqual(['magento']); + expect(model.initConfig()).toEqual(model); }); - }); - describe('hasScopeChanged', function () { - it('is function', function () { + it('changes string dataScope property to an array', function () { var model = new DataStorage({ - dataScope: '' + dataScope: 'magento' }); - expect(model.hasScopeChanged).toBeDefined(); - expect(typeof model.hasScopeChanged).toEqual('function'); + expect(model.dataScope).toEqual(['magento']); }); - it('returns false if no requests have been made', function () { + it('changes empty string dataScope property to an empty array', function () { var model = new DataStorage({ dataScope: '' }); - expect(model.hasScopeChanged()).toBeFalsy(); + expect(model.dataScope).toEqual([]); }); - it('tells whether parameters defined in the dataScope property have changed', function () { - var params, newParams, model; + it('doesn\'t change non-string dataScope property', function () { + var testScope = { + testKey: 'test value' + }, + model = new DataStorage({ + dataScope: testScope + }); - params = { - namespace: 'magento', - search: '', - filters: { - store_id: 0 + expect(model.dataScope).toEqual(testScope); + }); + + it('initializes _requests property as an empty array', function () { + var model = new DataStorage(); + + model._requests = null; + model.initConfig(); + expect(model._requests).toEqual([]); + }); + }); + + describe('"getByIds" method', function () { + + it('returns false if data for ids is missing', function () { + var model = new DataStorage(); + + expect(model.getByIds([1,2,3])).toEqual(false); + }); + + it('returns array of items', function () { + var item = { + id_field_name: 'entity_id', + entity_id: '1' }, - sorting: {}, - paging: {} - }; + model = new DataStorage({ + data: { + 1: item + } + }); - newParams = utils.extend({}, params, { - search: 'magento', - filters: { - store_id: 1 - } - }); + expect(model.getByIds([1])).toEqual([item]); + }); - model = new DataStorage({ - dataScope: 'filters.store_id' - }); + }); + + describe('"getIds" method', function () { + + it('returns an array of entity_id\'s from provided data', function () { + var model = new DataStorage(), + ids = [ + { + id_field_name: 'entity_id', + entity_id: '1' + }, + { + id_field_name: 'entity_id', + entity_id: '54' + } + ]; + + expect(model.getIds(ids)).toEqual(['1', '54']); + }); + + it('returns an array of entity_id\'s from stored data if no arguments provided', function () { + var model = new DataStorage({ + data: { + 1: { + id_field_name: 'entity_id', + entity_id: '1' + }, + 2: { + id_field_name: 'entity_id', + entity_id: '42' + } + } + }); + + expect(model.getIds()).toEqual(['1', '42']); + }); + + }); + + describe('"getData" method', function () { + + var model = new DataStorage(); + + it('returns the result of requestData method if scope have been changed', function () { + var requestDataResult = 'requestDataResult'; + + spyOn(model, 'clearRequests'); + spyOn(model, 'hasScopeChanged').and.returnValue(true); + spyOn(model, 'requestData').and.returnValue(requestDataResult); + spyOn(model, 'getRequest'); + expect(model.getData()).toEqual(requestDataResult); + expect(model.clearRequests).toHaveBeenCalled(); + expect(model.getRequest).not.toHaveBeenCalled(); + }); + + it('returns the cached result if scope have not been changed', function () { + var cachedRequestDataResult = 'cachedRequestDataResult'; + + spyOn(model, 'clearRequests'); + spyOn(model, 'requestData'); + spyOn(model, 'hasScopeChanged').and.returnValue(false); + spyOn(model, 'getRequest').and.returnValue(true); + spyOn(model, 'getRequestData').and.returnValue(cachedRequestDataResult); + + expect(model.getData()).toEqual(cachedRequestDataResult); + expect(model.clearRequests).not.toHaveBeenCalled(); + expect(model.requestData).not.toHaveBeenCalled(); + }); + + it('returns the result of requestData method if refresh option is provided', function () { + var requestDataResult = 'requestDataResult', + options = { + refresh: true + }; + + spyOn(model, 'getRequest').and.returnValue(true); + spyOn(model, 'clearRequests'); + spyOn(model, 'hasScopeChanged').and.returnValue(true); + spyOn(model, 'requestData').and.returnValue(requestDataResult); + expect(model.getData({}, options)).toEqual(requestDataResult); + expect(model.clearRequests).toHaveBeenCalled(); + }); + + }); + + describe('"hasScopeChanged" method', function () { + + it('returns false if no requests have been made', function () { + var model = new DataStorage(); + + expect(model.hasScopeChanged()).toBeFalsy(); + }); + + it('returns true for not cached params', function () { + var params = { + search: '1', + filters: { + store_id: 0 + } + }, + newParams = { + search: '2', + filters: { + store_id: 1 + } + }, + model = new DataStorage({ + dataScope: 'filters.store_id' + }); model.cacheRequest({ totalRecords: 0 @@ -72,5 +199,303 @@ define([ expect(model.hasScopeChanged(newParams)).toBeTruthy(); }); }); + + describe('"updateData" method', function () { + var model = new DataStorage({ + dataScope: 'magento', + requestConfig: { + url: 'magento.com', + method: 'GET', + dataType: 'json' + }, + data: { + 1: { + id_field_name: 'entity_id', + entity_id: '1', + field: 'value' + } + } + }); + + it('updates data items', function () { + var data = [{ + id_field_name: 'entity_id', + entity_id: '1', + field: 'updatedValue' + }]; + + expect(model.updateData(data)).toEqual(model); + expect(model.getByIds([1])).toEqual(data); + }); + }); + + describe('"requestData" method', function () { + var model = new DataStorage(); + + it('Check Ajax request', function () { + var result = 'result'; + + spyOn(model, 'onRequestComplete').and.returnValue(result); + spyOn($, 'ajax').and.returnValue({ + /** + * Success result for ajax request + * + * @param {Function} handler + * @returns {*} + */ + done: function (handler) { + return handler(); + } + }); + expect(model.requestData({})).toEqual(result); + }); + }); + + describe('"getRequest" method', function () { + var model = new DataStorage({ + dataScope: 'magento' + }); + + it('returns cached request', function () { + var params = { + namespace: 'magento', + search: '', + sorting: {}, + paging: {} + }, + request = { + ids: ['1'], + params: params, + totalRecords: 1, + errorMessage: '' + }; + + model._requests.push(request); + expect(model.getRequest(params)).toEqual(request); + }); + }); + + describe('"getRequestData" method', function () { + it('returns request data', function () { + var request = { + ids: [1,2], + totalRecords: 2, + errorMessage: '' + }, + items = [ + { + id_field_name: 'entity_id', + entity_id: '1' + }, + { + id_field_name: 'entity_id', + entity_id: '2' + } + ], + result = { + items: items, + totalRecords: 2, + errorMessage: '' + }, + model = new DataStorage({ + cachedRequestDelay: 0 + }); + + spyOn(model, 'getByIds').and.returnValue(items); + model.getRequestData(request).then(function (promiseResult) { + expect(promiseResult).toEqual(result); + }); + }); + }); + + describe('"cacheRequest" method', function () { + var model = new DataStorage({ + dataScope: 'magento' + }); + + it('adds the request to the cache', function () { + var params = { + namespace: 'magento', + search: '', + sorting: {}, + paging: {} + }, + ids = ['1','2','3'], + data = { + items: ids, + totalRecords: 3, + errorMessage: '' + }, + request = { + ids: ids, + params: params, + totalRecords: 3, + errorMessage: '' + }; + + spyOn(model, 'removeRequest'); + spyOn(model, 'getIds').and.returnValue(ids); + model.cacheRequest(data, params); + expect(model.getRequest(params)).toEqual(request); + expect(model.removeRequest).not.toHaveBeenCalled(); + }); + + it('overwrites the previously cached request for the same params', function () { + var params = { + namespace: 'magento', + search: '', + sorting: {}, + paging: {} + }, + ids = ['1','2','3'], + firstData = { + items: ids, + totalRecords: 3, + errorMessage: '' + }, + secondData = { + items: ids, + totalRecords: 3, + errorMessage: 'Error message' + }, + firstRequest = { + ids: ids, + params: params, + totalRecords: 3, + errorMessage: '' + }, + secondRequest = { + ids: ids, + params: params, + totalRecords: 3, + errorMessage: 'Error message' + }; + + spyOn(model, 'getIds').and.returnValue(ids); + model.cacheRequest(firstData, params); + expect(model.getRequest(params)).toEqual(firstRequest); + model.cacheRequest(secondData, params); + expect(model.getRequest(params)).toEqual(secondRequest); + }); + }); + + describe('"clearRequests" method', function () { + + it('removes all cached requests', function () { + var model = new DataStorage(), + params = { + namespace: 'magento', + search: 'magento', + filters: { + store_id: 1 + } + }; + + model._requests.push({ + ids: ['1','2','3','4'], + params: params, + totalRecords: 4, + errorMessage: 'errorMessage' + }); + model.clearRequests(); + expect(model._requests).toEqual([]); + }); + }); + + describe('"removeRequest" method', function () { + + var model = new DataStorage(); + + it('removes the request from the cache', function () { + var params = { + namespace: 'magento', + search: '', + sorting: {}, + paging: {} + }, + request = { + ids: ['1','2','3'], + params: params, + totalRecords: 3, + errorMessage: '' + }; + + model._requests = [request]; + expect(model.getRequest(params)).toEqual(request); + model.removeRequest(request); + expect(model.getRequest(params)).toBeFalsy(); + }); + }); + + describe('"wasRequested" method', function () { + var model = new DataStorage({ + dataScope: 'magento' + }); + + it('returns false if request is not present in cache', function () { + var params = { + namespace: 'magento', + search: '', + sorting: {}, + paging: {} + }; + + expect(model.wasRequested(params)).toBeFalsy(); + }); + + it('returns true if request is present in cache', function () { + var params = { + namespace: 'magento', + search: '', + sorting: {}, + paging: {} + }, + request = { + ids: ['1','2','3'], + params: params, + totalRecords: 3, + errorMessage: '' + }; + + model._requests = [request]; + + expect(model.wasRequested(params)).toBeTruthy(); + }); + }); + + describe('"onRequestComplete" method', function () { + + it('updates data and does not cache the request if caching is disabled', function () { + var model = new DataStorage({ + cacheRequests: false + }), + data = { + items: [] + }, + params = {}; + + spyOn(model, 'updateData'); + spyOn(model, 'cacheRequest'); + model.onRequestComplete(params, data); + expect(model.updateData).toHaveBeenCalled(); + expect(model.cacheRequest).not.toHaveBeenCalled(); + }); + + it('updates data and adds the request to cache if caching is enabled', function () { + var model = new DataStorage({ + cacheRequests: true + }), + data = { + items: [] + }, + params = {}; + + spyOn(model, 'updateData'); + spyOn(model, 'cacheRequest'); + model.onRequestComplete(params, data); + expect(model.updateData).toHaveBeenCalled(); + expect(model.cacheRequest).toHaveBeenCalled(); + }); + }); }); });