From 4632121525d11214a3c1d5f5e9dc21485a613315 Mon Sep 17 00:00:00 2001 From: Mike Kreiser Date: Sun, 5 Nov 2023 12:56:50 -0500 Subject: [PATCH] Pass un-flattened response through populateObject methods including manualParse --- .../base-cacheable-object.js | 4 +-- .../base-cacheable-object.test.js | 10 +++++--- src/base-classes/base-object/base-object.js | 25 +++++++++++-------- .../base-object/base-object.test.js | 8 +++--- 4 files changed, 28 insertions(+), 19 deletions(-) diff --git a/src/base-classes/base-cacheable-object/base-cacheable-object.js b/src/base-classes/base-cacheable-object/base-cacheable-object.js index 7f578fe..34ed529 100644 --- a/src/base-classes/base-cacheable-object/base-cacheable-object.js +++ b/src/base-classes/base-cacheable-object/base-cacheable-object.js @@ -26,10 +26,10 @@ class BaseCacheableObject extends BaseObject { * @override */ static _populateObject({ - data, constructorParams, instance, isDataFromServer + data, constructorParams, rawData, instance, isDataFromServer }) { const populatedInstance = super._populateObject({ - data, constructorParams, instance, isDataFromServer + data, constructorParams, rawData, instance, isDataFromServer }); if (isDataFromServer && populatedInstance.getCacheId()) { diff --git a/src/base-classes/base-cacheable-object/base-cacheable-object.test.js b/src/base-classes/base-cacheable-object/base-cacheable-object.test.js index 7422e3b..38714cc 100644 --- a/src/base-classes/base-cacheable-object/base-cacheable-object.test.js +++ b/src/base-classes/base-cacheable-object/base-cacheable-object.test.js @@ -12,9 +12,11 @@ describe('BaseCacheableObject', () => { let data; let isDataFromServer; let instance; + let rawData; beforeEach(() => { data = {}; + rawData = {}; isDataFromServer = true; instance = new TestBaseCacheableObject(); }); @@ -23,9 +25,9 @@ describe('BaseCacheableObject', () => { // Super lazy way to test jest.spyOn(BaseObject, '_populateObject'); - TestBaseCacheableObject._populateObject({ data, instance, isDataFromServer }); + TestBaseCacheableObject._populateObject({ data, instance, rawData, isDataFromServer }); - expect(BaseObject._populateObject).toBeCalledWith({ data, instance, isDataFromServer }); + expect(BaseObject._populateObject).toBeCalledWith({ data, instance, rawData, isDataFromServer }); BaseObject._populateObject.mockRestore(); }); @@ -50,7 +52,7 @@ describe('BaseCacheableObject', () => { const id = 'someCacheId23'; jest.spyOn(instance, 'getCacheId').mockReturnValue(id); - TestBaseCacheableObject._populateObject({ data, instance, isDataFromServer }); + TestBaseCacheableObject._populateObject({ data, instance, rawData, isDataFromServer }); expect(TestBaseCacheableObject.get(id)).toBe(instance); }); @@ -89,7 +91,7 @@ describe('BaseCacheableObject', () => { test('returns populated instance', () => { const returnedInstance = TestBaseCacheableObject._populateObject({ - data, instance, isDataFromServer + data, instance, rawData, isDataFromServer }); expect(returnedInstance).toBe(instance); diff --git a/src/base-classes/base-object/base-object.js b/src/base-classes/base-object/base-object.js index af9422c..811d7ec 100644 --- a/src/base-classes/base-object/base-object.js +++ b/src/base-classes/base-object/base-object.js @@ -49,7 +49,7 @@ class BaseObject { * @return {*} */ static _processObjectValue({ - data, constructorParams, instance, value + data, rawData, constructorParams, instance, value }) { if (!value.key) { throw new Error( @@ -60,10 +60,10 @@ class BaseObject { const responseData = _.get(data, value.key); if (_.isFunction(value.manualParse)) { - return value.manualParse(responseData, data, constructorParams, instance); + return value.manualParse(responseData, data, rawData, constructorParams, instance); } else if (value.BaseObject) { const buildInstance = (passedData) => ( - value.BaseObject.buildFromServer(passedData, constructorParams) + value.BaseObject.buildFromServer(passedData, constructorParams, rawData) ); return value.isArray ? _.map(responseData, buildInstance) : buildInstance(responseData); @@ -90,7 +90,7 @@ class BaseObject { * @param {String} options.value The value of the responseMap entry being parsed. */ static _processResponseMapItem({ - data, constructorParams, instance, isDataFromServer, key, value + data, rawData, constructorParams, instance, isDataFromServer, key, value }) { /** * @typedef {Object} BaseObject~ResponseMapValueObject @@ -145,7 +145,7 @@ class BaseObject { item = _.get(data, value); } else if (_.isPlainObject(value)) { item = this._processObjectValue({ - data, constructorParams, instance, value + data, rawData, constructorParams, instance, value }); } else { throw new Error( @@ -171,7 +171,7 @@ class BaseObject { * @return {BaseObject} The mutated BaseObject instance. */ static _populateObject({ - data, constructorParams, instance, isDataFromServer + data, rawData, constructorParams, instance, isDataFromServer }) { if (!instance) { throw new Error(`${this.displayName}: _populateObject: Did not receive instance to populate`); @@ -185,14 +185,14 @@ class BaseObject { _.set(deferredMapItems, key, value); } else { this._processResponseMapItem({ - data, constructorParams, instance, isDataFromServer, key, value + data, rawData, constructorParams, instance, isDataFromServer, key, value }); } }); _.forEach(deferredMapItems, (value, key) => { this._processResponseMapItem({ - data, constructorParams, instance, isDataFromServer, key, value + data, rawData, constructorParams, instance, isDataFromServer, key, value }); }); @@ -211,9 +211,14 @@ class BaseObject { static buildFromServer(data, constructorParams) { const instance = new this(constructorParams); - const dataToUse = this.flattenResponse ? flattenObject(data) : data; + const flatData = this.flattenResponse ? flattenObjectSansNumericKeys(data) : data; + this._populateObject({ - data: dataToUse, constructorParams, instance, isDataFromServer: true + data: flatData, + rawData: data, + constructorParams, + instance, + isDataFromServer: true }); return instance; diff --git a/src/base-classes/base-object/base-object.test.js b/src/base-classes/base-object/base-object.test.js index d8a434d..0527dd4 100644 --- a/src/base-classes/base-object/base-object.test.js +++ b/src/base-classes/base-object/base-object.test.js @@ -148,7 +148,7 @@ describe('BaseObject', () => { }); const callPopulate = (Klass = TestBaseObject) => Klass._populateObject({ - data, constructorParams, instance, isDataFromServer + data, rawData: data, constructorParams, instance, isDataFromServer }); describe('when a instance is not passed', () => { @@ -295,7 +295,7 @@ describe('BaseObject', () => { test('calls the manualParse function', () => { callPopulate(); expect(TestBaseObject.responseMap.someManualObject.manualParse).toBeCalledWith( - data.manual, data, constructorParams, instance + data.manual, data, data, constructorParams, instance ); }); @@ -428,7 +428,7 @@ describe('BaseObject', () => { expect( TestBaseObject.responseMap.someManualAndBaseObject.manualParse ).toBeCalledWith( - data.both, data, constructorParams, instance + data.both, data, data, constructorParams, instance ); expect(MappingTestBaseObject.buildFromServer).not.toBeCalledWith(data.both); @@ -611,6 +611,7 @@ describe('BaseObject', () => { TestBaseObject.buildFromServer(data, constructorParams); expect(TestBaseObject._populateObject).toBeCalledWith({ data: flattenObject(data), + rawData: data, constructorParams, instance: expect.any(TestBaseObject), isDataFromServer: true @@ -643,6 +644,7 @@ describe('BaseObject', () => { TestBaseObject.buildFromServer(data, constructorParams); expect(TestBaseObject._populateObject).toBeCalledWith({ data, + rawData: data, constructorParams, instance: expect.any(TestBaseObject), isDataFromServer: true