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

Commit 0673e88

Browse files
committed
Add React::Test::Utils
1 parent 4b86d72 commit 0673e88

File tree

3 files changed

+42
-20
lines changed

3 files changed

+42
-20
lines changed

lib/react/test/utils.rb

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
module React
2+
module Test
3+
class Utils
4+
`var ReactTestUtils = React.addons.TestUtils`
5+
6+
def self.render_into_document(element, options = {})
7+
raise "You should pass a valid React::Element" unless React.is_valid_element(element)
8+
native_instance = `ReactTestUtils.renderIntoDocument(#{element.to_n})`
9+
10+
if `#{native_instance}._getOpalInstance !== undefined`
11+
`#{native_instance}._getOpalInstance()`
12+
else
13+
native_instance
14+
end
15+
end
16+
17+
def self.simulate(event, element)
18+
Simulate.new.click(element)
19+
end
20+
21+
class Simulate
22+
include Native
23+
def initialize
24+
super(`ReactTestUtils.Simulate`)
25+
end
26+
27+
def click(component_instance)
28+
`#{@native}['click']`.call(component_instance.dom_node, {})
29+
end
30+
end
31+
end
32+
end
33+
end

spec/react/test/utils_spec.rb

+8-20
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,6 @@
11
require 'spec_helper'
22

33
if opal?
4-
module React
5-
module Test
6-
class Utils
7-
def self.simulate(event, element)
8-
Simulate.new.click(element)
9-
end
10-
11-
class Simulate
12-
include Native
13-
def initialize
14-
super(`React.addons.TestUtils.Simulate`)
15-
end
16-
17-
def click(component_instance)
18-
`#{@native}['click']`.call(component_instance.dom_node, {})
19-
end
20-
end
21-
end
22-
end
23-
end
244
RSpec.describe React::Test::Utils do
255
it 'simulates' do
266
stub_const 'Foo', Class.new
@@ -41,5 +21,13 @@ def render
4121
expect_any_instance_of(Foo).to receive(:click)
4222
described_class.simulate(:click, instance)
4323
end
24+
25+
describe "render_into_document" do
26+
it "works with native element" do
27+
expect {
28+
described_class.render_into_document(React.create_element('div'))
29+
}.to_not raise_error
30+
end
31+
end
4432
end
4533
end

spec/spec_helper.rb

+1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ def ruby?
1818
require 'react/react-source'
1919
require 'hyper-react'
2020
require 'react/test/rspec'
21+
require 'react/test/utils'
2122

2223
require File.expand_path('../support/react/spec_helpers', __FILE__)
2324

0 commit comments

Comments
 (0)