improved react-addons-update tests
Moshe Kolodny committed
Dec 20, 2015 at 21:50 UTC
7c0189fbad0652a4c8458d437f43b757f492f46e
1 file changed
+138
-48
src/addons/__tests__/update-test.js
+138
-48
@@ -14,68 +14,158 @@
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
- 'update(): expected spec of $push to be an array; got 7. Did you ' +
21
- 'forget to wrap your parameter in an array?'
22
- );
23
- expect(update.bind(null, 1, {$push: 7})).toThrow(
24
- 'update(): expected target of $push to be an array; got 1.'
25
- );
17
+
18
+ describe('$push', function() {
19
+ it('pushes', function() {
20
+ expect(update([1], {$push: [7]})).toEqual([1, 7]);
21
+ });
22
+ it('does not mutate the original object', function() {
23
+ var obj = [1];
24
+ update(obj, {$push: [7]});
25
+ expect(obj).toEqual([1]);
26
+ });
27
+ it('only pushes an array', function() {
28
+ expect(update.bind(null, [], {$push: 7})).toThrow(
29
+ 'update(): expected spec of $push to be an array; got 7. Did you ' +
30
+ 'forget to wrap your parameter in an array?'
31
+ );
32
+ });
33
+ it('only pushes unto an array', function() {
34
+ expect(update.bind(null, 1, {$push: 7})).toThrow(
35
+ 'update(): expected target of $push to be an array; got 1.'
36
+ );
37
+ });
38
});
39
28
- it('should support unshift', function() {
29
- expect(update([1], {$unshift: [7]})).toEqual([7, 1]);
30
- expect(update.bind(null, [], {$unshift: 7})).toThrow(
31
- 'update(): expected spec of $unshift to be an array; got 7. Did you ' +
32
- 'forget to wrap your parameter in an array?'
33
- );
34
- expect(update.bind(null, 1, {$unshift: 7})).toThrow(
35
- 'update(): expected target of $unshift to be an array; got 1.'
36
- );
40
+ describe('$unshift', function() {
41
+ it('unshifts', function() {
42
+ expect(update([1], {$unshift: [7]})).toEqual([7, 1]);
43
+ });
44
+ it('does not mutate the original object', function() {
45
+ var obj = [1];
46
+ update(obj, {$unshift: [7]});
47
+ expect(obj).toEqual([1]);
48
+ });
49
+ it('only unshifts an array', function() {
50
+ expect(update.bind(null, [], {$unshift: 7})).toThrow(
51
+ 'update(): expected spec of $unshift to be an array; got 7. Did you ' +
52
+ 'forget to wrap your parameter in an array?'
53
+ );
54
+ });
55
+ it('only unshifts unto an array', function() {
56
+ expect(update.bind(null, 1, {$unshift: 7})).toThrow(
57
+ 'update(): expected target of $unshift to be an array; got 1.'
58
+ );
59
+ });
60
});
61
39
- it('should support splice', function() {
40
- expect(update([1, 4, 3], {$splice: [[1, 1, 2]]})).toEqual([1, 2, 3]);
41
- expect(update.bind(null, [], {$splice: 1})).toThrow(
42
- 'update(): expected spec of $splice to be an array of arrays; got 1. ' +
43
- 'Did you forget to wrap your parameters in an array?'
44
- );
45
- expect(update.bind(null, [], {$splice: [1]})).toThrow(
46
- 'update(): expected spec of $splice to be an array of arrays; got 1. ' +
47
- 'Did you forget to wrap your parameters in an array?'
48
- );
49
- expect(update.bind(null, 1, {$splice: 7})).toThrow(
50
- 'Expected $splice target to be an array; got 1'
51
- );
62
+ describe('$splice', function() {
63
+ it('splices', function() {
64
+ expect(update([1, 4, 3], {$splice: [[1, 1, 2]]})).toEqual([1, 2, 3]);
65
+ });
66
+ it('does not mutate the original object', function() {
67
+ var obj = [1, 4, 3];
68
+ update(obj, {$splice: [[1, 1, 2]]});
69
+ expect(obj).toEqual([1, 4, 3]);
70
+ });
71
+ it('only splices an array of arrays', function() {
72
+ expect(update.bind(null, [], {$splice: 1})).toThrow(
73
+ 'update(): expected spec of $splice to be an array of arrays; got 1. ' +
74
+ 'Did you forget to wrap your parameters in an array?'
75
+ );
76
+ expect(update.bind(null, [], {$splice: [1]})).toThrow(
77
+ 'update(): expected spec of $splice to be an array of arrays; got 1. ' +
78
+ 'Did you forget to wrap your parameters in an array?'
79
+ );
80
+ });
81
+ it('only splices unto an array', function() {
82
+ expect(update.bind(null, 1, {$splice: 7})).toThrow(
83
+ 'Expected $splice target to be an array; got 1'
84
+ );
85
+ });
86
});
87
54
- it('should support merge', function() {
55
- expect(update({a: 'b'}, {$merge: {c: 'd'}})).toEqual({a: 'b', c: 'd'});
56
- expect(update.bind(null, {}, {$merge: 7})).toThrow(
57
- 'update(): $merge expects a spec of type \'object\'; got 7'
58
- );
59
- expect(update.bind(null, 7, {$merge: {a: 'b'}})).toThrow(
60
- 'update(): $merge expects a target of type \'object\'; got 7'
61
- );
88
+ describe('$merge', function() {
89
+ it('merges', function() {
90
+ expect(update({a: 'b'}, {$merge: {c: 'd'}})).toEqual({a: 'b', c: 'd'});
91
+ });
92
+ it('does not mutate the original object', function() {
93
+ var obj = {a: 'b'};
94
+ update(obj, {$merge: {c: 'd'}});
95
+ expect(obj).toEqual({a: 'b'});
96
+ });
97
+ it('only merges with an object', function() {
98
+ expect(update.bind(null, {}, {$merge: 7})).toThrow(
99
+ 'update(): $merge expects a spec of type \'object\'; got 7'
100
+ );
101
+ });
102
+ it('only merges with an object', function() {
103
+ expect(update.bind(null, 7, {$merge: {a: 'b'}})).toThrow(
104
+ 'update(): $merge expects a target of type \'object\'; got 7'
105
+ );
106
+ });
107
});
108
64
- it('should support set', function() {
65
- expect(update({a: 'b'}, {$set: {c: 'd'}})).toEqual({c: 'd'});
109
+ describe('$set', function() {
110
+ it('sets', function() {
111
+ expect(update({a: 'b'}, {$set: {c: 'd'}})).toEqual({c: 'd'});
112
+ });
113
+ it('does not mutate the original object', function() {
114
+ var obj = {a: 'b'};
115
+ update(obj, {$set: {c: 'd'}});
116
+ expect(obj).toEqual({a: 'b'});
117
+ });
118
});
119
68
- it('should support apply', function() {
69
- expect(update(2, {$apply: (x) => x * 2})).toEqual(4);
70
- expect(update.bind(null, 2, {$apply: 123})).toThrow(
71
- 'update(): expected spec of $apply to be a function; got 123.'
72
- );
120
+ describe('$apply', function() {
121
+ var applier = function(node) {
122
+ return {v: node.v * 2};
123
+ };
124
+ it('applies', function() {
125
+ expect(update({v: 2}, {$apply: applier})).toEqual({v: 4});
126
+ });
127
+ it('does not mutate the original object', function() {
128
+ var obj = {v: 2};
129
+ update(obj, {$apply: applier});
130
+ expect(obj).toEqual({v: 2});
131
+ });
132
+ it('only applies a function', function() {
133
+ expect(update.bind(null, 2, {$apply: 123})).toThrow(
134
+ 'update(): expected spec of $apply to be a function; got 123.'
135
+ );
136
+ });
137
});
138
139
it('should support deep updates', function() {
76
- expect(update({a: 'b', c: {d: 'e'}}, {c: {d: {$set: 'f'}}})).toEqual({
140
+ expect(update({
141
+ a: 'b',
142
+ c: {
143
+ d: 'e',
144
+ f: [1],
145
+ g: [2],
146
+ h: [3],
147
+ i: {j: 'k'},
148
+ l: 4,
149
+ },
150
+ }, {
151
+ c: {
152
+ d: {$set: 'm'},
153
+ f: {$push: [5]},
154
+ g: {$unshift: [6]},
155
+ h: {$splice: [[0, 1, 7]]},
156
+ i: {$merge: {n: 'o'}},
157
+ l: {$apply: (x) => x * 2},
158
+ },
159
+ })).toEqual({
160
a: 'b',
78
- c: {d: 'f'},
161
+ c: {
162
+ d: 'm',
163
+ f: [1, 5],
164
+ g: [6, 2],
165
+ h: [7],
166
+ i: {j: 'k', n: 'o'},
167
+ l: 8,
168
+ },
169
});
170
});
171