forked from facebook/react
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.js
More file actions
110 lines (94 loc) · 2.83 KB
/
test.js
File metadata and controls
110 lines (94 loc) · 2.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
/**
* Copyright (c) 2015-present, Facebook, Inc.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @emails react-core
*/
'use strict';
// Catch stray warnings
var env = jasmine.getEnv();
var callCount = 0;
var oldError = console.error;
var newError = function() {
callCount++;
oldError.apply(this, arguments);
};
console.error = newError;
env.beforeEach(() => {
callCount = 0;
jasmine.addMatchers({
toBeReset() {
return {
compare(actual) {
if (actual !== newError && !jasmine.isSpy(actual)) {
return {
pass: false,
message: 'Test did not tear down console.error mock properly.'
};
}
return {pass: true};
}
};
},
toNotHaveBeenCalled() {
return {
compare(actual) {
return {
pass: callCount === 0,
message:
'Expected test not to warn. If the warning is expected, mock ' +
"it out using spyOn(console, 'error'); and test that the " +
'warning occurs.'
};
}
};
}
});
});
env.afterEach(() => {
expect(console.error).toBeReset();
expect(console.error).toNotHaveBeenCalled();
});
const update = require(process.env.TEST_ENTRY);
describe('update', () => {
// https://facebook.github.io/react/docs/update.html#simple-push
it('should support simple push', () => {
const array = [1, 2, 3];
const newArray = update(array, {$push: [4]});
expect(array).toEqual([1, 2, 3]);
expect(newArray).toEqual([1, 2, 3, 4]);
});
// https://facebook.github.io/react/docs/update.html#nested-collections
it('should support nested collections', () => {
const collection = [1, 2, {a: [12, 17, 15]}];
const newCollection = update(collection, {
2: {a: {$splice: [[1, 1, 13, 14]]}}
});
expect(collection).toEqual([1, 2, {a: [12, 17, 15]}]);
expect(newCollection).toEqual([1, 2, {a: [12, 13, 14, 15]}]);
});
// https://facebook.github.io/react/docs/update.html#updating-a-value-based-on-its-current-one
it('should support updating a value based on its current one', () => {
const obj = {a: 5, b: 3};
const newObj = update(obj, {
b: {
$apply: function(x) {
return x * 2;
}
}
});
expect(newObj).toEqual({a: 5, b: 6});
const newObj2 = update(obj, {b: {$set: obj.b * 2}});
expect(newObj2).toEqual({a: 5, b: 6});
expect(obj).toEqual({a: 5, b: 3});
});
// https://facebook.github.io/react/docs/update.html#shallow-merge
it('should support shallow merge', () => {
const obj = {a: 5, b: 3};
const newObj = update(obj, {$merge: {b: 6, c: 7}});
expect(newObj).toEqual({a: 5, b: 6, c: 7});
expect(obj).toEqual({a: 5, b: 3});
});
});