From c9d5e8ef8e3b12f6483ec02e2bf561c2752b68d1 Mon Sep 17 00:00:00 2001 From: konarshankar07 Date: Wed, 22 Jan 2020 22:46:44 +0530 Subject: [PATCH 01/10] Error message to becached for grid data storage component --- app/code/Magento/Ui/view/base/web/js/grid/data-storage.js | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) 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; From 8357cf80e7cde248c091bf7fef6934c40e47400a Mon Sep 17 00:00:00 2001 From: konarshankar07 Date: Fri, 24 Jan 2020 01:11:38 +0530 Subject: [PATCH 02/10] Jasmine test added for initConfig, getByIds, getRequestData, cacheRequest --- .../Ui/base/js/grid/data-storage.test.js | 125 +++++++++++++++++- 1 file changed, 124 insertions(+), 1 deletion(-) 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..454fc1280c548 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 @@ -12,7 +12,8 @@ define([ 'use strict'; describe('Magento_Ui/js/grid/data-storage', function () { - describe('costructor', function () { + + describe('constructor', function () { it('converts dataScope property to array', function () { var model = new DataStorage({ dataScope: 'magento' @@ -22,6 +23,77 @@ define([ }); }); + describe('"initConfig" method', function () { + + var model = new DataStorage({ + dataScope: 'magento' + }); + + it('Check for defined ', function () { + expect(model.hasOwnProperty('initConfig')).toBeDefined(); + }); + + it('Check method type', function () { + var type = typeof model.initConfig; + + expect(type).toEqual('function'); + }); + + it('Check returned value if method called without arguments', function () { + expect(model.initConfig()).toBeDefined(); + }); + + it('Check returned value type if method called without arguments', function () { + var type = typeof model.initConfig(); + + expect(type).toEqual('object'); + }); + + it('Check this.dataScope property (is modify in initConfig method)', function () { + model.dataScope = null; + model.initConfig(); + expect(typeof model.dataScope).toEqual('object'); + }); + + it('Check this._requests property (is modify in initConfig method)', function () { + model._requests = null; + model.initConfig(); + expect(typeof model._requests).toEqual('object'); + }); + }); + + describe('"getByIds"', function() { + + var model = new DataStorage({ + dataScope: 'magento' + }); + + it('check for defined', function() { + expect(model.hasOwnProperty('getByIds')).toBeDefined(); + }); + + it('check method type', function () { + expect(typeof model.getByIds).toEqual('function'); + }); + + it('Check returned value if method called with argument', function () { + var ids = [1,2,3]; + expect(model.getByIds(ids)).toBeDefined(); + }); + + it('check returned false if method called with argument', function() { + var ids = [1,2,3]; + var type = typeof model.getByIds(ids); + expect(type).toEqual('boolean'); + }); + + it('Return false', function() { + var ids = [1,2,3]; + expect(model.getByIds(ids)).toEqual('false'); + }); + + }); + describe('hasScopeChanged', function () { it('is function', function () { var model = new DataStorage({ @@ -72,5 +144,56 @@ define([ expect(model.hasScopeChanged(newParams)).toBeTruthy(); }); }); + describe('"getRequestData" method', function () { + var model = new DataStorage({ + dataScope: 'magento' + }); + it('Check for defined ', function () { + expect(model.hasOwnProperty('getRequestData')).toBeDefined(); + }); + + it('Check method type', function () { + var type = typeof model.getRequestData; + + expect(type).toEqual('function'); + }); + + it('check "getRequestData" has been executed', function () { + var request = { + ids: [1,2,3] + }; + expect(model.getRequestData(request)).toBeTruthy(); + }); + }); + + describe('"cacheRequest" method', function () { + var model = new DataStorage({ + dataScope: 'magento' + }); + it('Check for defined ', function () { + expect(model.hasOwnProperty('cacheRequest')).toBeDefined(); + }); + + it('Check method type', function () { + var type = typeof model.cacheRequest; + + expect(type).toEqual('function'); + }); + + it('check "cacheRequest" has been executed', function () { + var data = { + items: [1,2,3], + totalRecords: 3, + errorMessage: '' + }, + params = { + namespace: 'magento', + search: '', + sorting: {}, + paging: {} + }; + expect(model.cacheRequest(data, params)).toBeTruthy(); + }); + }); }); }); From a17c2a51d226f810ddc5bf32a7b28b23b6b59ea2 Mon Sep 17 00:00:00 2001 From: konarshankar07 Date: Mon, 27 Jan 2020 01:10:23 +0530 Subject: [PATCH 03/10] More jasmine tests added --- .../Ui/base/js/grid/data-storage.test.js | 434 +++++++++++++++++- 1 file changed, 418 insertions(+), 16 deletions(-) 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 454fc1280c548..f595020673ed7 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 @@ -62,13 +62,13 @@ define([ }); }); - describe('"getByIds"', function() { + describe('"getByIds" method', function () { var model = new DataStorage({ dataScope: 'magento' }); - it('check for defined', function() { + it('check for defined', function () { expect(model.hasOwnProperty('getByIds')).toBeDefined(); }); @@ -78,23 +78,198 @@ define([ it('Check returned value if method called with argument', function () { var ids = [1,2,3]; + expect(model.getByIds(ids)).toBeDefined(); }); - it('check returned false if method called with argument', function() { - var ids = [1,2,3]; - var type = typeof model.getByIds(ids); + it('check returned type if method called with argument', function () { + var ids = [1,2,3], + type = typeof model.getByIds(ids); + expect(type).toEqual('boolean'); }); - it('Return false', function() { + it('Return false if "getByIds" has been called', function () { + var ids = [1,2,3]; + + expect(model.getByIds(ids)).toEqual(false); + }); + + it('Return array if "getByIds" has been called', function () { + var ids = [1]; + + model = new DataStorage({ + dataScope: 'magento', + data: { + 1: { + id_field_name: 'entity_id', + entity_id: '1' + } + } + }); + + expect(typeof model.getByIds(ids)).toEqual('object'); + }); + + }); + + describe('"getIds" method', function () { + + var model = new DataStorage({ + dataScope: 'magento' + }); + + it('check for defined', function () { + expect(model.hasOwnProperty('getIds')).toBeDefined(); + }); + + it('check method type', function () { + expect(typeof model.getIds).toEqual('function'); + }); + + it('check returned value if method called with argument', function () { var ids = [1,2,3]; - expect(model.getByIds(ids)).toEqual('false'); + + expect(model.getIds(ids)).toBeDefined(); + }); + + it('check returned type if method called with argument', function () { + var ids = [1,2,3], + type = typeof model.getIds(ids); + + expect(type).toEqual('object'); }); }); - describe('hasScopeChanged', function () { + describe('"getData" method', function () { + + var model = new DataStorage({ + dataScope: 'magento' + }); + + it('check for defined', function () { + expect(model.hasOwnProperty('getData')).toBeDefined(); + }); + + it('check method type', function () { + expect(typeof model.getData).toEqual('function'); + }); + + it('check returned value if method called with argument', function () { + var params = { + namespace: 'magento', + search: '', + filters: { + store_id: 0 + }, + sorting: {}, + paging: {} + }; + + expect(model.getData(params)).toBeDefined(); + }); + + it('check returned type if method called with argument', function () { + var params = { + namespace: 'magento', + search: '', + filters: { + store_id: 0 + }, + sorting: {}, + paging: {} + }, + type = typeof model.getData(params); + + expect(type).toEqual('object'); + }); + + it('check "clearRequests" has been called', function () { + var params = { + namespace: 'magento', + search: '', + filters: { + store_id: 0 + }, + sorting: {}, + paging: {} + }; + + spyOn(model, 'clearRequests'); + spyOn(model, 'hasScopeChanged').and.callFake(function () { + return true; + }); + model.getData(params); + expect(model.clearRequests).toHaveBeenCalled(); + }); + + it('check "getRequest" has been called', function () { + var params = { + namespace: 'magento', + search: '', + filters: { + store_id: 0 + }, + sorting: {}, + paging: {} + }; + + spyOn(model, 'getRequest'); + spyOn(model, 'hasScopeChanged').and.callFake(function () { + return false; + }); + model.getData(params); + expect(model.getRequest).toHaveBeenCalled(); + }); + + it('Return "getRequestData" method', function () { + var params = { + namespace: 'magento', + search: '', + filters: { + store_id: 0 + }, + sorting: {}, + paging: {} + }, + options = { + refresh: false + }; + + spyOn(model, 'getRequestData'); + spyOn(model, 'getRequest').and.callFake(function () { + return true; + }); + model.getData(params, options); + expect(model.getRequestData).toHaveBeenCalled(); + }); + + it('Return "requestData" method', function () { + var params = { + namespace: 'magento', + search: '', + filters: { + store_id: 0 + }, + sorting: {}, + paging: {} + }, + options = { + refresh: true + }; + + spyOn(model, 'requestData'); + spyOn(model, 'getRequest').and.callFake(function () { + return false; + }); + model.getData(params, options); + expect(model.requestData).toHaveBeenCalled(); + }); + + }); + + describe('"hasScopeChanged" method', function () { it('is function', function () { var model = new DataStorage({ dataScope: '' @@ -144,10 +319,37 @@ define([ expect(model.hasScopeChanged(newParams)).toBeTruthy(); }); }); + + describe('"updateData" method', function () { + var model = new DataStorage({ + dataScope: 'magento' + }); + + it('Check for defined ', function () { + expect(model.hasOwnProperty('updateData')).toBeDefined(); + }); + + it('Check method type', function () { + var type = typeof model.updateData; + + expect(type).toEqual('function'); + }); + + it('Check updateData has been called', function () { + var data = [{ + id_field_name: 'entity_id', + entity_id: '1' + }]; + + expect(model.updateData(data)).toBeTruthy(); + }); + }); + describe('"getRequestData" method', function () { var model = new DataStorage({ dataScope: 'magento' }); + it('Check for defined ', function () { expect(model.hasOwnProperty('getRequestData')).toBeDefined(); }); @@ -162,6 +364,7 @@ define([ var request = { ids: [1,2,3] }; + expect(model.getRequestData(request)).toBeTruthy(); }); }); @@ -170,6 +373,7 @@ define([ var model = new DataStorage({ dataScope: 'magento' }); + it('Check for defined ', function () { expect(model.hasOwnProperty('cacheRequest')).toBeDefined(); }); @@ -180,19 +384,217 @@ define([ expect(type).toEqual('function'); }); - it('check "cacheRequest" has been executed', function () { - var data = { - items: [1,2,3], - totalRecords: 3, - errorMessage: '' - }, - params = { + it('check "model._requests"', function () { + var params = { + namespace: 'magento', + search: '', + sorting: {}, + paging: {} + }, + data = { + items: ['1','2','3'], + totalRecords: 3 + }; + + spyOn(model, 'removeRequest'); + spyOn(model, 'getIds').and.callFake(function () { + return ['1','2','3']; + }); + model.cacheRequest(data, params); + expect(typeof model._requests).toEqual('object'); + expect(model.getIds).toHaveBeenCalled(); + expect(model.removeRequest).not.toHaveBeenCalled(); + }); + + it('check "removeRequest" is executed', function () { + var params = { namespace: 'magento', search: '', sorting: {}, paging: {} + }, + data = { + items: ['1','2','3'], + totalRecords: 3 }; - expect(model.cacheRequest(data, params)).toBeTruthy(); + + spyOn(model, 'removeRequest'); + spyOn(model, 'getRequest').and.callFake(function () { + return true; + }); + spyOn(model, 'getIds').and.callFake(function () { + return ['1','2','3']; + }); + model.cacheRequest(data, params); + expect(typeof model._requests).toEqual('object'); + expect(model.getIds).toHaveBeenCalled(); + expect(model.removeRequest).toHaveBeenCalled(); + }); + }); + + describe('"clearRequests" method', function () { + + var model = new DataStorage({ + dataScope: 'magento' + }); + + it('Check for defined ', function () { + expect(model.hasOwnProperty('clearRequests')).toBeDefined(); + }); + + it('Check method type', function () { + var type = typeof model.clearRequests; + + expect(type).toEqual('function'); + }); + + it('check "clearRequests" will empty _requests array', function () { + var params = { + namespace: 'magento', + search: 'magento', + filters: { + store_id: 1 + } + }; + + model = new DataStorage({ + dataScope: 'magento', + _requests: [] + }); + + 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({ + dataScope: 'magento' + }); + + it('Check for defined ', function () { + expect(model.hasOwnProperty('removeRequest')).toBeDefined(); + }); + + it('Check method type', function () { + var type = typeof model.removeRequest; + + expect(type).toEqual('function'); + }); + + it('check "removeRequest" is defined', function () { + var params = { + namespace: 'magento', + search: 'magento', + filters: { + store_id: 1 + } + }, + request = [{ + ids: [1,2,3], + params: params, + totalRecords: 3, + errorMessage: 'errorMessage' + }]; + + expect(model.removeRequest(request)).toBeDefined(); + }); + }); + + describe('"wasRequested" method', function () { + var model = new DataStorage({ + dataScope: 'magento' + }); + + it('Check for defined ', function () { + expect(model.hasOwnProperty('wasRequested')).toBeDefined(); + }); + + it('Check method type', function () { + var type = typeof model.wasRequested; + + expect(type).toEqual('function'); + }); + + it('Return false if getRequest method returns false', function () { + var params = { + namespace: 'magento', + search: '', + sorting: {}, + paging: {} + }; + + model.wasRequested(params); + expect(model.wasRequested(params)).toBeFalsy(); + }); + }); + + describe('"onRequestComplete" method', function () { + var model = new DataStorage({ + dataScope: 'magento' + }); + + it('Check for defined ', function () { + expect(model.hasOwnProperty('onRequestComplete')).toBeDefined(); + }); + + it('Check method type', function () { + var type = typeof model.onRequestComplete; + + expect(type).toEqual('function'); + }); + + it('Check "updateData" method has been called', function () { + var data = { + items: [{ + id_field_name: 'entity_id', + entity_id: '1' + }] + }, +params = { + namespace: 'magento', + search: '', + sorting: {}, + paging: {} + }; + + spyOn(model, 'updateData').and.callFake(function () { + return data; + }); + model.onRequestComplete(params, data); + expect(model.updateData).toHaveBeenCalled(); + }); + + it('Check "cacheRequest" method has been called', function () { + var data = { + items: [{ + id_field_name: 'entity_id', + entity_id: '1' + }] + }, + params = { + namespace: 'magento', + search: '', + sorting: {}, + paging: {} + }; + + model = new DataStorage({ + dataScope: 'magento', + cacheRequests: true + }); + spyOn(model, 'cacheRequest').and.callFake(function () { + return data; + }); + model.onRequestComplete(params, data); + expect(model.cacheRequest).toHaveBeenCalled(); }); }); }); From 7fafb2d13517705d86ca0c65cfa52d52bd1c5c40 Mon Sep 17 00:00:00 2001 From: konarshankar07 Date: Sat, 1 Feb 2020 17:41:26 +0530 Subject: [PATCH 04/10] More method cover with jasmine tests --- .../Ui/base/js/grid/data-storage.test.js | 133 +++++++++++++++++- 1 file changed, 129 insertions(+), 4 deletions(-) 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 f595020673ed7..163466ebe9175 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,9 +6,11 @@ /*eslint max-nested-callbacks: 0*/ /*jscs:disable requireCamelCaseOrUpperCaseIdentifiers*/ define([ + 'jquery', 'mageUtils', + 'underscore', 'Magento_Ui/js/grid/data-storage' -], function (utils, DataStorage) { +], function ($, utils, _, DataStorage) { 'use strict'; describe('Magento_Ui/js/grid/data-storage', function () { @@ -322,7 +324,12 @@ define([ describe('"updateData" method', function () { var model = new DataStorage({ - dataScope: 'magento' + dataScope: 'magento', + requestConfig: { + url: 'magento.com', + method: 'GET', + dataType: 'json' + } }); it('Check for defined ', function () { @@ -345,6 +352,83 @@ define([ }); }); + describe('"requestData" method', function () { + var model = new DataStorage({ + dataScope: 'magento' + }); + + it('Check for defined', function () { + expect(model.hasOwnProperty('requestData')).toBeDefined(); + }); + + it('Check method type', function () { + var type = typeof model.requestData; + + expect(type).toEqual('function'); + }); + + it('Check Ajax request', function () { + var params = { + namespace: 'magento', + search: '', + filters: { + store_id: 0 + }, + sorting: {}, + paging: {} + }, + query = utils.copy(params); + + spyOn(model, 'onRequestComplete'); + spyOn($, 'ajax').and.callFake(function () { + return { + /** + * Success result for ajax request + */ + done: function () { + model.onRequestComplete(model, query); + } + }; + }); + model.requestData(params); + expect($.ajax).toHaveBeenCalled(); + expect(model.onRequestComplete).toHaveBeenCalled(); + }); + }); + + describe('"getRequest" method', function () { + var model = new DataStorage({ + dataScope: 'magento' + }); + + it('Check for defined', function () { + expect(model.hasOwnProperty('getRequest')).toBeDefined(); + }); + + it('Check method', function () { + var type = typeof model.getRequest; + + expect(type).toEqual('function'); + }); + + it('check "getRequest" has been executed', function () { + var params = { + namespace: 'magento', + search: '', + sorting: {}, + paging: {} + }; + + model._requests.push({ + ids: ['1'], + params: params, + totalRecords: 1, + errorMessage: '' + }); + expect(model.getRequest(params)).toBeTruthy(); + }); + }); + describe('"getRequestData" method', function () { var model = new DataStorage({ dataScope: 'magento' @@ -367,6 +451,45 @@ define([ expect(model.getRequestData(request)).toBeTruthy(); }); + + it('check "getByIds" has been executed', function () { + var request = { + ids: [1,2,3] + }; + + spyOn(model, 'getByIds'); + model.getRequestData(request); + expect(model.getByIds).toHaveBeenCalled(); + }); + + it('check "delay" function has been executed', function () { + var request = { + ids: [1,2,3], + totalRecords: 3, + errorMessage: '' + }; + + spyOn(_, 'delay'); + model.getRequestData(request); + expect(_.delay).toHaveBeenCalled(); + }); + + it('check "delay" function has not been executed', function () { + var request = { + ids: [1,2,3], + totalRecords: 3, + errorMessage: '' + }; + + model = new DataStorage({ + dataScope: 'magento', + cachedRequestDelay: 0 + }); + + spyOn(_, 'delay'); + model.getRequestData(request); + expect(_.delay).not.toHaveBeenCalled(); + }); }); describe('"cacheRequest" method', function () { @@ -531,7 +654,9 @@ define([ paging: {} }; - model.wasRequested(params); + spyOn(model, 'getRequest').and.callFake(function () { + return false; + }); expect(model.wasRequested(params)).toBeFalsy(); }); }); @@ -558,7 +683,7 @@ define([ entity_id: '1' }] }, -params = { + params = { namespace: 'magento', search: '', sorting: {}, From 9de1e591300d8b77a7aeeffda2c3c01ab4763e55 Mon Sep 17 00:00:00 2001 From: konarshankar07 Date: Sat, 1 Feb 2020 23:27:22 +0530 Subject: [PATCH 05/10] Fixed static tests --- .../code/Magento/Ui/base/js/grid/data-storage.test.js | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) 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 163466ebe9175..02d303782a9d5 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 @@ -480,12 +480,10 @@ define([ totalRecords: 3, errorMessage: '' }; - - model = new DataStorage({ - dataScope: 'magento', - cachedRequestDelay: 0 - }); - + model = new DataStorage({ + dataScope: 'magento', + cachedRequestDelay: 0 + }); spyOn(_, 'delay'); model.getRequestData(request); expect(_.delay).not.toHaveBeenCalled(); From c3ba2ce2468e04ed87f11c8a2cbac7732ef1fe40 Mon Sep 17 00:00:00 2001 From: konarshankar07 Date: Thu, 6 Feb 2020 23:26:35 +0530 Subject: [PATCH 06/10] improve test names --- .../app/code/Magento/Ui/base/js/grid/data-storage.test.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) 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 02d303782a9d5..ade7d09797139 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 @@ -225,7 +225,7 @@ define([ expect(model.getRequest).toHaveBeenCalled(); }); - it('Return "getRequestData" method', function () { + it('it returns cached request data if a cached request exists and no refresh option is provided', function () { var params = { namespace: 'magento', search: '', @@ -247,7 +247,7 @@ define([ expect(model.getRequestData).toHaveBeenCalled(); }); - it('Return "requestData" method', function () { + it('if refresh option is true so it will ignore cache and execute the requestData function', function () { var params = { namespace: 'magento', search: '', From 0aef9add469dda3a0102b886b008757469bb8f26 Mon Sep 17 00:00:00 2001 From: konarshankar07 Date: Sat, 8 Feb 2020 01:16:03 +0530 Subject: [PATCH 07/10] feedback changes --- .../Ui/base/js/grid/data-storage.test.js | 178 ++---------------- 1 file changed, 17 insertions(+), 161 deletions(-) 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 ade7d09797139..33c2e97e0b85f 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 @@ -31,20 +31,6 @@ define([ dataScope: 'magento' }); - it('Check for defined ', function () { - expect(model.hasOwnProperty('initConfig')).toBeDefined(); - }); - - it('Check method type', function () { - var type = typeof model.initConfig; - - expect(type).toEqual('function'); - }); - - it('Check returned value if method called without arguments', function () { - expect(model.initConfig()).toBeDefined(); - }); - it('Check returned value type if method called without arguments', function () { var type = typeof model.initConfig(); @@ -70,20 +56,6 @@ define([ dataScope: 'magento' }); - it('check for defined', function () { - expect(model.hasOwnProperty('getByIds')).toBeDefined(); - }); - - it('check method type', function () { - expect(typeof model.getByIds).toEqual('function'); - }); - - it('Check returned value if method called with argument', function () { - var ids = [1,2,3]; - - expect(model.getByIds(ids)).toBeDefined(); - }); - it('check returned type if method called with argument', function () { var ids = [1,2,3], type = typeof model.getByIds(ids); @@ -98,7 +70,13 @@ define([ }); it('Return array if "getByIds" has been called', function () { - var ids = [1]; + var ids = [1], + expectedValue = [ + { + id_field_name: 'entity_id', + entity_id: '1' + } + ]; model = new DataStorage({ dataScope: 'magento', @@ -110,7 +88,7 @@ define([ } }); - expect(typeof model.getByIds(ids)).toEqual('object'); + expect(model.getByIds(ids)).toEqual(expectedValue); }); }); @@ -121,25 +99,15 @@ define([ dataScope: 'magento' }); - it('check for defined', function () { - expect(model.hasOwnProperty('getIds')).toBeDefined(); - }); - - it('check method type', function () { - expect(typeof model.getIds).toEqual('function'); - }); - - it('check returned value if method called with argument', function () { - var ids = [1,2,3]; - - expect(model.getIds(ids)).toBeDefined(); - }); - - it('check returned type if method called with argument', function () { - var ids = [1,2,3], - type = typeof model.getIds(ids); - - expect(type).toEqual('object'); + it('check array of entity_id will return', function () { + var ids = [ + { + id_field_name: 'entity_id', + entity_id: '1' + } + ], + expectedValue = ['1']; + expect(model.getIds(ids)).toEqual(expectedValue); }); }); @@ -150,28 +118,6 @@ define([ dataScope: 'magento' }); - it('check for defined', function () { - expect(model.hasOwnProperty('getData')).toBeDefined(); - }); - - it('check method type', function () { - expect(typeof model.getData).toEqual('function'); - }); - - it('check returned value if method called with argument', function () { - var params = { - namespace: 'magento', - search: '', - filters: { - store_id: 0 - }, - sorting: {}, - paging: {} - }; - - expect(model.getData(params)).toBeDefined(); - }); - it('check returned type if method called with argument', function () { var params = { namespace: 'magento', @@ -332,16 +278,6 @@ define([ } }); - it('Check for defined ', function () { - expect(model.hasOwnProperty('updateData')).toBeDefined(); - }); - - it('Check method type', function () { - var type = typeof model.updateData; - - expect(type).toEqual('function'); - }); - it('Check updateData has been called', function () { var data = [{ id_field_name: 'entity_id', @@ -357,16 +293,6 @@ define([ dataScope: 'magento' }); - it('Check for defined', function () { - expect(model.hasOwnProperty('requestData')).toBeDefined(); - }); - - it('Check method type', function () { - var type = typeof model.requestData; - - expect(type).toEqual('function'); - }); - it('Check Ajax request', function () { var params = { namespace: 'magento', @@ -401,16 +327,6 @@ define([ dataScope: 'magento' }); - it('Check for defined', function () { - expect(model.hasOwnProperty('getRequest')).toBeDefined(); - }); - - it('Check method', function () { - var type = typeof model.getRequest; - - expect(type).toEqual('function'); - }); - it('check "getRequest" has been executed', function () { var params = { namespace: 'magento', @@ -434,16 +350,6 @@ define([ dataScope: 'magento' }); - it('Check for defined ', function () { - expect(model.hasOwnProperty('getRequestData')).toBeDefined(); - }); - - it('Check method type', function () { - var type = typeof model.getRequestData; - - expect(type).toEqual('function'); - }); - it('check "getRequestData" has been executed', function () { var request = { ids: [1,2,3] @@ -495,16 +401,6 @@ define([ dataScope: 'magento' }); - it('Check for defined ', function () { - expect(model.hasOwnProperty('cacheRequest')).toBeDefined(); - }); - - it('Check method type', function () { - var type = typeof model.cacheRequest; - - expect(type).toEqual('function'); - }); - it('check "model._requests"', function () { var params = { namespace: 'magento', @@ -559,16 +455,6 @@ define([ dataScope: 'magento' }); - it('Check for defined ', function () { - expect(model.hasOwnProperty('clearRequests')).toBeDefined(); - }); - - it('Check method type', function () { - var type = typeof model.clearRequests; - - expect(type).toEqual('function'); - }); - it('check "clearRequests" will empty _requests array', function () { var params = { namespace: 'magento', @@ -600,16 +486,6 @@ define([ dataScope: 'magento' }); - it('Check for defined ', function () { - expect(model.hasOwnProperty('removeRequest')).toBeDefined(); - }); - - it('Check method type', function () { - var type = typeof model.removeRequest; - - expect(type).toEqual('function'); - }); - it('check "removeRequest" is defined', function () { var params = { namespace: 'magento', @@ -634,16 +510,6 @@ define([ dataScope: 'magento' }); - it('Check for defined ', function () { - expect(model.hasOwnProperty('wasRequested')).toBeDefined(); - }); - - it('Check method type', function () { - var type = typeof model.wasRequested; - - expect(type).toEqual('function'); - }); - it('Return false if getRequest method returns false', function () { var params = { namespace: 'magento', @@ -664,16 +530,6 @@ define([ dataScope: 'magento' }); - it('Check for defined ', function () { - expect(model.hasOwnProperty('onRequestComplete')).toBeDefined(); - }); - - it('Check method type', function () { - var type = typeof model.onRequestComplete; - - expect(type).toEqual('function'); - }); - it('Check "updateData" method has been called', function () { var data = { items: [{ From 7ef0c88e9c0b452a5e43ad0189a052a4dab500a8 Mon Sep 17 00:00:00 2001 From: Sergii Ivashchenko Date: Sun, 9 Feb 2020 23:31:38 +0000 Subject: [PATCH 08/10] magento/magento2#26502: Code review changes --- .../Ui/base/js/grid/data-storage.test.js | 657 ++++++++---------- 1 file changed, 287 insertions(+), 370 deletions(-) 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 33c2e97e0b85f..da2e4ace72f7c 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 @@ -7,257 +7,186 @@ /*jscs:disable requireCamelCaseOrUpperCaseIdentifiers*/ define([ 'jquery', - 'mageUtils', - 'underscore', 'Magento_Ui/js/grid/data-storage' -], function ($, utils, _, DataStorage) { +], function ($, DataStorage) { 'use strict'; describe('Magento_Ui/js/grid/data-storage', function () { - describe('constructor', 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('"initConfig" method', function () { + it('changes string dataScope property to an array', function () { + var model = new DataStorage({ + dataScope: 'magento' + }); - var model = new DataStorage({ - dataScope: 'magento' + expect(model.dataScope).toEqual(['magento']); }); - it('Check returned value type if method called without arguments', function () { - var type = typeof model.initConfig(); + it('changes empty string dataScope property to an empty array', function () { + var model = new DataStorage({ + dataScope: '' + }); - expect(type).toEqual('object'); + expect(model.dataScope).toEqual([]); }); - it('Check this.dataScope property (is modify in initConfig method)', function () { - model.dataScope = null; - model.initConfig(); - expect(typeof model.dataScope).toEqual('object'); + it('doesn\'t change non-string dataScope property', function () { + var testScope = { + testKey: 'test value' + }, + model = new DataStorage({ + dataScope: testScope + }); + + expect(model.dataScope).toEqual(testScope); }); - it('Check this._requests property (is modify in initConfig method)', function () { + it('initializes _requests property as an empty array', function () { + var model = new DataStorage(); model._requests = null; model.initConfig(); - expect(typeof model._requests).toEqual('object'); + expect(model._requests).toEqual([]); }); }); describe('"getByIds" method', function () { - var model = new DataStorage({ - dataScope: 'magento' + it('returns false if data for ids is missing', function () { + var model = new DataStorage(); + + expect(model.getByIds([1,2,3])).toEqual(false); }); - it('check returned type if method called with argument', function () { - var ids = [1,2,3], - type = typeof model.getByIds(ids); + it('returns array of items', function () { + var item = { + id_field_name: 'entity_id', + entity_id: '1' + }, + model = new DataStorage({ + data: { + 1: item + } + }); - expect(type).toEqual('boolean'); + expect(model.getByIds([1])).toEqual([item]); }); - it('Return false if "getByIds" has been called', function () { - var ids = [1,2,3]; + }); - expect(model.getByIds(ids)).toEqual(false); - }); + describe('"getIds" method', function () { - it('Return array if "getByIds" has been called', function () { - var ids = [1], - expectedValue = [ + 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' - } - ]; - - model = new DataStorage({ - dataScope: 'magento', - data: { - 1: { + }, + { id_field_name: 'entity_id', - entity_id: '1' + entity_id: '54' } - } - }); + ]; - expect(model.getByIds(ids)).toEqual(expectedValue); + expect(model.getIds(ids)).toEqual(['1', '54']); }); - }); - - describe('"getIds" method', function () { + 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' + }, + } + }); - var model = new DataStorage({ - dataScope: 'magento' - }); - - it('check array of entity_id will return', function () { - var ids = [ - { - id_field_name: 'entity_id', - entity_id: '1' - } - ], - expectedValue = ['1']; - expect(model.getIds(ids)).toEqual(expectedValue); + expect(model.getIds()).toEqual(['1', '42']); }); }); describe('"getData" method', function () { - var model = new DataStorage({ - dataScope: 'magento' - }); - - it('check returned type if method called with argument', function () { - var params = { - namespace: 'magento', - search: '', - filters: { - store_id: 0 - }, - sorting: {}, - paging: {} - }, - type = typeof model.getData(params); - - expect(type).toEqual('object'); - }); + var model = new DataStorage(); - it('check "clearRequests" has been called', function () { - var params = { - namespace: 'magento', - search: '', - filters: { - store_id: 0 - }, - sorting: {}, - paging: {} - }; + it('returns the result of requestData method if scope have been changed', function () { + var requestDataResult = 'requestDataResult'; spyOn(model, 'clearRequests'); - spyOn(model, 'hasScopeChanged').and.callFake(function () { - return true; - }); - model.getData(params); + spyOn(model, 'hasScopeChanged').and.returnValue(true); + spyOn(model, 'requestData').and.returnValue(requestDataResult); + expect(model.getData()).toEqual(requestDataResult); expect(model.clearRequests).toHaveBeenCalled(); }); - it('check "getRequest" has been called', function () { - var params = { - namespace: 'magento', - search: '', - filters: { - store_id: 0 - }, - sorting: {}, - paging: {} - }; + it('returns the cached result if scope have not been changed', function () { + var cachedRequestDataResult = 'cachedRequestDataResult'; - spyOn(model, 'getRequest'); - spyOn(model, 'hasScopeChanged').and.callFake(function () { - return false; - }); - model.getData(params); - expect(model.getRequest).toHaveBeenCalled(); - }); - - it('it returns cached request data if a cached request exists and no refresh option is provided', function () { - var params = { - namespace: 'magento', - search: '', - filters: { - store_id: 0 - }, - sorting: {}, - paging: {} - }, - options = { - refresh: false - }; + spyOn(model, 'clearRequests'); + spyOn(model, 'requestData'); + spyOn(model, 'hasScopeChanged').and.returnValue(false); + spyOn(model, 'getRequest').and.returnValue(true); + spyOn(model, 'getRequestData').and.returnValue(cachedRequestDataResult); - spyOn(model, 'getRequestData'); - spyOn(model, 'getRequest').and.callFake(function () { - return true; - }); - model.getData(params, options); - expect(model.getRequestData).toHaveBeenCalled(); + expect(model.getData()).toEqual(cachedRequestDataResult); + expect(model.clearRequests).not.toHaveBeenCalled(); + expect(model.requestData).not.toHaveBeenCalled(); }); - it('if refresh option is true so it will ignore cache and execute the requestData function', function () { - var params = { - namespace: 'magento', - search: '', - filters: { - store_id: 0 - }, - sorting: {}, - paging: {} - }, + it('returns the result of requestData method if refresh option is provided', function () { + var requestDataResult = 'requestDataResult', options = { refresh: true }; - spyOn(model, 'requestData'); - spyOn(model, 'getRequest').and.callFake(function () { - return false; - }); - model.getData(params, options); - expect(model.requestData).toHaveBeenCalled(); + 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('is function', function () { - var model = new DataStorage({ - dataScope: '' - }); - - expect(model.hasScopeChanged).toBeDefined(); - expect(typeof model.hasScopeChanged).toEqual('function'); - }); it('returns false if no requests have been made', function () { - var model = new DataStorage({ - dataScope: '' - }); + var model = new DataStorage(); expect(model.hasScopeChanged()).toBeFalsy(); }); - it('tells whether parameters defined in the dataScope property have changed', function () { - var params, newParams, model; - - params = { - namespace: 'magento', - search: '', - filters: { - store_id: 0 + it('returns true for not cached params', function () { + var params = { + search: '1', + filters: { + store_id: 0 + } }, - sorting: {}, - paging: {} - }; - - newParams = utils.extend({}, params, { - search: 'magento', - filters: { - store_id: 1 - } - }); - - model = new DataStorage({ - dataScope: 'filters.store_id' - }); + newParams = { + search: '2', + filters: { + store_id: 1 + } + }, + model = new DataStorage({ + dataScope: 'filters.store_id' + }); model.cacheRequest({ totalRecords: 0 @@ -275,50 +204,47 @@ define([ url: 'magento.com', method: 'GET', dataType: 'json' + }, + data: { + 1: { + id_field_name: 'entity_id', + entity_id: '1', + field: 'value' + }, } }); - it('Check updateData has been called', function () { + it('updates data items', function () { var data = [{ id_field_name: 'entity_id', - entity_id: '1' + entity_id: '1', + field: 'updatedValue' }]; - expect(model.updateData(data)).toBeTruthy(); + expect(model.updateData(data)).toEqual(model); + expect(model.getByIds([1])).toEqual(data); }); }); describe('"requestData" method', function () { - var model = new DataStorage({ - dataScope: 'magento' - }); + var model = new DataStorage(); it('Check Ajax request', function () { - var params = { - namespace: 'magento', - search: '', - filters: { - store_id: 0 - }, - sorting: {}, - paging: {} - }, - query = utils.copy(params); - - spyOn(model, 'onRequestComplete'); - spyOn($, 'ajax').and.callFake(function () { - return { - /** - * Success result for ajax request - */ - done: function () { - model.onRequestComplete(model, query); - } - }; + var result = 'result'; + + spyOn(model, 'onRequestComplete').and.returnValue(result); + spyOn($, 'ajax').and.returnValue({ + /** + * Success result for ajax request + * + * @param handler + * @returns {*} + */ + done: function (handler) { + return handler(); + } }); - model.requestData(params); - expect($.ajax).toHaveBeenCalled(); - expect(model.onRequestComplete).toHaveBeenCalled(); + expect(model.requestData({})).toEqual(result); }); }); @@ -327,72 +253,54 @@ define([ dataScope: 'magento' }); - it('check "getRequest" has been executed', function () { + it('returns cached request', function () { var params = { - namespace: 'magento', - search: '', - sorting: {}, - paging: {} - }; + namespace: 'magento', + search: '', + sorting: {}, + paging: {} + }, + request = { + ids: ['1'], + params: params, + totalRecords: 1, + errorMessage: '' + }; - model._requests.push({ - ids: ['1'], - params: params, - totalRecords: 1, - errorMessage: '' - }); - expect(model.getRequest(params)).toBeTruthy(); + model._requests.push(request); + expect(model.getRequest(params)).toEqual(request); }); }); describe('"getRequestData" method', function () { - var model = new DataStorage({ - dataScope: 'magento' - }); - - it('check "getRequestData" has been executed', function () { - var request = { - ids: [1,2,3] - }; - - expect(model.getRequestData(request)).toBeTruthy(); - }); - - it('check "getByIds" has been executed', function () { - var request = { - ids: [1,2,3] - }; - - spyOn(model, 'getByIds'); - model.getRequestData(request); - expect(model.getByIds).toHaveBeenCalled(); - }); - - it('check "delay" function has been executed', function () { + it('returns request data', function () { var request = { - ids: [1,2,3], - totalRecords: 3, - errorMessage: '' - }; - - spyOn(_, 'delay'); - model.getRequestData(request); - expect(_.delay).toHaveBeenCalled(); - }); - - it('check "delay" function has not been executed', function () { - var request = { - ids: [1,2,3], - totalRecords: 3, + ids: [1,2], + totalRecords: 2, errorMessage: '' - }; - model = new DataStorage({ - dataScope: 'magento', - cachedRequestDelay: 0 - }); - spyOn(_, 'delay'); - model.getRequestData(request); - expect(_.delay).not.toHaveBeenCalled(); + }, + 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) + }) }); }); @@ -401,73 +309,83 @@ define([ dataScope: 'magento' }); - it('check "model._requests"', function () { + it('adds the request to the cache', function () { var params = { - namespace: 'magento', - search: '', - sorting: {}, - paging: {} - }, - data = { - items: ['1','2','3'], - totalRecords: 3 - }; + 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.callFake(function () { - return ['1','2','3']; - }); + spyOn(model, 'getIds').and.returnValue(ids); model.cacheRequest(data, params); - expect(typeof model._requests).toEqual('object'); - expect(model.getIds).toHaveBeenCalled(); + expect(model.getRequest(params)).toEqual(request); expect(model.removeRequest).not.toHaveBeenCalled(); }); - it('check "removeRequest" is executed', function () { + it('overwrites the previously cached request for the same params', function () { var params = { namespace: 'magento', search: '', sorting: {}, paging: {} }, - data = { - items: ['1','2','3'], - totalRecords: 3 + 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, 'removeRequest'); - spyOn(model, 'getRequest').and.callFake(function () { - return true; - }); - spyOn(model, 'getIds').and.callFake(function () { - return ['1','2','3']; - }); - model.cacheRequest(data, params); - expect(typeof model._requests).toEqual('object'); - expect(model.getIds).toHaveBeenCalled(); - expect(model.removeRequest).toHaveBeenCalled(); + 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 () { - var model = new DataStorage({ - dataScope: 'magento' - }); - - it('check "clearRequests" will empty _requests array', function () { - var params = { - namespace: 'magento', - search: 'magento', - filters: { - store_id: 1 - } - }; - - model = new DataStorage({ - dataScope: 'magento', - _requests: [] - }); + 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'], @@ -482,26 +400,26 @@ define([ describe('"removeRequest" method', function () { - var model = new DataStorage({ - dataScope: 'magento' - }); + var model = new DataStorage(); - it('check "removeRequest" is defined', function () { + it('removes the request from the cache', function () { var params = { - namespace: 'magento', - search: 'magento', - filters: { - store_id: 1 - } - }, - request = [{ - ids: [1,2,3], - params: params, - totalRecords: 3, - errorMessage: 'errorMessage' - }]; + namespace: 'magento', + search: '', + sorting: {}, + paging: {} + }, + request = { + ids: ['1','2','3'], + params: params, + totalRecords: 3, + errorMessage: '' + }; - expect(model.removeRequest(request)).toBeDefined(); + model._requests = [request]; + expect(model.getRequest(params)).toEqual(request); + model.removeRequest(request); + expect(model.getRequest(params)).toBeFalsy(); }); }); @@ -510,7 +428,7 @@ define([ dataScope: 'magento' }); - it('Return false if getRequest method returns false', function () { + it('returns false if request is not present in cache', function () { var params = { namespace: 'magento', search: '', @@ -518,61 +436,60 @@ define([ paging: {} }; - spyOn(model, 'getRequest').and.callFake(function () { - return false; - }); expect(model.wasRequested(params)).toBeFalsy(); }); - }); - describe('"onRequestComplete" method', function () { - var model = new DataStorage({ - dataScope: 'magento' - }); - - it('Check "updateData" method has been called', function () { - var data = { - items: [{ - id_field_name: 'entity_id', - entity_id: '1' - }] - }, - params = { + 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: '' }; - spyOn(model, 'updateData').and.callFake(function () { - return data; - }); + 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('Check "cacheRequest" method has been called', function () { - var data = { - items: [{ - id_field_name: 'entity_id', - entity_id: '1' - }] - }, - params = { - namespace: 'magento', - search: '', - sorting: {}, - paging: {} - }; + it('updates data and adds the request to cache if caching is enabled', function () { + var model = new DataStorage({ + cacheRequests: true + }), + data = { + items: [] + }, + params = {}; - model = new DataStorage({ - dataScope: 'magento', - cacheRequests: true - }); - spyOn(model, 'cacheRequest').and.callFake(function () { - return data; - }); + spyOn(model, 'updateData'); + spyOn(model, 'cacheRequest'); model.onRequestComplete(params, data); + expect(model.updateData).toHaveBeenCalled(); expect(model.cacheRequest).toHaveBeenCalled(); }); }); From a1d2a846d5752eb3708155ecc9468835d82f4504 Mon Sep 17 00:00:00 2001 From: Shankar Konar Date: Mon, 10 Feb 2020 11:25:47 +0530 Subject: [PATCH 09/10] Fixed static tests --- .../Magento/Ui/base/js/grid/data-storage.test.js | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) 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 da2e4ace72f7c..4fe7aff69d0e2 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 @@ -5,6 +5,8 @@ /*eslint max-nested-callbacks: 0*/ /*jscs:disable requireCamelCaseOrUpperCaseIdentifiers*/ +/*eslint newline-after-var: ["error", "always"]*/ +/*eslint-env es6*/ define([ 'jquery', 'Magento_Ui/js/grid/data-storage' @@ -52,6 +54,7 @@ define([ it('initializes _requests property as an empty array', function () { var model = new DataStorage(); + model._requests = null; model.initConfig(); expect(model._requests).toEqual([]); @@ -110,7 +113,7 @@ define([ 2: { id_field_name: 'entity_id', entity_id: '42' - }, + } } }); @@ -129,8 +132,10 @@ define([ 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 () { @@ -210,7 +215,7 @@ define([ id_field_name: 'entity_id', entity_id: '1', field: 'value' - }, + } } }); @@ -237,7 +242,7 @@ define([ /** * Success result for ajax request * - * @param handler + * @param {Function} handler * @returns {*} */ done: function (handler) { @@ -297,10 +302,11 @@ define([ model = new DataStorage({ cachedRequestDelay: 0 }); + spyOn(model, 'getByIds').and.returnValue(items); model.getRequestData(request).then(function (promiseResult) { - expect(promiseResult).toEqual(result) - }) + expect(promiseResult).toEqual(result); + }); }); }); From 1f2ed9035507c8ebf46ddacd0e9c923206ffec0d Mon Sep 17 00:00:00 2001 From: Shankar Konar Date: Mon, 10 Feb 2020 11:29:00 +0530 Subject: [PATCH 10/10] code cleanup --- .../tests/app/code/Magento/Ui/base/js/grid/data-storage.test.js | 2 -- 1 file changed, 2 deletions(-) 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 4fe7aff69d0e2..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 @@ -5,8 +5,6 @@ /*eslint max-nested-callbacks: 0*/ /*jscs:disable requireCamelCaseOrUpperCaseIdentifiers*/ -/*eslint newline-after-var: ["error", "always"]*/ -/*eslint-env es6*/ define([ 'jquery', 'Magento_Ui/js/grid/data-storage'