Skip to content

Commit

Permalink
Pass un-flattened response through populateObject methods including m…
Browse files Browse the repository at this point in the history
…anualParse
  • Loading branch information
mkreiser committed Nov 12, 2023
1 parent 9604209 commit 4632121
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,11 @@ describe('BaseCacheableObject', () => {
let data;
let isDataFromServer;
let instance;
let rawData;

beforeEach(() => {
data = {};
rawData = {};
isDataFromServer = true;
instance = new TestBaseCacheableObject();
});
Expand All @@ -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();
});
Expand All @@ -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);
});
Expand Down Expand Up @@ -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);
Expand Down
25 changes: 15 additions & 10 deletions src/base-classes/base-object/base-object.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ class BaseObject {
* @return {*}
*/
static _processObjectValue({
data, constructorParams, instance, value
data, rawData, constructorParams, instance, value
}) {
if (!value.key) {
throw new Error(
Expand All @@ -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);
Expand All @@ -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
Expand Down Expand Up @@ -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(
Expand All @@ -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`);
Expand All @@ -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
});
});

Expand All @@ -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;
Expand Down
8 changes: 5 additions & 3 deletions src/base-classes/base-object/base-object.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down Expand Up @@ -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
);
});

Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -643,6 +644,7 @@ describe('BaseObject', () => {
TestBaseObject.buildFromServer(data, constructorParams);
expect(TestBaseObject._populateObject).toBeCalledWith({
data,
rawData: data,
constructorParams,
instance: expect.any(TestBaseObject),
isDataFromServer: true
Expand Down

0 comments on commit 4632121

Please sign in to comment.