Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
81155 views
1
/**
2
* Copyright 2013-2014, Facebook, Inc.
3
* All rights reserved.
4
*
5
* This source code is licensed under the BSD-style license found in the
6
* LICENSE file in the root directory of this source tree. An additional grant
7
* of patent rights can be found in the PATENTS file in the same directory.
8
*
9
* @emails react-core
10
*/
11
12
"use strict";
13
14
var update = require('update');
15
16
describe('update', function() {
17
it('should support push', function() {
18
expect(update([1], {$push: [7]})).toEqual([1, 7]);
19
expect(update.bind(null, [], {$push: 7})).toThrow(
20
'Invariant Violation: update(): expected spec of $push to be an ' +
21
'array; got 7. Did you forget to wrap your parameter in an array?'
22
);
23
expect(update.bind(null, 1, {$push: 7})).toThrow(
24
'Invariant Violation: update(): expected target of $push to be an ' +
25
'array; got 1.'
26
);
27
});
28
29
it('should support unshift', function() {
30
expect(update([1], {$unshift: [7]})).toEqual([7, 1]);
31
expect(update.bind(null, [], {$unshift: 7})).toThrow(
32
'Invariant Violation: update(): expected spec of $unshift to be an ' +
33
'array; got 7. Did you forget to wrap your parameter in an array?'
34
);
35
expect(update.bind(null, 1, {$unshift: 7})).toThrow(
36
'Invariant Violation: update(): expected target of $unshift to be an ' +
37
'array; got 1.'
38
);
39
});
40
41
it('should support splice', function() {
42
expect(update([1, 4, 3], {$splice: [[1, 1, 2]]})).toEqual([1, 2, 3]);
43
expect(update.bind(null, [], {$splice: 1})).toThrow(
44
'Invariant Violation: update(): expected spec of $splice to be an ' +
45
'array of arrays; got 1. Did you forget to wrap your parameters in an '+
46
'array?'
47
);
48
expect(update.bind(null, [], {$splice: [1]})).toThrow(
49
'Invariant Violation: update(): expected spec of $splice to be an ' +
50
'array of arrays; got 1. Did you forget to wrap your parameters in an ' +
51
'array?'
52
);
53
expect(update.bind(null, 1, {$splice: 7})).toThrow(
54
'Invariant Violation: Expected $splice target to be an array; got 1'
55
);
56
});
57
58
it('should support merge', function() {
59
expect(update({a: 'b'}, {$merge: {c: 'd'}})).toEqual({a: 'b', c: 'd'});
60
expect(update.bind(null, {}, {$merge: 7})).toThrow(
61
'Invariant Violation: update(): $merge expects a spec of type ' +
62
'\'object\'; got 7'
63
);
64
expect(update.bind(null, 7, {$merge: {a: 'b'}})).toThrow(
65
'Invariant Violation: update(): $merge expects a target of type ' +
66
'\'object\'; got 7'
67
);
68
});
69
70
it('should support set', function() {
71
expect(update({a: 'b'}, {$set: {c: 'd'}})).toEqual({c: 'd'});
72
});
73
74
it('should support apply', function() {
75
expect(update(2, {$apply: function(x) { return x * 2; }})).toEqual(4);
76
expect(update.bind(null, 2, {$apply: 123})).toThrow(
77
'Invariant Violation: update(): expected spec of $apply to be a ' +
78
'function; got 123.'
79
);
80
});
81
82
it('should support deep updates', function() {
83
expect(update({a: 'b', c: {d: 'e'}}, {c: {d: {$set: 'f'}}})).toEqual({
84
a: 'b',
85
c: {d: 'f'}
86
});
87
});
88
89
it('should require a command', function() {
90
expect(update.bind(null, {a: 'b'}, {a: 'c'})).toThrow(
91
'Invariant Violation: update(): You provided a key path to update() ' +
92
'that did not contain one of $push, $unshift, $splice, $set, $merge, ' +
93
'$apply. Did you forget to include {$set: ...}?'
94
);
95
});
96
});
97
98