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

Commit de9a8e2

Browse files
committed
begin building component unit test support
1 parent 98a421b commit de9a8e2

File tree

3 files changed

+101
-0
lines changed

3 files changed

+101
-0
lines changed

lib/react/test/session.rb

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
module React
2+
module Test
3+
class Session
4+
def mount(component_klass, params = {})
5+
@element = React.create_element(component_klass, params)
6+
component
7+
end
8+
9+
def instance
10+
@instance ||= Native(`React.addons.TestUtils.renderIntoDocument(#{element.to_n})`)
11+
end
12+
13+
def element
14+
@element
15+
end
16+
17+
def component
18+
@component ||= `#{instance.to_n}._getOpalInstance.apply(#{instance})`
19+
end
20+
21+
def update_params(params)
22+
component.set_props(params)
23+
end
24+
end
25+
end
26+
end
27+

spec/react/test/session_spec.rb

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
require 'spec_helper'
2+
3+
if opal?
4+
RSpec.describe React::Test::Session do
5+
subject { described_class.new }
6+
before do
7+
stub_const 'Greeter', Class.new
8+
Greeter.class_eval do
9+
include React::Component
10+
11+
params do
12+
optional :message
13+
optional :from
14+
end
15+
16+
def render
17+
span { "Hello #{params.message}" }
18+
end
19+
end
20+
end
21+
22+
describe '#mount' do
23+
it 'returns an instance of the mounted component' do
24+
expect(subject.mount(Greeter)).to be_a(Greeter)
25+
end
26+
27+
it 'actualy mounts the component' do
28+
expect(subject.mount(Greeter)).to be_mounted
29+
end
30+
31+
it 'optionaly passes params to the component' do
32+
component = subject.mount(Greeter, message: 'world')
33+
expect(component.params.message).to eq('world')
34+
end
35+
end
36+
37+
describe '#component' do
38+
it 'returns the instance of the mounted component' do
39+
component = subject.mount(Greeter)
40+
expect(subject.component).to eq(component)
41+
end
42+
end
43+
44+
describe '#element' do
45+
it 'returns the React::Element for the mounted component' do
46+
subject.mount(Greeter)
47+
expect(subject.element).to be_a(React::Element)
48+
end
49+
end
50+
51+
describe '#instance' do
52+
it 'returns the React native instance of the component' do
53+
component = subject.mount(Greeter)
54+
native = component.instance_variable_get('@native')
55+
expect(subject.instance).to eq(native)
56+
end
57+
end
58+
59+
describe '#update_params' do
60+
it 'sends new params to the component' do
61+
component = subject.mount(Greeter, message: 'world')
62+
subject.update_params(message: 'moon')
63+
expect(component.params.message).to eq('moon')
64+
end
65+
66+
it 'leaves unspecified params in tact' do
67+
component = subject.mount(Greeter, message: 'world', from: 'outerspace')
68+
subject.update_params(message: 'moon')
69+
expect(component.params.from).to eq('outerspace')
70+
end
71+
end
72+
end
73+
end

spec/spec_helper.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ def ruby?
1313

1414
if RUBY_ENGINE == 'opal'
1515
require 'reactive-ruby'
16+
require 'react/test/session'
1617
require 'react/test/matchers/render_html_matcher'
1718
require File.expand_path('../support/react/spec_helpers', __FILE__)
1819

0 commit comments

Comments
 (0)