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 React = require('React');
15
var ReactStateSetters = require('ReactStateSetters');
16
var ReactTestUtils = require('ReactTestUtils');
17
18
var TestComponent;
19
var TestComponentWithMixin;
20
21
describe('ReactStateSetters', function() {
22
beforeEach(function() {
23
require('mock-modules').dumpCache();
24
25
TestComponent = React.createClass({
26
getInitialState: function() {
27
return {foo: 'foo'};
28
},
29
30
render: function() {
31
return <div />;
32
}
33
});
34
35
TestComponentWithMixin = React.createClass({
36
mixins: [ReactStateSetters.Mixin],
37
38
getInitialState: function() {
39
return {foo: 'foo'};
40
},
41
42
render: function() {
43
return <div />;
44
}
45
});
46
});
47
48
it('createStateSetter should update state', function() {
49
var instance = <TestComponent />;
50
instance = ReactTestUtils.renderIntoDocument(instance);
51
expect(instance.state).toEqual({foo: 'foo'});
52
53
var setter = ReactStateSetters.createStateSetter(
54
instance,
55
function(a, b, c) {
56
return {
57
foo: a + b + c,
58
bar: a * b * c
59
};
60
}
61
);
62
expect(instance.state).toEqual({foo: 'foo'});
63
64
setter(1, 2, 3);
65
expect(instance.state).toEqual({foo: 6, bar: 6});
66
67
setter(10, 11, 12);
68
expect(instance.state).toEqual({foo: 33, bar: 1320});
69
});
70
71
it('createStateKeySetter should update state', function() {
72
var instance = <TestComponent />;
73
instance = ReactTestUtils.renderIntoDocument(instance);
74
expect(instance.state).toEqual({foo: 'foo'});
75
76
var setter = ReactStateSetters.createStateKeySetter(instance, 'foo');
77
78
expect(instance.state).toEqual({foo: 'foo'});
79
80
setter('bar');
81
expect(instance.state).toEqual({foo: 'bar'});
82
83
setter('baz');
84
expect(instance.state).toEqual({foo: 'baz'});
85
});
86
87
it('createStateKeySetter is memoized', function() {
88
var instance = <TestComponent />;
89
instance = ReactTestUtils.renderIntoDocument(instance);
90
expect(instance.state).toEqual({foo: 'foo'});
91
92
var foo1 = ReactStateSetters.createStateKeySetter(instance, 'foo');
93
var bar1 = ReactStateSetters.createStateKeySetter(instance, 'bar');
94
95
var foo2 = ReactStateSetters.createStateKeySetter(instance, 'foo');
96
var bar2 = ReactStateSetters.createStateKeySetter(instance, 'bar');
97
98
expect(foo2).toBe(foo1);
99
expect(bar2).toBe(bar1);
100
});
101
102
it('createStateSetter should update state from mixin', function() {
103
var instance = <TestComponentWithMixin />;
104
instance = ReactTestUtils.renderIntoDocument(instance);
105
expect(instance.state).toEqual({foo: 'foo'});
106
107
var setter = instance.createStateSetter(
108
function(a, b, c) {
109
return {
110
foo: a + b + c,
111
bar: a * b * c
112
};
113
}
114
);
115
expect(instance.state).toEqual({foo: 'foo'});
116
117
setter(1, 2, 3);
118
expect(instance.state).toEqual({foo: 6, bar: 6});
119
120
setter(10, 11, 12);
121
expect(instance.state).toEqual({foo: 33, bar: 1320});
122
});
123
124
it('createStateKeySetter should update state with mixin', function() {
125
var instance = <TestComponentWithMixin />;
126
instance = ReactTestUtils.renderIntoDocument(instance);
127
expect(instance.state).toEqual({foo: 'foo'});
128
129
var setter = instance.createStateKeySetter('foo');
130
131
expect(instance.state).toEqual({foo: 'foo'});
132
133
setter('bar');
134
expect(instance.state).toEqual({foo: 'bar'});
135
136
setter('baz');
137
expect(instance.state).toEqual({foo: 'baz'});
138
});
139
140
it('createStateKeySetter is memoized with mixin', function() {
141
var instance = <TestComponentWithMixin />;
142
instance = ReactTestUtils.renderIntoDocument(instance);
143
expect(instance.state).toEqual({foo: 'foo'});
144
145
var foo1 = instance.createStateKeySetter('foo');
146
var bar1 = instance.createStateKeySetter('bar');
147
148
var foo2 = instance.createStateKeySetter('foo');
149
var bar2 = instance.createStateKeySetter('bar');
150
151
expect(foo2).toBe(foo1);
152
expect(bar2).toBe(bar1);
153
});
154
});
155
156