Skip to content

Commit

Permalink
Code changed to maintain old reference behaviour in non-dev
Browse files Browse the repository at this point in the history
Tests updated
  • Loading branch information
johnbastian-trayio committed Sep 12, 2019
1 parent 808f26c commit 1ad3606
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 8 deletions.
6 changes: 6 additions & 0 deletions lib/addMethod/globalize/validateObjectArgumentByReference.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,18 @@ module.exports = function (referenceModificationErrorMessage, nonObjectErrorMess
} else {
// eslint-disable-next-line no-console
console.warn(referenceModificationErrorMessage);
//Maintain backwards compatibility in non-dev mode
return referencedObject;
}
}
/*
Need another cloneDeep, else originalObjectCopy variable
becomes a reference for method config's function
*/
/*
TODO: introduce breaking change to introduce via a flag
(which also avoids the return of the reference object above)
*/
return _.cloneDeep(originalObjectCopy);
} else if (_.isPlainObject(returnedObject)) {
return returnedObject;
Expand Down
52 changes: 44 additions & 8 deletions tests/globalize_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -390,7 +390,7 @@ describe('#globalize', function () {
.then(done, done);
});

describe('should use original params if modified but not returned (and console warn)', function () {
describe('should use reference params if modified but not returned (and console warn)', function () {

const sampleGlobal = {
_globalOptions: {
Expand All @@ -414,7 +414,13 @@ describe('#globalize', function () {
it('global', function (done) {
globalize.before.call(sampleGlobal, {}, _.cloneDeep(originalParams))
.then(function (params) {
assert.deepEqual(params, originalParams);
assert.deepEqual(
params,
{
id: 'abc123',
notes: 'Hello'
}
);
})
.then(done, done);
});
Expand All @@ -426,7 +432,13 @@ describe('#globalize', function () {
_.cloneDeep(originalParams)
)
.then(function (params) {
assert.deepEqual(params, originalParams);
assert.deepEqual(
params,
{
id: 'abc123',
notes: ' World'
}
);
})
.then(done, done);
});
Expand All @@ -438,7 +450,13 @@ describe('#globalize', function () {
_.cloneDeep(originalParams)
)
.then(function (params) {
assert.deepEqual(params, originalParams);
assert.deepEqual(
params,
{
id: 'abc123',
notes: 'Hello World'
}
);
})
.then(done, done);
});
Expand Down Expand Up @@ -684,7 +702,7 @@ describe('#globalize', function () {
.then(done, done);
});

describe('should use original request if modified but not returned (and console warn)', function () {
describe('should use reference request if modified but not returned (and console warn)', function () {

const sampleGlobal = {
_globalOptions: {
Expand All @@ -708,7 +726,13 @@ describe('#globalize', function () {
it('global', function (done) {
globalize.beforeRequest.call(sampleGlobal, {}, _.cloneDeep(originalRequest))
.then(function (request) {
assert.deepEqual(request, originalRequest);
assert.deepEqual(
request,
{
method: 'get',
url: 'test.com?hello=world'
}
);
})
.then(done, done);
});
Expand All @@ -720,7 +744,13 @@ describe('#globalize', function () {
_.cloneDeep(originalRequest)
)
.then(function (request) {
assert.deepEqual(request, originalRequest);
assert.deepEqual(
request,
{
method: 'get',
url: 'test.com&test=123'
}
);
})
.then(done, done);
});
Expand All @@ -732,7 +762,13 @@ describe('#globalize', function () {
_.cloneDeep(originalRequest)
)
.then(function (request) {
assert.deepEqual(request, originalRequest);
assert.deepEqual(
request,
{
method: 'get',
url: 'test.com?hello=world&test=123'
}
);
})
.then(done, done);
});
Expand Down

0 comments on commit 1ad3606

Please sign in to comment.