Skip to content
This repository was archived by the owner on Oct 19, 2018. It is now read-only.

Commit 2d295ad

Browse files
committed
Clean up testing
1 parent 9fc21f2 commit 2d295ad

File tree

6 files changed

+67
-80
lines changed

6 files changed

+67
-80
lines changed

lib/react/testing.rb

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
module React
2+
module Testing
3+
`var ReactTestUtils = React.addons.TestUtils`
4+
5+
def simulate_event(event_name, dom_element, event_data = {})
6+
simulator = Native(`ReactTestUtils.Simulate`)
7+
simulator[event_name].call(dom_element, event_data)
8+
end
9+
10+
def render_to_document(element)
11+
`ReactTestUtils.renderIntoDocument(element)`
12+
end
13+
end
14+
end

spec/component_spec.rb

Lines changed: 37 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
11
require "spec_helper"
22

33
describe React::Component do
4-
after(:each) do
5-
React::ComponentFactory.clear_component_class_cache
6-
end
7-
84
it "should define component spec methods" do
95
stub_const 'Foo', Class.new
106
Foo.class_eval do
@@ -50,7 +46,7 @@ def bar2; end
5046
expect_any_instance_of(Foo).to receive(:bar)
5147
expect_any_instance_of(Foo).to receive(:bar2)
5248

53-
renderToDocument(Foo)
49+
render_to_document(React.create_element(Foo))
5450
end
5551

5652
it "should invoke `after_mount` registered methods when `componentDidMount()`" do
@@ -63,7 +59,7 @@ def bar4; end
6359
expect_any_instance_of(Foo).to receive(:bar3)
6460
expect_any_instance_of(Foo).to receive(:bar4)
6561

66-
renderToDocument(Foo)
62+
render_to_document(React.create_element(Foo))
6763
end
6864

6965
it "should allow multiple class declared life cycle hooker" do
@@ -84,7 +80,7 @@ def render
8480

8581
expect_any_instance_of(Foo).to receive(:bar)
8682

87-
renderToDocument(Foo)
83+
render_to_document(React.create_element(Foo))
8884
end
8985

9086
it "should allow block for life cycle callback" do
@@ -96,8 +92,8 @@ def render
9692
end
9793
end
9894

99-
element = renderToDocument(Foo)
100-
expect(element.state[:foo]).to be("bar")
95+
instance = render_to_document(React.create_element(Foo))
96+
expect(instance.state[:foo]).to be("bar")
10197
end
10298
end
10399

@@ -121,17 +117,17 @@ def set_up
121117
end
122118
end
123119

124-
element = renderToDocument(Foo)
125-
expect(element.state[:foo]).to be("bar")
120+
instance = render_to_document(React.create_element(Foo))
121+
expect(instance.state[:foo]).to be("bar")
126122
end
127123

128124
it "should define init state by passing a block to `define_state`" do
129125
Foo.class_eval do
130126
define_state(:foo) { 10 }
131127
end
132128

133-
element = renderToDocument(Foo)
134-
expect(element.state[:foo]).to be(10)
129+
instance = render_to_document(React.create_element(Foo))
130+
expect(instance.state[:foo]).to be(10)
135131
end
136132

137133
it "should define getter using `define_state`" do
@@ -143,8 +139,8 @@ def bump
143139
end
144140
end
145141

146-
element = renderToDocument(Foo)
147-
expect(element.state[:foo]).to be(30)
142+
instance = render_to_document(React.create_element(Foo))
143+
expect(instance.state[:foo]).to be(30)
148144
end
149145

150146
it "should define multiple state accessor by passing symols array to `define_state`" do
@@ -157,9 +153,9 @@ def set_up
157153
end
158154
end
159155

160-
element = renderToDocument(Foo)
161-
expect(element.state[:foo]).to be(10)
162-
expect(element.state[:foo2]).to be(20)
156+
instance = render_to_document(React.create_element(Foo))
157+
expect(instance.state[:foo]).to be(10)
158+
expect(instance.state[:foo2]).to be(20)
163159
end
164160

165161
it "should invoke `define_state` multiple times to define states" do
@@ -168,9 +164,9 @@ def set_up
168164
define_state(:foo2) { 40 }
169165
end
170166

