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

Commit a4bdc9c

Browse files
committed
Migrate some test to use React::Test::Utils.render_into_document
1 parent 0673e88 commit a4bdc9c

File tree

3 files changed

+36
-30
lines changed

3 files changed

+36
-30
lines changed

spec/react/children_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ def render
1515
let(:children) { described_class.new(`#{element.to_n}.props.children`) }
1616

1717
before(:each) do
18-
renderElementToDocument(element)
18+
React::Test::Utils.render_into_document(element)
1919
end
2020

2121
it 'is enumerable' do

spec/react/component_spec.rb

Lines changed: 30 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ def render
3434
end
3535

3636
describe 'Life Cycle' do
37+
let(:element_to_render) { React.create_element(Foo) }
38+
3739
before(:each) do
3840
stub_const 'Foo', Class.new
3941
Foo.class_eval do
@@ -54,7 +56,7 @@ def bar2; end
5456
expect_any_instance_of(Foo).to receive(:bar)
5557
expect_any_instance_of(Foo).to receive(:bar2)
5658

57-
renderToDocument(Foo)
59+
React::Test::Utils.render_into_document(element_to_render)
5860
end
5961

6062
it 'invokes `after_mount` registered methods when `componentDidMount()`' do
@@ -67,7 +69,7 @@ def bar4; end
6769
expect_any_instance_of(Foo).to receive(:bar3)
6870
expect_any_instance_of(Foo).to receive(:bar4)
6971

70-
renderToDocument(Foo)
72+
React::Test::Utils.render_into_document(element_to_render)
7173
end
7274

7375
it 'allows multiple class declared life cycle hooker' do
@@ -88,7 +90,7 @@ def render
8890

8991
expect_any_instance_of(Foo).to receive(:bar)
9092

91-
renderToDocument(Foo)
93+
React::Test::Utils.render_into_document(element_to_render)
9294
end
9395

9496
it 'allows block for life cycle callback' do
@@ -169,6 +171,8 @@ def render
169171
end
170172

171173
describe 'State setter & getter' do
174+
let(:element_to_render) { React.create_element(Foo) }
175+
172176
before(:each) do
173177
stub_const 'Foo', Class.new
174178
Foo.class_eval do
@@ -188,17 +192,17 @@ def set_up
188192
end
189193
end
190194

191-
element = renderToDocument(Foo)
192-
expect(element.state.foo).to be('bar')
195+
instance = React::Test::Utils.render_into_document(element_to_render)
196+
expect(instance.state.foo).to be('bar')
193197
end
194198

195199
it 'defines init state by passing a block to `define_state`' do
196200
Foo.class_eval do
197201
define_state(:foo) { 10 }
198202
end
199203

200-
element = renderToDocument(Foo)
201-
expect(element.state.foo).to be(10)
204+
instance = React::Test::Utils.render_into_document(element_to_render)
205+
expect(instance.state.foo).to be(10)
202206
end
203207

204208
it 'defines getter using `define_state`' do
@@ -210,8 +214,8 @@ def bump
210214
end
211215
end
212216

213-
element = renderToDocument(Foo)
214-
expect(element.state.foo).to be(30)
217+
instance = React::Test::Utils.render_into_document(element_to_render)
218+
expect(instance.state.foo).to be(30)
215219
end
216220

217221
it 'defines multiple state accessors by passing array to `define_state`' do
@@ -224,9 +228,9 @@ def set_up
224228
end
225229
end
226230

227-
element = renderToDocument(Foo)
228-
expect(element.state.foo).to be(10)
229-
expect(element.state.foo2).to be(20)
231+
instance = React::Test::Utils.render_into_document(element_to_render)
232+
expect(instance.state.foo).to be(10)
233+
expect(instance.state.foo2).to be(20)
230234
end
231235

232236
it 'invokes `define_state` multiple times to define states' do
@@ -235,29 +239,30 @@ def set_up
235239
define_state(:foo2) { 40 }
236240
end
237241

238-
element = renderToDocument(Foo)
239-
expect(element.state.foo).to be(30)
240-
expect(element.state.foo2).to be(40)
242+
instance = React::Test::Utils.render_into_document(element_to_render)
243+
expect(instance.state.foo).to be(30)
244+
expect(instance.state.foo2).to be(40)
241245
end
242246

243247
it 'can initialize multiple state variables with a block' do
244248
Foo.class_eval do
245249
define_state(:foo, :foo2) { 30 }
246250
end
247-
element = renderToDocument(Foo)
248-
expect(element.state.foo).to be(30)
249-
expect(element.state.foo2).to be(30)
251+
252+
instance = React::Test::Utils.render_into_document(element_to_render)
253+
expect(instance.state.foo).to be(30)
254+
expect(instance.state.foo2).to be(30)
250255
end
251256

252257
it 'can mix multiple state variables with initializers and a block' do
253258
Foo.class_eval do
254259
define_state(:x, :y, foo: 1, bar: 2) {3}
255260
end
256-
element = renderToDocument(Foo)
257-
expect(element.state.x).to be(3)
258-
expect(element.state.y).to be(3)
259-
expect(element.state.foo).to be(1)
260-
expect(element.state.bar).to be(2)
261+
instance = React::Test::Utils.render_into_document(element_to_render)
262+
expect(instance.state.x).to be(3)
263+
expect(instance.state.y).to be(3)
264+
expect(instance.state.foo).to be(1)
265+
expect(instance.state.bar).to be(2)
261266
end
262267

263268
it 'gets state in render method' do
@@ -268,8 +273,8 @@ def render
268273
end
269274
end
270275

271-
element = renderToDocument(Foo)
272-
expect(`#{element.dom_node}.textContent`).to eq('10')
276+
instance = React::Test::Utils.render_into_document(element_to_render)
277+
expect(`#{instance.dom_node}.textContent`).to eq('10')
273278
end
274279

275280
it 'supports original `setState` as `set_state` method' do

spec/react/react_spec.rb

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,9 @@
4343
element = React.create_element('div') do
4444
[React.create_element('span'), React.create_element('span'), React.create_element('span')]
4545
end
46-
instance = renderElementToDocument(element)
47-
expect(`#{instance.dom_node}.children.length`).to eq(3)
46+
dom_node = React::Test::Utils.render_into_document(element)
47+
48+
expect(`#{dom_node}.children.length`).to eq(3)
4849
end
4950
end
5051

@@ -117,8 +118,8 @@ def render
117118
end
118119
end
119120

120-
renderToDocument(Foo)
121-
renderToDocument(Foo)
121+
React::Test::Utils.render_into_document(React.create_element(Foo))
122+
React::Test::Utils.render_into_document(React.create_element(Foo))
122123

123124
expect(`count`).to eq(2)
124125
end

0 commit comments

Comments
 (0)