reactive-ruby 0.7.28 → 0.7.29
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.codeclimate.yml +6 -0
- data/Gemfile.lock +4 -1
- data/README.md +132 -68
- data/Rakefile +5 -2
- data/example/examples/Gemfile +0 -2
- data/example/rails-tutorial/Gemfile +3 -2
- data/lib/generators/reactive_ruby/test_app/templates/application.rb +11 -0
- data/lib/generators/reactive_ruby/test_app/templates/assets/javascripts/application.rb +2 -0
- data/lib/generators/reactive_ruby/test_app/templates/assets/javascripts/components.rb +3 -0
- data/lib/generators/reactive_ruby/test_app/templates/boot.rb +6 -0
- data/lib/generators/reactive_ruby/test_app/templates/script/rails +5 -0
- data/lib/generators/reactive_ruby/test_app/templates/views/components/hello_world.rb +11 -0
- data/lib/generators/reactive_ruby/test_app/templates/views/components/todo.rb +14 -0
- data/lib/generators/reactive_ruby/test_app/test_app_generator.rb +105 -0
- data/lib/rails-helpers/top_level_rails_component.rb +9 -16
- data/lib/{reactive-ruby → react}/api.rb +8 -65
- data/lib/{reactive-ruby → react}/callbacks.rb +0 -0
- data/lib/react/component.rb +266 -0
- data/lib/react/component/api.rb +48 -0
- data/lib/react/component/class_methods.rb +183 -0
- data/lib/{reactive-ruby → react}/element.rb +10 -11
- data/lib/{reactive-ruby → react}/event.rb +0 -0
- data/lib/{reactive-ruby → react}/ext/hash.rb +0 -0
- data/lib/{reactive-ruby → react}/ext/string.rb +0 -0
- data/lib/react/native_library.rb +57 -0
- data/lib/{reactive-ruby → react}/observable.rb +0 -4
- data/lib/{reactive-ruby → react}/rendering_context.rb +0 -6
- data/lib/{reactive-ruby → react}/state.rb +3 -6
- data/lib/{reactive-ruby → react}/top_level.rb +2 -3
- data/lib/react/validator.rb +127 -0
- data/lib/reactive-ruby.rb +20 -20
- data/lib/reactive-ruby/version.rb +1 -1
- data/{opal-spec/reactjs → spec}/index.html.erb +1 -1
- data/{opal-spec → spec/react}/callbacks_spec.rb +8 -9
- data/{opal-spec → spec/react}/component_spec.rb +170 -120
- data/spec/react/dsl_spec.rb +16 -0
- data/{opal-spec → spec/react}/element_spec.rb +7 -20
- data/{opal-spec → spec/react}/event_spec.rb +3 -1
- data/spec/react/native_library_spec.rb +10 -0
- data/spec/react/param_declaration_spec.rb +18 -0
- data/{opal-spec → spec/react}/react_spec.rb +3 -2
- data/spec/react/react_state_spec.rb +22 -0
- data/spec/react/top_level_component_spec.rb +68 -0
- data/{opal-spec → spec/react}/tutorial/tutorial_spec.rb +11 -13
- data/{opal-spec → spec/react}/validator_spec.rb +50 -4
- data/spec/reactive-ruby/isomorphic_helpers_spec.rb +22 -4
- data/spec/spec_helper.rb +51 -0
- data/spec/support/react/spec_helpers.rb +57 -0
- data/spec/vendor/es5-shim.min.js +6 -0
- metadata +56 -24
- data/lib/reactive-ruby/component.rb +0 -502
- data/lib/reactive-ruby/validator.rb +0 -99
- data/old-readme +0 -220
- data/opal-spec/spec_helper.rb +0 -29
data/lib/reactive-ruby.rb
CHANGED
@@ -1,25 +1,25 @@
|
|
1
1
|
if RUBY_ENGINE == 'opal'
|
2
|
-
require
|
3
|
-
require
|
4
|
-
require
|
5
|
-
require
|
6
|
-
require
|
7
|
-
require
|
8
|
-
require
|
9
|
-
require
|
10
|
-
require
|
11
|
-
require
|
12
|
-
require
|
13
|
-
require
|
14
|
-
require
|
2
|
+
require 'sources/react.js'
|
3
|
+
require 'react/top_level'
|
4
|
+
require 'react/component'
|
5
|
+
require 'react/element'
|
6
|
+
require 'react/event'
|
7
|
+
require 'react/api'
|
8
|
+
require 'react/validator'
|
9
|
+
require 'react/observable'
|
10
|
+
require 'react/rendering_context'
|
11
|
+
require 'react/state'
|
12
|
+
require 'reactive-ruby/isomorphic_helpers'
|
13
|
+
require 'rails-helpers/top_level_rails_component'
|
14
|
+
require 'reactive-ruby/version'
|
15
15
|
else
|
16
|
-
require
|
17
|
-
require
|
18
|
-
require
|
19
|
-
require
|
20
|
-
require
|
21
|
-
require
|
22
|
-
require
|
16
|
+
require 'opal'
|
17
|
+
require 'opal-browser'
|
18
|
+
require 'opal-activesupport'
|
19
|
+
require 'reactive-ruby/version'
|
20
|
+
require 'reactive-ruby/rails' if defined?(Rails)
|
21
|
+
require 'reactive-ruby/isomorphic_helpers'
|
22
|
+
require 'reactive-ruby/serializers'
|
23
23
|
|
24
24
|
Opal.append_path File.expand_path('../', __FILE__).untaint
|
25
25
|
end
|
@@ -1,17 +1,15 @@
|
|
1
|
-
require
|
2
|
-
require "react/callbacks"
|
1
|
+
require 'spec_helper'
|
3
2
|
|
3
|
+
if opal?
|
4
4
|
describe React::Callbacks do
|
5
|
-
it
|
5
|
+
it 'defines callback' do
|
6
6
|
stub_const 'Foo', Class.new
|
7
7
|
Foo.class_eval do
|
8
8
|
include React::Callbacks
|
9
9
|
define_callback :before_dinner
|
10
|
-
|
11
10
|
before_dinner :wash_hand
|
12
11
|
|
13
12
|
def wash_hand
|
14
|
-
|
15
13
|
end
|
16
14
|
end
|
17
15
|
|
@@ -19,7 +17,7 @@ describe React::Callbacks do
|
|
19
17
|
Foo.new.run_callback(:before_dinner)
|
20
18
|
end
|
21
19
|
|
22
|
-
it
|
20
|
+
it 'defines multiple callbacks' do
|
23
21
|
stub_const 'Foo', Class.new
|
24
22
|
Foo.class_eval do
|
25
23
|
include React::Callbacks
|
@@ -37,7 +35,7 @@ describe React::Callbacks do
|
|
37
35
|
Foo.new.run_callback(:before_dinner)
|
38
36
|
end
|
39
37
|
|
40
|
-
it
|
38
|
+
it 'defines block callback' do
|
41
39
|
stub_const 'Foo', Class.new
|
42
40
|
Foo.class_eval do
|
43
41
|
include React::Callbacks
|
@@ -60,7 +58,7 @@ describe React::Callbacks do
|
|
60
58
|
expect(foo.b).to eq(20)
|
61
59
|
end
|
62
60
|
|
63
|
-
it
|
61
|
+
it 'defines multiple callback group' do
|
64
62
|
stub_const 'Foo', Class.new
|
65
63
|
Foo.class_eval do
|
66
64
|
include React::Callbacks
|
@@ -80,7 +78,7 @@ describe React::Callbacks do
|
|
80
78
|
expect(foo.a).to eq(10)
|
81
79
|
end
|
82
80
|
|
83
|
-
it
|
81
|
+
it 'receives args as callback' do
|
84
82
|
stub_const 'Foo', Class.new
|
85
83
|
Foo.class_eval do
|
86
84
|
include React::Callbacks
|
@@ -105,3 +103,4 @@ describe React::Callbacks do
|
|
105
103
|
expect(foo.lorem).to eq('1-2')
|
106
104
|
end
|
107
105
|
end
|
106
|
+
end
|
@@ -1,46 +1,47 @@
|
|
1
|
-
require
|
1
|
+
require 'spec_helper'
|
2
2
|
|
3
|
+
if opal?
|
3
4
|
describe React::Component do
|
4
5
|
after(:each) do
|
5
6
|
React::API.clear_component_class_cache
|
6
7
|
end
|
7
8
|
|
8
|
-
it
|
9
|
+
it 'defines component spec methods' do
|
9
10
|
stub_const 'Foo', Class.new
|
10
11
|
Foo.class_eval do
|
11
12
|
include React::Component
|
12
13
|
def render
|
13
|
-
React.create_element(
|
14
|
+
React.create_element('div')
|
14
15
|
end
|
15
16
|
end
|
16
17
|
|
17
18
|
# Class Methods
|
18
|
-
expect(Foo).to respond_to(
|
19
|
-
expect(Foo).to respond_to(
|
20
|
-
expect(Foo).to respond_to(
|
19
|
+
expect(Foo).to respond_to('initial_state')
|
20
|
+
expect(Foo).to respond_to('default_props')
|
21
|
+
expect(Foo).to respond_to('prop_types')
|
21
22
|
|
22
23
|
# Instance method
|
23
|
-
expect(Foo.new).to respond_to(
|
24
|
-
expect(Foo.new).to respond_to(
|
25
|
-
expect(Foo.new).to respond_to(
|
26
|
-
expect(Foo.new).to respond_to(
|
27
|
-
expect(Foo.new).to respond_to(
|
28
|
-
expect(Foo.new).to respond_to(
|
29
|
-
expect(Foo.new).to respond_to(
|
24
|
+
expect(Foo.new).to respond_to('component_will_mount')
|
25
|
+
expect(Foo.new).to respond_to('component_did_mount')
|
26
|
+
expect(Foo.new).to respond_to('component_will_receive_props')
|
27
|
+
expect(Foo.new).to respond_to('should_component_update?')
|
28
|
+
expect(Foo.new).to respond_to('component_will_update')
|
29
|
+
expect(Foo.new).to respond_to('component_did_update')
|
30
|
+
expect(Foo.new).to respond_to('component_will_unmount')
|
30
31
|
end
|
31
32
|
|
32
|
-
describe
|
33
|
+
describe 'Life Cycle' do
|
33
34
|
before(:each) do
|
34
35
|
stub_const 'Foo', Class.new
|
35
36
|
Foo.class_eval do
|
36
37
|
include React::Component
|
37
38
|
def render
|
38
|
-
React.create_element(
|
39
|
+
React.create_element('div') { 'lorem' }
|
39
40
|
end
|
40
41
|
end
|
41
42
|
end
|
42
43
|
|
43
|
-
it
|
44
|
+
it 'invokes `before_mount` registered methods when `componentWillMount()`' do
|
44
45
|
Foo.class_eval do
|
45
46
|
before_mount :bar, :bar2
|
46
47
|
def bar; end
|
@@ -53,7 +54,7 @@ describe React::Component do
|
|
53
54
|
renderToDocument(Foo)
|
54
55
|
end
|
55
56
|
|
56
|
-
it
|
57
|
+
it 'invokes `after_mount` registered methods when `componentDidMount()`' do
|
57
58
|
Foo.class_eval do
|
58
59
|
after_mount :bar3, :bar4
|
59
60
|
def bar3; end
|
@@ -66,7 +67,7 @@ describe React::Component do
|
|
66
67
|
renderToDocument(Foo)
|
67
68
|
end
|
68
69
|
|
69
|
-
it
|
70
|
+
it 'allows multiple class declared life cycle hooker' do
|
70
71
|
stub_const 'FooBar', Class.new
|
71
72
|
Foo.class_eval do
|
72
73
|
before_mount :bar
|
@@ -78,7 +79,7 @@ describe React::Component do
|
|
78
79
|
after_mount :bar2
|
79
80
|
def bar2; end
|
80
81
|
def render
|
81
|
-
React.create_element(
|
82
|
+
React.create_element('div') { 'lorem' }
|
82
83
|
end
|
83
84
|
end
|
84
85
|
|
@@ -87,45 +88,45 @@ describe React::Component do
|
|
87
88
|
renderToDocument(Foo)
|
88
89
|
end
|
89
90
|
|
90
|
-
it
|
91
|
+
it 'allows block for life cycle callback' do
|
91
92
|
Foo.class_eval do
|
92
|
-
|
93
|
+
export_state(:foo)
|
93
94
|
|
94
95
|
before_mount do
|
95
|
-
|
96
|
+
foo! 'bar'
|
96
97
|
end
|
97
98
|
end
|
98
99
|
|
99
100
|
element = renderToDocument(Foo)
|
100
|
-
expect(
|
101
|
+
expect(Foo.foo).to be('bar')
|
101
102
|
end
|
102
103
|
end
|
103
104
|
|
104
|
-
describe
|
105
|
+
describe 'State setter & getter' do
|
105
106
|
before(:each) do
|
106
107
|
stub_const 'Foo', Class.new
|
107
108
|
Foo.class_eval do
|
108
109
|
include React::Component
|
109
110
|
def render
|
110
|
-
React.create_element(
|
111
|
+
React.create_element('div') { 'lorem' }
|
111
112
|
end
|
112
113
|
end
|
113
114
|
end
|
114
115
|
|
115
|
-
it
|
116
|
+
it 'defines setter using `define_state`' do
|
116
117
|
Foo.class_eval do
|
117
118
|
define_state :foo
|
118
119
|
before_mount :set_up
|
119
120
|
def set_up
|
120
|
-
self.foo =
|
121
|
+
self.foo = 'bar'
|
121
122
|
end
|
122
123
|
end
|
123
124
|
|
124
125
|
element = renderToDocument(Foo)
|
125
|
-
expect(element.state.foo).to be(
|
126
|
+
expect(element.state.foo).to be('bar')
|
126
127
|
end
|
127
128
|
|
128
|
-
it
|
129
|
+
it 'defines init state by passing a block to `define_state`' do
|
129
130
|
Foo.class_eval do
|
130
131
|
define_state(:foo) { 10 }
|
131
132
|
end
|
@@ -134,7 +135,7 @@ describe React::Component do
|
|
134
135
|
expect(element.state.foo).to be(10)
|
135
136
|
end
|
136
137
|
|
137
|
-
it
|
138
|
+
it 'defines getter using `define_state`' do
|
138
139
|
Foo.class_eval do
|
139
140
|
define_state(:foo) { 10 }
|
140
141
|
before_mount :bump
|
@@ -147,7 +148,7 @@ describe React::Component do
|
|
147
148
|
expect(element.state.foo).to be(30)
|
148
149
|
end
|
149
150
|
|
150
|
-
it
|
151
|
+
it 'defines multiple state accessors by passing array to `define_state`' do
|
151
152
|
Foo.class_eval do
|
152
153
|
define_state :foo, :foo2
|
153
154
|
before_mount :set_up
|
@@ -162,7 +163,7 @@ describe React::Component do
|
|
162
163
|
expect(element.state.foo2).to be(20)
|
163
164
|
end
|
164
165
|
|
165
|
-
it
|
166
|
+
it 'invokes `define_state` multiple times to define states' do
|
166
167
|
Foo.class_eval do
|
167
168
|
define_state(:foo) { 30 }
|
168
169
|
define_state(:foo2) { 40 }
|
@@ -173,54 +174,66 @@ describe React::Component do
|
|
173
174
|
expect(element.state.foo2).to be(40)
|
174
175
|
end
|
175
176
|
|
176
|
-
it
|
177
|
-
|
178
|
-
|
179
|
-
|
180
|
-
|
181
|
-
|
177
|
+
it 'can initialize multiple state variables with a block' do
|
178
|
+
Foo.class_eval do
|
179
|
+
define_state(:foo, :foo2) { 30 }
|
180
|
+
end
|
181
|
+
element = renderToDocument(Foo)
|
182
|
+
expect(element.state.foo).to be(30)
|
183
|
+
expect(element.state.foo2).to be(30)
|
184
|
+
end
|
185
|
+
|
186
|
+
it 'can mix multiple state variables with initializers and a block' do
|
187
|
+
Foo.class_eval do
|
188
|
+
define_state(:x, :y, foo: 1, bar: 2) {3}
|
189
|
+
end
|
190
|
+
element = renderToDocument(Foo)
|
191
|
+
expect(element.state.x).to be(3)
|
192
|
+
expect(element.state.y).to be(3)
|
193
|
+
expect(element.state.foo).to be(1)
|
194
|
+
expect(element.state.bar).to be(2)
|
182
195
|
end
|
183
196
|
|
184
|
-
it
|
197
|
+
it 'gets state in render method' do
|
185
198
|
Foo.class_eval do
|
186
199
|
define_state(:foo) { 10 }
|
187
200
|
def render
|
188
|
-
React.create_element(
|
201
|
+
React.create_element('div') { self.foo }
|
189
202
|
end
|
190
203
|
end
|
191
204
|
|
192
205
|
element = renderToDocument(Foo)
|
193
|
-
expect(element.getDOMNode.textContent).to eq(
|
206
|
+
expect(element.getDOMNode.textContent).to eq('10')
|
194
207
|
end
|
195
208
|
|
196
|
-
it
|
209
|
+
it 'supports original `setState` as `set_state` method' do
|
197
210
|
Foo.class_eval do
|
198
211
|
before_mount do
|
199
|
-
self.set_state(foo:
|
212
|
+
self.set_state(foo: 'bar')
|
200
213
|
end
|
201
214
|
end
|
202
215
|
|
203
216
|
element = renderToDocument(Foo)
|
204
|
-
expect(element.state.foo).to be(
|
217
|
+
expect(element.state.foo).to be('bar')
|
205
218
|
end
|
206
219
|
|
207
|
-
it
|
220
|
+
it 'supports original `replaceState` as `set_state!` method' do
|
208
221
|
Foo.class_eval do
|
209
222
|
before_mount do
|
210
|
-
self.set_state(foo:
|
211
|
-
self.set_state!(bar:
|
223
|
+
self.set_state(foo: 'bar')
|
224
|
+
self.set_state!(bar: 'lorem')
|
212
225
|
end
|
213
226
|
end
|
214
227
|
|
215
228
|
element = renderToDocument(Foo)
|
216
229
|
expect(element.state.foo).to be_nil
|
217
|
-
expect(element.state.bar).to eq(
|
230
|
+
expect(element.state.bar).to eq('lorem')
|
218
231
|
end
|
219
232
|
|
220
|
-
it
|
233
|
+
it 'supports original `state` method' do
|
221
234
|
Foo.class_eval do
|
222
235
|
before_mount do
|
223
|
-
self.set_state(foo:
|
236
|
+
self.set_state(foo: 'bar')
|
224
237
|
end
|
225
238
|
|
226
239
|
def render
|
@@ -228,15 +241,15 @@ describe React::Component do
|
|
228
241
|
end
|
229
242
|
end
|
230
243
|
|
231
|
-
expect(React.render_to_static_markup(React.create_element(Foo))).to eq(
|
244
|
+
expect(React.render_to_static_markup(React.create_element(Foo))).to eq('<div>bar</div>')
|
232
245
|
end
|
233
246
|
|
234
|
-
it
|
247
|
+
it 'transforms state getter to Ruby object' do
|
235
248
|
Foo.class_eval do
|
236
249
|
define_state :foo
|
237
250
|
|
238
251
|
before_mount do
|
239
|
-
self.foo = [{a:
|
252
|
+
self.foo = [{a: "Hello"}]
|
240
253
|
end
|
241
254
|
|
242
255
|
def render
|
@@ -244,12 +257,12 @@ describe React::Component do
|
|
244
257
|
end
|
245
258
|
end
|
246
259
|
|
247
|
-
expect(React.render_to_static_markup(React.create_element(Foo))).to eq(
|
260
|
+
expect(React.render_to_static_markup(React.create_element(Foo))).to eq('<div>Hello</div>')
|
248
261
|
end
|
249
262
|
end
|
250
263
|
|
251
|
-
describe
|
252
|
-
describe
|
264
|
+
describe 'Props' do
|
265
|
+
describe 'this.props could be accessed through `params` method' do
|
253
266
|
before do
|
254
267
|
stub_const 'Foo', Class.new
|
255
268
|
Foo.class_eval do
|
@@ -257,30 +270,30 @@ describe React::Component do
|
|
257
270
|
end
|
258
271
|
end
|
259
272
|
|
260
|
-
it
|
273
|
+
it 'reads from parent passed properties through `params`' do
|
261
274
|
Foo.class_eval do
|
262
275
|
def render
|
263
|
-
React.create_element(
|
276
|
+
React.create_element('div') { params[:prop] }
|
264
277
|
end
|
265
278
|
end
|
266
279
|
|
267
|
-
element = renderToDocument(Foo, prop:
|
268
|
-
expect(element.getDOMNode.textContent).to eq(
|
280
|
+
element = renderToDocument(Foo, prop: 'foobar')
|
281
|
+
expect(element.getDOMNode.textContent).to eq('foobar')
|
269
282
|
end
|
270
283
|
|
271
|
-
it
|
284
|
+
it 'accesses nested params as orignal Ruby object' do
|
272
285
|
Foo.class_eval do
|
273
286
|
def render
|
274
|
-
React.create_element(
|
287
|
+
React.create_element('div') { params[:prop][0][:foo] }
|
275
288
|
end
|
276
289
|
end
|
277
290
|
|
278
291
|
element = renderToDocument(Foo, prop: [{foo: 10}])
|
279
|
-
expect(element.getDOMNode.textContent).to eq(
|
292
|
+
expect(element.getDOMNode.textContent).to eq('10')
|
280
293
|
end
|
281
294
|
end
|
282
295
|
|
283
|
-
describe
|
296
|
+
describe 'Props Updating' do
|
284
297
|
before do
|
285
298
|
stub_const 'Foo', Class.new
|
286
299
|
Foo.class_eval do
|
@@ -288,32 +301,32 @@ describe React::Component do
|
|
288
301
|
end
|
289
302
|
end
|
290
303
|
|
291
|
-
it
|
304
|
+
it 'supports original `setProps` as method `set_props`' do
|
292
305
|
Foo.class_eval do
|
293
306
|
def render
|
294
|
-
React.create_element(
|
307
|
+
React.create_element('div') { params[:foo] }
|
295
308
|
end
|
296
309
|
end
|
297
310
|
|
298
311
|
element = renderToDocument(Foo, {foo: 10})
|
299
312
|
element.set_props(foo: 20)
|
300
|
-
expect(element.dom_node.innerHTML).to eq('20')
|
313
|
+
expect(`#{element.dom_node}.innerHTML`).to eq('20')
|
301
314
|
end
|
302
315
|
|
303
|
-
it
|
316
|
+
it 'supports original `replaceProps` as method `set_props!`' do
|
304
317
|
Foo.class_eval do
|
305
318
|
def render
|
306
|
-
React.create_element(
|
319
|
+
React.create_element('div') { params[:foo] ? 'exist' : 'null' }
|
307
320
|
end
|
308
321
|
end
|
309
322
|
|
310
323
|
element = renderToDocument(Foo, {foo: 10})
|
311
324
|
element.set_props!(bar: 20)
|
312
|
-
expect(element.
|
325
|
+
expect(element.getDOMNode.innerHTML).to eq('null')
|
313
326
|
end
|
314
327
|
end
|
315
328
|
|
316
|
-
describe
|
329
|
+
describe 'Prop validation' do
|
317
330
|
before do
|
318
331
|
stub_const 'Foo', Class.new
|
319
332
|
Foo.class_eval do
|
@@ -321,7 +334,7 @@ describe React::Component do
|
|
321
334
|
end
|
322
335
|
end
|
323
336
|
|
324
|
-
it
|
337
|
+
it 'specifies validation rules using `params` class method' do
|
325
338
|
Foo.class_eval do
|
326
339
|
params do
|
327
340
|
requires :foo, type: String
|
@@ -332,7 +345,7 @@ describe React::Component do
|
|
332
345
|
expect(Foo.prop_types).to have_key(:_componentValidator)
|
333
346
|
end
|
334
347
|
|
335
|
-
it
|
348
|
+
it 'logs error in warning if validation failed' do
|
336
349
|
stub_const 'Lorem', Class.new
|
337
350
|
Foo.class_eval do
|
338
351
|
params do
|
@@ -351,10 +364,10 @@ describe React::Component do
|
|
351
364
|
}
|
352
365
|
renderToDocument(Foo, bar: 10, lorem: Lorem.new)
|
353
366
|
`window.console = org_console;`
|
354
|
-
expect(`log`).to eq(["Warning: Failed propType: In component `Foo`\nRequired prop `foo` was not specified\nProvided prop `bar`
|
367
|
+
expect(`log`).to eq(["Warning: Failed propType: In component `Foo`\nRequired prop `foo` was not specified\nProvided prop `bar` could not be converted to String"])
|
355
368
|
end
|
356
369
|
|
357
|
-
it
|
370
|
+
it 'should not log anything if validation pass' do
|
358
371
|
stub_const 'Lorem', Class.new
|
359
372
|
Foo.class_eval do
|
360
373
|
params do
|
@@ -371,34 +384,34 @@ describe React::Component do
|
|
371
384
|
var org_console = window.console;
|
372
385
|
window.console = {warn: function(str){log.push(str)}}
|
373
386
|
}
|
374
|
-
renderToDocument(Foo, foo: 10, bar:
|
387
|
+
renderToDocument(Foo, foo: 10, bar: '10', lorem: Lorem.new)
|
375
388
|
`window.console = org_console;`
|
376
389
|
expect(`log`).to eq([])
|
377
390
|
end
|
378
391
|
end
|
379
392
|
|
380
|
-
describe
|
381
|
-
it
|
393
|
+
describe 'Default props' do
|
394
|
+
it 'sets default props using validation helper' do
|
382
395
|
stub_const 'Foo', Class.new
|
383
396
|
Foo.class_eval do
|
384
397
|
include React::Component
|
385
398
|
params do
|
386
|
-
optional :foo, default:
|
387
|
-
optional :bar, default:
|
399
|
+
optional :foo, default: 'foo'
|
400
|
+
optional :bar, default: 'bar'
|
388
401
|
end
|
389
402
|
|
390
403
|
def render
|
391
|
-
div { params[:foo] +
|
404
|
+
div { params[:foo] + '-' + params[:bar]}
|
392
405
|
end
|
393
406
|
end
|
394
407
|
|
395
|
-
expect(React.render_to_static_markup(React.create_element(Foo, foo:
|
396
|
-
expect(React.render_to_static_markup(React.create_element(Foo))).to eq(
|
408
|
+
expect(React.render_to_static_markup(React.create_element(Foo, foo: 'lorem'))).to eq('<div>lorem-bar</div>')
|
409
|
+
expect(React.render_to_static_markup(React.create_element(Foo))).to eq('<div>foo-bar</div>')
|
397
410
|
end
|
398
411
|
end
|
399
412
|
end
|
400
413
|
|
401
|
-
describe
|
414
|
+
describe 'Event handling' do
|
402
415
|
before do
|
403
416
|
stub_const 'Foo', Class.new
|
404
417
|
Foo.class_eval do
|
@@ -406,12 +419,12 @@ describe React::Component do
|
|
406
419
|
end
|
407
420
|
end
|
408
421
|
|
409
|
-
it
|
422
|
+
it 'works in render method' do
|
410
423
|
Foo.class_eval do
|
411
424
|
define_state(:clicked) { false }
|
412
425
|
|
413
426
|
def render
|
414
|
-
React.create_element(
|
427
|
+
React.create_element('div').on(:click) do
|
415
428
|
self.clicked = true
|
416
429
|
end
|
417
430
|
end
|
@@ -423,46 +436,46 @@ describe React::Component do
|
|
423
436
|
expect(instance.state.clicked).to eq(true)
|
424
437
|
end
|
425
438
|
|
426
|
-
it
|
439
|
+
it 'invokes handler on `this.props` using emit' do
|
427
440
|
Foo.class_eval do
|
428
441
|
after_mount :setup
|
429
442
|
|
430
443
|
def setup
|
431
|
-
self.emit(:foo_submit,
|
444
|
+
self.emit(:foo_submit, 'bar')
|
432
445
|
end
|
433
446
|
|
434
447
|
def render
|
435
|
-
React.create_element(
|
448
|
+
React.create_element('div')
|
436
449
|
end
|
437
450
|
end
|
438
451
|
|
439
452
|
expect { |b|
|
440
453
|
element = React.create_element(Foo).on(:foo_submit, &b)
|
441
454
|
renderElementToDocument(element)
|
442
|
-
}.to yield_with_args(
|
455
|
+
}.to yield_with_args('bar')
|
443
456
|
end
|
444
457
|
|
445
|
-
it
|
458
|
+
it 'invokes handler with multiple params using emit' do
|
446
459
|
Foo.class_eval do
|
447
460
|
after_mount :setup
|
448
461
|
|
449
462
|
def setup
|
450
|
-
self.emit(:foo_invoked, [1,2,3],
|
463
|
+
self.emit(:foo_invoked, [1,2,3], 'bar')
|
451
464
|
end
|
452
465
|
|
453
466
|
def render
|
454
|
-
React.create_element(
|
467
|
+
React.create_element('div')
|
455
468
|
end
|
456
469
|
end
|
457
470
|
|
458
471
|
expect { |b|
|
459
472
|
element = React.create_element(Foo).on(:foo_invoked, &b)
|
460
473
|
renderElementToDocument(element)
|
461
|
-
}.to yield_with_args([1,2,3],
|
474
|
+
}.to yield_with_args([1,2,3], 'bar')
|
462
475
|
end
|
463
476
|
end
|
464
477
|
|
465
|
-
describe
|
478
|
+
describe '#refs' do
|
466
479
|
before do
|
467
480
|
stub_const 'Foo', Class.new
|
468
481
|
Foo.class_eval do
|
@@ -470,10 +483,10 @@ describe React::Component do
|
|
470
483
|
end
|
471
484
|
end
|
472
485
|
|
473
|
-
it
|
486
|
+
it 'correctly assigns refs' do
|
474
487
|
Foo.class_eval do
|
475
488
|
def render
|
476
|
-
React.create_element(
|
489
|
+
React.create_element('input', type: :text, ref: :field)
|
477
490
|
end
|
478
491
|
end
|
479
492
|
|
@@ -481,11 +494,11 @@ describe React::Component do
|
|
481
494
|
expect(element.refs.field).not_to be_nil
|
482
495
|
end
|
483
496
|
|
484
|
-
it
|
497
|
+
it 'accesses refs through `refs` method' do
|
485
498
|
Foo.class_eval do
|
486
499
|
def render
|
487
|
-
React.create_element(
|
488
|
-
refs[:field].value =
|
500
|
+
React.create_element('input', type: :text, ref: :field).on(:click) do
|
501
|
+
refs[:field].value = 'some_stuff'
|
489
502
|
end
|
490
503
|
end
|
491
504
|
end
|
@@ -493,12 +506,12 @@ describe React::Component do
|
|
493
506
|
element = renderToDocument(Foo)
|
494
507
|
simulateEvent(:click, element)
|
495
508
|
|
496
|
-
expect(element.refs.field.value).to eq(
|
509
|
+
expect(element.refs.field.value).to eq('some_stuff')
|
497
510
|
end
|
498
511
|
end
|
499
512
|
|
500
|
-
describe
|
501
|
-
it
|
513
|
+
describe '#render' do
|
514
|
+
it 'supports element building helpers' do
|
502
515
|
stub_const 'Foo', Class.new
|
503
516
|
Foo.class_eval do
|
504
517
|
include React::Component
|
@@ -515,15 +528,15 @@ describe React::Component do
|
|
515
528
|
include React::Component
|
516
529
|
def render
|
517
530
|
div do
|
518
|
-
present Foo, foo:
|
531
|
+
present Foo, foo: 'astring'
|
519
532
|
end
|
520
533
|
end
|
521
534
|
end
|
522
535
|
|
523
|
-
expect(React.render_to_static_markup(React.create_element(Bar))).to eq(
|
536
|
+
expect(React.render_to_static_markup(React.create_element(Bar))).to eq('<div><div><span>astring</span></div></div>')
|
524
537
|
end
|
525
538
|
|
526
|
-
it
|
539
|
+
it 'builds single node in top-level render without providing a block' do
|
527
540
|
stub_const 'Foo', Class.new
|
528
541
|
Foo.class_eval do
|
529
542
|
include React::Component
|
@@ -534,38 +547,39 @@ describe React::Component do
|
|
534
547
|
end
|
535
548
|
|
536
549
|
element = React.create_element(Foo)
|
537
|
-
expect(React.render_to_static_markup(element)).to eq(
|
550
|
+
expect(React.render_to_static_markup(element)).to eq('<div></div>')
|
538
551
|
end
|
539
552
|
|
540
|
-
it
|
553
|
+
it 'redefines `p` to make method missing work' do
|
541
554
|
stub_const 'Foo', Class.new
|
542
555
|
Foo.class_eval do
|
543
556
|
include React::Component
|
544
557
|
|
545
558
|
def render
|
546
|
-
p(class_name:
|
559
|
+
p(class_name: 'foo') do
|
547
560
|
p
|
548
|
-
div {
|
549
|
-
p(id:
|
561
|
+
div { 'lorem ipsum' }
|
562
|
+
p(id: '10')
|
550
563
|
end
|
551
564
|
end
|
552
565
|
end
|
553
566
|
|
554
567
|
element = React.create_element(Foo)
|
555
|
-
|
568
|
+
markup = '<p class="foo"><p></p><div>lorem ipsum</div><p id="10"></p></p>'
|
569
|
+
expect(React.render_to_static_markup(element)).to eq(markup)
|
556
570
|
end
|
557
571
|
|
558
|
-
it
|
572
|
+
it 'only overrides `p` in render context' do
|
559
573
|
stub_const 'Foo', Class.new
|
560
574
|
Foo.class_eval do
|
561
575
|
include React::Component
|
562
576
|
|
563
577
|
before_mount do
|
564
|
-
p
|
578
|
+
p 'first'
|
565
579
|
end
|
566
580
|
|
567
581
|
after_mount do
|
568
|
-
p
|
582
|
+
p 'second'
|
569
583
|
end
|
570
584
|
|
571
585
|
def render
|
@@ -573,20 +587,20 @@ describe React::Component do
|
|
573
587
|
end
|
574
588
|
end
|
575
589
|
|
576
|
-
expect(Kernel).to receive(:p).with(
|
577
|
-
expect(Kernel).to receive(:p).with(
|
590
|
+
expect(Kernel).to receive(:p).with('first')
|
591
|
+
expect(Kernel).to receive(:p).with('second')
|
578
592
|
renderToDocument(Foo)
|
579
593
|
end
|
580
594
|
end
|
581
595
|
|
582
|
-
describe
|
583
|
-
it
|
596
|
+
describe 'isMounted()' do
|
597
|
+
it 'returns true if after mounted' do
|
584
598
|
stub_const 'Foo', Class.new
|
585
599
|
Foo.class_eval do
|
586
600
|
include React::Component
|
587
601
|
|
588
602
|
def render
|
589
|
-
React.create_element(
|
603
|
+
React.create_element('div')
|
590
604
|
end
|
591
605
|
end
|
592
606
|
|
@@ -594,4 +608,40 @@ describe React::Component do
|
|
594
608
|
expect(component.mounted?).to eq(true)
|
595
609
|
end
|
596
610
|
end
|
611
|
+
|
612
|
+
describe '#children' do
|
613
|
+
before(:each) do
|
614
|
+
stub_const 'Foo', Class.new
|
615
|
+
Foo.class_eval do
|
616
|
+
include React::Component
|
617
|
+
export_state :the_children
|
618
|
+
before_mount do
|
619
|
+
the_children! children
|
620
|
+
end
|
621
|
+
def render
|
622
|
+
React.create_element('div') { 'lorem' }
|
623
|
+
end
|
624
|
+
end
|
625
|
+
end
|
626
|
+
|
627
|
+
it 'returns an Enumerable' do
|
628
|
+
ele = React.create_element(Foo) {
|
629
|
+
[React.create_element('a'), React.create_element('li')]
|
630
|
+
}
|
631
|
+
renderElementToDocument(ele)
|
632
|
+
nodes = Foo.the_children.map { |ele| ele.element_type }
|
633
|
+
expect(nodes).to eq(['a', 'li'])
|
634
|
+
end
|
635
|
+
|
636
|
+
it 'returns an Enumerator when not providing a block' do
|
637
|
+
ele = React.create_element(Foo) {
|
638
|
+
[React.create_element('a'), React.create_element('li')]
|
639
|
+
}
|
640
|
+
renderElementToDocument(ele)
|
641
|
+
nodes = Foo.the_children.each
|
642
|
+
expect(nodes).to be_a(Enumerator)
|
643
|
+
expect(nodes.size).to eq(2)
|
644
|
+
end
|
645
|
+
end
|
646
|
+
end
|
597
647
|
end
|