171-
element = renderToDocument(Foo)
172-
expect(element.state[:foo]).to be(30)
173-
expect(element.state[:foo2]).to be(40)
167+
instance = render_to_document(React.create_element(Foo))
168+
expect(instance.state[:foo]).to be(30)
169+
expect(instance.state[:foo2]).to be(40)
174170
end
175171

176172
it "should raise error if multiple states and block given at the same time" do
@@ -189,8 +185,8 @@ def render
189185
end
190186
end
191187

192-
element = renderToDocument(Foo)
193-
expect(element.dom_node.textContent).to eq("10")
188+
instance = render_to_document(React.create_element(Foo))
189+
expect(instance.dom_node.textContent).to eq("10")
194190
end
195191

196192
it "should support original `setState` as `set_state` method" do
@@ -200,8 +196,8 @@ def render
200196
end
201197
end
202198

203-
element = renderToDocument(Foo)
204-
expect(element.state[:foo]).to be("bar")
199+
instance = render_to_document(React.create_element(Foo))
200+
expect(instance.state[:foo]).to be("bar")
205201
end
206202

207203
it "should support originl `state` method" do
@@ -251,8 +247,8 @@ def render
251247
end
252248
end
253249

254-
element = renderToDocument(Foo, prop: "foobar")
255-
expect(element.dom_node.textContent).to eq("foobar")
250+
instance = render_to_document(React.create_element(Foo, prop: "foobar"))
251+
expect(instance.dom_node.textContent).to eq("foobar")
256252
end
257253

258254
it "should access nested params as orignal Ruby object" do
@@ -262,8 +258,8 @@ def render
262258
end
263259
end
264260

265-
element = renderToDocument(Foo, prop: [{foo: 10}])
266-
expect(element.dom_node.textContent).to eq("10")
261+
instance = render_to_document(React.create_element(Foo, prop: [{foo: 10}]))
262+
expect(instance.dom_node.textContent).to eq("10")
267263
end
268264
end
269265

@@ -303,7 +299,7 @@ def render; div; end
303299
var org_console = window.console;
304300
window.console = {warn: function(str){log.push(str)}}
305301
}
306-
renderToDocument(Foo, bar: 10, lorem: Lorem.new)
302+
render_to_document(React.create_element(Foo, bar: 10, lorem: Lorem.new))
307303
`window.console = org_console;`
308304
expect(`log`).to eq(["Warning: Failed propType: In component `Foo`\nRequired prop `foo` was not specified\nProvided prop `bar` was not the specified type `String`"])
309305
end
@@ -325,7 +321,7 @@ def render; div; end
325321
var org_console = window.console;
326322
window.console = {warn: function(str){log.push(str)}}
327323
}
328-
renderToDocument(Foo, foo: 10, bar: "10", lorem: Lorem.new)
324+
render_to_document(React.create_element(Foo, foo: 10, bar: "10", lorem: Lorem.new))
329325
`window.console = org_console;`
330326
expect(`log`).to eq([])
331327
end
@@ -371,9 +367,8 @@ def render
371367
end
372368
end
373369

374-
element = React.create_element(Foo)
375-
instance = renderElementToDocument(element)
376-
simulateEvent(:click, instance)
370+
instance = render_to_document(React.create_element(Foo))
371+
simulate_event(:click, React.find_dom_node(instance))
377372
expect(instance.state[:clicked]).to eq(true)
378373
end
379374

@@ -392,7 +387,7 @@ def render
392387

393388
expect { |b|
394389
element = React.create_element(Foo).on(:foo_submit, &b)
395-
renderElementToDocument(element)
390+
render_to_document(element)
396391
}.to yield_with_args("bar")
397392
end
398393

@@ -411,7 +406,7 @@ def render
411406

412407
expect { |b|
413408
element = React.create_element(Foo).on(:foo_invoked, &b)
414-
renderElementToDocument(element)
409+
render_to_document(element)
415410
}.to yield_with_args([1,2,3], "bar")
416411
end
417412
end
@@ -431,8 +426,8 @@ def render
431426
end
432427
end
433428

434-
element = renderToDocument(Foo)
435-
expect(element.refs[:field]).not_to be_nil
429+
instance = render_to_document(React.create_element(Foo))
430+
expect(instance.refs[:field]).not_to be_nil
436431
end
437432

