Merge pull request #5697 from kolodny/stricter-update-tests

improved react-addons-update tests
This commit is contained in:
Paul O’Shannessy
2015-12-20 21:11:29 -08:00

View File

@@ -14,68 +14,158 @@
var update = require('update');
describe('update', function() {
it('should support push', function() {
expect(update([1], {$push: [7]})).toEqual([1, 7]);
expect(update.bind(null, [], {$push: 7})).toThrow(
'update(): expected spec of $push to be an array; got 7. Did you ' +
'forget to wrap your parameter in an array?'
);
expect(update.bind(null, 1, {$push: 7})).toThrow(
'update(): expected target of $push to be an array; got 1.'
);
describe('$push', function() {
it('pushes', function() {
expect(update([1], {$push: [7]})).toEqual([1, 7]);
});
it('does not mutate the original object', function() {
var obj = [1];
update(obj, {$push: [7]});
expect(obj).toEqual([1]);
});
it('only pushes an array', function() {
expect(update.bind(null, [], {$push: 7})).toThrow(
'update(): expected spec of $push to be an array; got 7. Did you ' +
'forget to wrap your parameter in an array?'
);
});
it('only pushes unto an array', function() {
expect(update.bind(null, 1, {$push: 7})).toThrow(
'update(): expected target of $push to be an array; got 1.'
);
});
});
it('should support unshift', function() {
expect(update([1], {$unshift: [7]})).toEqual([7, 1]);
expect(update.bind(null, [], {$unshift: 7})).toThrow(
'update(): expected spec of $unshift to be an array; got 7. Did you ' +
'forget to wrap your parameter in an array?'
);
expect(update.bind(null, 1, {$unshift: 7})).toThrow(
'update(): expected target of $unshift to be an array; got 1.'
);
describe('$unshift', function() {
it('unshifts', function() {
expect(update([1], {$unshift: [7]})).toEqual([7, 1]);
});
it('does not mutate the original object', function() {
var obj = [1];
update(obj, {$unshift: [7]});
expect(obj).toEqual([1]);
});
it('only unshifts an array', function() {
expect(update.bind(null, [], {$unshift: 7})).toThrow(
'update(): expected spec of $unshift to be an array; got 7. Did you ' +
'forget to wrap your parameter in an array?'
);
});
it('only unshifts unto an array', function() {
expect(update.bind(null, 1, {$unshift: 7})).toThrow(
'update(): expected target of $unshift to be an array; got 1.'
);
});
});
it('should support splice', function() {
expect(update([1, 4, 3], {$splice: [[1, 1, 2]]})).toEqual([1, 2, 3]);
expect(update.bind(null, [], {$splice: 1})).toThrow(
'update(): expected spec of $splice to be an array of arrays; got 1. ' +
'Did you forget to wrap your parameters in an array?'
);
expect(update.bind(null, [], {$splice: [1]})).toThrow(
'update(): expected spec of $splice to be an array of arrays; got 1. ' +
'Did you forget to wrap your parameters in an array?'
);
expect(update.bind(null, 1, {$splice: 7})).toThrow(
'Expected $splice target to be an array; got 1'
);
describe('$splice', function() {
it('splices', function() {
expect(update([1, 4, 3], {$splice: [[1, 1, 2]]})).toEqual([1, 2, 3]);
});
it('does not mutate the original object', function() {
var obj = [1, 4, 3];
update(obj, {$splice: [[1, 1, 2]]});
expect(obj).toEqual([1, 4, 3]);
});
it('only splices an array of arrays', function() {
expect(update.bind(null, [], {$splice: 1})).toThrow(
'update(): expected spec of $splice to be an array of arrays; got 1. ' +
'Did you forget to wrap your parameters in an array?'
);
expect(update.bind(null, [], {$splice: [1]})).toThrow(
'update(): expected spec of $splice to be an array of arrays; got 1. ' +
'Did you forget to wrap your parameters in an array?'
);
});
it('only splices unto an array', function() {
expect(update.bind(null, 1, {$splice: 7})).toThrow(
'Expected $splice target to be an array; got 1'
);
});
});
it('should support merge', function() {
expect(update({a: 'b'}, {$merge: {c: 'd'}})).toEqual({a: 'b', c: 'd'});
expect(update.bind(null, {}, {$merge: 7})).toThrow(
'update(): $merge expects a spec of type \'object\'; got 7'
);
expect(update.bind(null, 7, {$merge: {a: 'b'}})).toThrow(
'update(): $merge expects a target of type \'object\'; got 7'
);
describe('$merge', function() {
it('merges', function() {
expect(update({a: 'b'}, {$merge: {c: 'd'}})).toEqual({a: 'b', c: 'd'});
});
it('does not mutate the original object', function() {
var obj = {a: 'b'};
update(obj, {$merge: {c: 'd'}});
expect(obj).toEqual({a: 'b'});
});
it('only merges with an object', function() {
expect(update.bind(null, {}, {$merge: 7})).toThrow(
'update(): $merge expects a spec of type \'object\'; got 7'
);
});
it('only merges with an object', function() {
expect(update.bind(null, 7, {$merge: {a: 'b'}})).toThrow(
'update(): $merge expects a target of type \'object\'; got 7'
);
});
});
it('should support set', function() {
expect(update({a: 'b'}, {$set: {c: 'd'}})).toEqual({c: 'd'});
describe('$set', function() {
it('sets', function() {
expect(update({a: 'b'}, {$set: {c: 'd'}})).toEqual({c: 'd'});
});
it('does not mutate the original object', function() {
var obj = {a: 'b'};
update(obj, {$set: {c: 'd'}});
expect(obj).toEqual({a: 'b'});
});
});
it('should support apply', function() {
expect(update(2, {$apply: (x) => x * 2})).toEqual(4);
expect(update.bind(null, 2, {$apply: 123})).toThrow(
'update(): expected spec of $apply to be a function; got 123.'
);
describe('$apply', function() {
var applier = function(node) {
return {v: node.v * 2};
};
it('applies', function() {
expect(update({v: 2}, {$apply: applier})).toEqual({v: 4});
});
it('does not mutate the original object', function() {
var obj = {v: 2};
update(obj, {$apply: applier});
expect(obj).toEqual({v: 2});
});
it('only applies a function', function() {
expect(update.bind(null, 2, {$apply: 123})).toThrow(
'update(): expected spec of $apply to be a function; got 123.'
);
});
});
it('should support deep updates', function() {
expect(update({a: 'b', c: {d: 'e'}}, {c: {d: {$set: 'f'}}})).toEqual({
expect(update({
a: 'b',
c: {d: 'f'},
c: {
d: 'e',
f: [1],
g: [2],
h: [3],
i: {j: 'k'},
l: 4,
},
}, {
c: {
d: {$set: 'm'},
f: {$push: [5]},
g: {$unshift: [6]},
h: {$splice: [[0, 1, 7]]},
i: {$merge: {n: 'o'}},
l: {$apply: (x) => x * 2},
},
})).toEqual({
a: 'b',
c: {
d: 'm',
f: [1, 5],
g: [6, 2],
h: [7],
i: {j: 'k', n: 'o'},
l: 8,
},
});
});