From 27563bc4e7dc7d20d1ef6acca6f7b24932cac27d Mon Sep 17 00:00:00 2001 From: Wilhelm Behncke Date: Thu, 3 Mar 2016 18:23:36 +0100 Subject: [PATCH] FEATURE: Make migrations tolerant towards undefined subjects --- src/migrations/atoms/add/index.spec.js | 5 +++++ src/migrations/atoms/drop/index.spec.js | 5 +++++ src/migrations/atoms/override/index.spec.js | 5 +++++ src/migrations/atoms/pop/index.spec.js | 5 +++++ src/migrations/atoms/remove/index.spec.js | 5 +++++ src/migrations/atoms/set/index.js | 8 ++++++-- src/migrations/atoms/set/index.spec.js | 5 +++++ src/migrations/atoms/shift/index.spec.js | 5 +++++ src/migrations/atoms/unshift/index.spec.js | 5 +++++ src/migrations/molecules/merge/index.spec.js | 5 +++++ src/migrations/molecules/toggle/index.js | 4 ++++ src/migrations/molecules/toggle/index.spec.js | 7 +++++++ 12 files changed, 62 insertions(+), 2 deletions(-) diff --git a/src/migrations/atoms/add/index.spec.js b/src/migrations/atoms/add/index.spec.js index 114ffdd..515d1bb 100644 --- a/src/migrations/atoms/add/index.spec.js +++ b/src/migrations/atoms/add/index.spec.js @@ -109,6 +109,11 @@ describe('Migrations > Atoms > $add', () => { expect(console.warn.calledOnce).to.equal(true); expect(console.warn.calledWith('Cannot add {a: overwrite?} to test, because it is already set.')).to.equal(true); }); + + it('should tolerate undefined subjects', () => { + const subject = undefined; + expect($add('some.path', 'someValue', subject)).to.be.an('undefined'); + }); }); describe('Immutable', () => { diff --git a/src/migrations/atoms/drop/index.spec.js b/src/migrations/atoms/drop/index.spec.js index 8a67125..efa78f7 100644 --- a/src/migrations/atoms/drop/index.spec.js +++ b/src/migrations/atoms/drop/index.spec.js @@ -82,6 +82,11 @@ describe('Migrations > Atoms > $drop', () => { expect(console.warn.calledTwice).to.equal(true); expect(console.warn.calledWith('Cannot drop an item from a undefined.')).to.equal(true); }); + + it('should tolerate undefined subjects', () => { + const subject = undefined; + expect($drop('some.path', subject)).to.be.an('undefined'); + }); }); describe('Immutable', () => { diff --git a/src/migrations/atoms/override/index.spec.js b/src/migrations/atoms/override/index.spec.js index 3747b16..0120ae4 100644 --- a/src/migrations/atoms/override/index.spec.js +++ b/src/migrations/atoms/override/index.spec.js @@ -70,6 +70,11 @@ describe('Migrations > Atoms > $override', () => { }); expect($override('test', ['foo'], subject)).to.not.equal(subject); }); + + it('should tolerate undefined subjects', () => { + const subject = undefined; + expect($override('some.path', 'someValue', subject)).to.be.an('undefined'); + }); }); describe('Immutable', () => { diff --git a/src/migrations/atoms/pop/index.spec.js b/src/migrations/atoms/pop/index.spec.js index a66c12b..1033c2b 100644 --- a/src/migrations/atoms/pop/index.spec.js +++ b/src/migrations/atoms/pop/index.spec.js @@ -50,6 +50,11 @@ describe('Migrations > Atoms > $pop', () => { expect(console.warn.calledThrice).to.equal(true); expect(console.warn.calledWith('Cannot pop an item from a object.')).to.equal(true); }); + + it('should tolerate undefined subjects', () => { + const subject = undefined; + expect($pop('some.path', subject)).to.be.an('undefined'); + }); }); describe('Immutable', () => { diff --git a/src/migrations/atoms/remove/index.spec.js b/src/migrations/atoms/remove/index.spec.js index 734604c..80cad5c 100644 --- a/src/migrations/atoms/remove/index.spec.js +++ b/src/migrations/atoms/remove/index.spec.js @@ -80,6 +80,11 @@ describe('Migrations > Atoms > $remove', () => { expect(console.warn.calledTwice).to.equal(true); expect(console.warn.calledWith('Cannot remove an item from a undefined.')).to.equal(true); }); + + it('should tolerate undefined subjects', () => { + const subject = undefined; + expect($remove('some.path', 'someValue', subject)).to.be.an('undefined'); + }); }); describe('Immutable', () => { diff --git a/src/migrations/atoms/set/index.js b/src/migrations/atoms/set/index.js index 460163f..ab8bfe3 100644 --- a/src/migrations/atoms/set/index.js +++ b/src/migrations/atoms/set/index.js @@ -40,7 +40,11 @@ const recursivelySetValueInObject = (object, value, path) => { // export default createPolymorphFunction( path => value => subject => { - const object = JSON.parse(JSON.stringify(subject)); - return recursivelySetValueInObject(object, value, resolveObjectPath(path)); + if (typeof subject !== 'undefined') { + const object = JSON.parse(JSON.stringify(subject)); + return recursivelySetValueInObject(object, value, resolveObjectPath(path)); + } + + return subject; } ); diff --git a/src/migrations/atoms/set/index.spec.js b/src/migrations/atoms/set/index.spec.js index d11cc63..2c47ea6 100644 --- a/src/migrations/atoms/set/index.spec.js +++ b/src/migrations/atoms/set/index.spec.js @@ -154,6 +154,11 @@ describe('Migrations > Atoms > $set', () => { }] }); }); + + it('should tolerate undefined subjects', () => { + const subject = undefined; + expect($set('some.path', 'someValue', subject)).to.be.an('undefined'); + }); }); describe('Immutable', () => { diff --git a/src/migrations/atoms/shift/index.spec.js b/src/migrations/atoms/shift/index.spec.js index 1d7e2fb..0e18e34 100644 --- a/src/migrations/atoms/shift/index.spec.js +++ b/src/migrations/atoms/shift/index.spec.js @@ -50,6 +50,11 @@ describe('Migrations > Atoms > $shift', () => { expect(console.warn.calledThrice).to.equal(true); expect(console.warn.calledWith('Cannot shift an item from a object.')).to.equal(true); }); + + it('should tolerate undefined subjects', () => { + const subject = undefined; + expect($shift('some.path', subject)).to.be.an('undefined'); + }); }); describe('Immutable', () => { diff --git a/src/migrations/atoms/unshift/index.spec.js b/src/migrations/atoms/unshift/index.spec.js index 648cd44..bd6a497 100644 --- a/src/migrations/atoms/unshift/index.spec.js +++ b/src/migrations/atoms/unshift/index.spec.js @@ -109,6 +109,11 @@ describe('Migrations > Atoms > $unshift', () => { expect(console.warn.calledOnce).to.equal(true); expect(console.warn.calledWith('Cannot add {a: overwrite?} to test, because it is already set.')).to.equal(true); }); + + it('should tolerate undefined subjects', () => { + const subject = undefined; + expect($unshift('some.path', 'someValue', subject)).to.be.an('undefined'); + }); }); describe('Immutable', () => { diff --git a/src/migrations/molecules/merge/index.spec.js b/src/migrations/molecules/merge/index.spec.js index 3cadd5d..db432f5 100644 --- a/src/migrations/molecules/merge/index.spec.js +++ b/src/migrations/molecules/merge/index.spec.js @@ -82,6 +82,11 @@ describe('Migrations > Atoms > $merge', () => { }); expect($merge('test', {a: ['foo']}, subject)).to.not.equal(subject); }); + + it('should tolerate undefined subjects', () => { + const subject = undefined; + expect($merge('some.path', {some: 'value'}, subject)).to.be.an('undefined'); + }); }); describe('Immutable', () => { diff --git a/src/migrations/molecules/toggle/index.js b/src/migrations/molecules/toggle/index.js index 660a5be..490da0a 100644 --- a/src/migrations/molecules/toggle/index.js +++ b/src/migrations/molecules/toggle/index.js @@ -39,6 +39,8 @@ export default createPolymorphFunction( if (typeof target === 'boolean') { return $set(path, !target, value); } + } else if (typeof value === 'undefined') { + return value; } return fallback => { @@ -60,6 +62,8 @@ export default createPolymorphFunction( // Perform value toggle with empty fallback // return $set(path, target === value ? getEmptyValue(value) : value, fallback); + } else if (typeof fallback === 'undefined') { + return fallback; } return subject => { diff --git a/src/migrations/molecules/toggle/index.spec.js b/src/migrations/molecules/toggle/index.spec.js index a21c283..7b240fd 100644 --- a/src/migrations/molecules/toggle/index.spec.js +++ b/src/migrations/molecules/toggle/index.spec.js @@ -116,6 +116,13 @@ describe('Migrations > Molecules > $toggle', () => { expect($toggle('test', 'foo', 'bar', subject)).to.deep.equal({ test: 'bar' }); expect($toggle('test', 'foo', 'bar', subject)).to.not.equal(subject); }); + + it('should tolerate undefined subjects', () => { + const subject = undefined; + expect($toggle('some.path', subject)).to.be.an('undefined'); + expect($toggle('some.path', 'foo', subject)).to.be.an('undefined'); + expect($toggle('some.path', 'foo', 'bar', subject)).to.be.an('undefined'); + }); }); describe('Immutable', () => {