438433
it "should access refs through `refs` method" do
@@ -444,10 +439,10 @@ def render
444439
end
445440
end
446441

447-
element = renderToDocument(Foo)
448-
simulateEvent(:click, element)
442+
instance = render_to_document(React.create_element(Foo))
443+
simulate_event(:click, React.find_dom_node(instance))
449444

450-
expect(element.refs[:field].value).to eq("some_stuff")
445+
expect(instance.refs[:field].value).to eq("some_stuff")
451446
end
452447
end
453448

@@ -529,7 +524,7 @@ def render
529524

530525
expect(Kernel).to receive(:p).with("first")
531526
expect(Kernel).to receive(:p).with("second")
532-
renderToDocument(Foo)
527+
render_to_document(React.create_element(Foo))
533528
end
534529
end
535530
end

spec/element_spec.rb

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -54,20 +54,20 @@
5454
it "should be subscribable through `on(:event_name)` method" do
5555
expect { |b|
5656
element = React.create_element("div").on(:click, &b)
57-
instance = renderElementToDocument(element)
58-
simulateEvent(:click, instance)
57+
instance = render_to_document(element)
58+
simulate_event(:click, React.find_dom_node(instance))
5959
}.to yield_with_args(React::Event)
6060

6161
expect { |b|
6262
element = React.create_element("div").on(:key_down, &b)
63-
instance = renderElementToDocument(element)
64-
simulateEvent(:keyDown, instance, {key: "Enter"})
63+
instance = render_to_document(element)
64+
simulate_event(:keyDown, React.find_dom_node(instance), {key: "Enter"})
6565
}.to yield_control
6666

6767
expect { |b|
6868
element = React.create_element("form").on(:submit, &b)
69-
instance = renderElementToDocument(element)
70-
simulateEvent(:submit, instance, {})
69+
instance = render_to_document(element)
70+
simulate_event(:submit, React.find_dom_node(instance), {})
7171
}.to yield_control
7272
end
7373

spec/event_spec.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
expect(event).to respond_to(:prevent_default)
1717
expect(event).to respond_to(:stop_propagation)
1818
end
19-
instance = renderElementToDocument(element)
20-
simulateEvent(:click, instance)
19+
instance = render_to_document(element)
20+
simulate_event(:click, React.find_dom_node(instance))
2121
end
22-
end
22+
end

spec/react_spec.rb

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
11
require "spec_helper"
22

33
describe React do
4-
after(:each) do
5-
React::ComponentFactory.clear_component_class_cache
6-
end
7-
84
describe "is_valid_element" do
95
it "should return true if passed a valid element" do
106
element = `React.createElement('div')`
@@ -105,8 +101,8 @@ def render
105101
end
106102
end
107103

108-
renderToDocument(Foo)
109-
renderToDocument(Foo)
104+
render_to_document(React.create_element(Foo))
105+
render_to_document(React.create_element(Foo))
110106

111107
expect(`count`).to eq(2)
112108
end

spec/spec_helper.rb

Lines changed: 5 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,9 @@
11
require 'react'
2-
3-
module ReactTestHelpers
4-
`var ReactTestUtils = React.addons.TestUtils`
5-
6-
def renderToDocument(type, options = {})
7-
element = React.create_element(type, options)
8-
return renderElementToDocument(element)
9-
end
10-
11-
def renderElementToDocument(element)
12-
`ReactTestUtils.renderIntoDocument(#{element})`
13-
end
14-
15-
def simulateEvent(event, component, params = {})
16-
simulator = Native(`ReactTestUtils.Simulate`)
17-
simulator[event.to_s].call(`React.findDOMNode(#{component})`, params)
18-
end
19-
20-
def isElementOfType(element, type)
21-
`React.addons.TestUtils.isElementOfType(#{element}, #{type.cached_component_class})`
22-
end
23-
end
2+
require 'react/testing'
243

254
RSpec.configure do |config|
26-
config.include ReactTestHelpers
5+
config.include React::Testing
6+
config.after :each do
7+
React::ComponentFactory.clear_component_class_cache
8+
end
279
end

0 commit comments

Comments
 (0)