Skip to content
This repository has been archived by the owner on Dec 22, 2022. It is now read-only.

Commit

Permalink
FEATURE: Make migrations tolerant towards undefined subjects
Browse files Browse the repository at this point in the history
  • Loading branch information
grebaldi committed Mar 3, 2016
1 parent 52fc896 commit 27563bc
Show file tree
Hide file tree
Showing 12 changed files with 62 additions and 2 deletions.
5 changes: 5 additions & 0 deletions src/migrations/atoms/add/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down
5 changes: 5 additions & 0 deletions src/migrations/atoms/drop/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down
5 changes: 5 additions & 0 deletions src/migrations/atoms/override/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down
5 changes: 5 additions & 0 deletions src/migrations/atoms/pop/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down
5 changes: 5 additions & 0 deletions src/migrations/atoms/remove/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down
8 changes: 6 additions & 2 deletions src/migrations/atoms/set/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
);
5 changes: 5 additions & 0 deletions src/migrations/atoms/set/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down
5 changes: 5 additions & 0 deletions src/migrations/atoms/shift/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down
5 changes: 5 additions & 0 deletions src/migrations/atoms/unshift/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down
5 changes: 5 additions & 0 deletions src/migrations/molecules/merge/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down
4 changes: 4 additions & 0 deletions src/migrations/molecules/toggle/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 => {
Expand All @@ -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 => {
Expand Down
7 changes: 7 additions & 0 deletions src/migrations/molecules/toggle/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down

0 comments on commit 27563bc

Please sign in to comment.