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

Commit eb2c54c

Browse files
committed
Fix #175
1 parent 89c72c5 commit eb2c54c

File tree

2 files changed

+40
-4
lines changed

2 files changed

+40
-4
lines changed

lib/react/state.rb

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,16 @@ def []=(state, new_value)
1515

1616
def method_missing(method, *args)
1717
if match = method.match(/^(.+)\!$/)
18+
key_name = $1
1819
if args.count > 0
1920
current_value = State.get_state(@from, match[1])
20-
State.set_state(@from, $1, args[0])
21+
State.set_state(@from, key_name, args[0])
2122
current_value
2223
else
2324
current_state = State.get_state(@from, match[1])
24-
State.set_state(@from, $1, current_state)
25+
State.set_state(@from, key_name, current_state)
2526
Observable.new(current_state) do |update|
26-
State.set_state(@from, $1, update)
27+
State.set_state(@from, key_name, update)
2728
end
2829
end
2930
else

spec/react/observable_spec.rb

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,42 @@
11
require 'spec_helper'
22

33
if opal?
4+
45
describe 'React::Observable' do
5-
# tbd
6+
it "allows to set value on Observable" do
7+
stub_const 'Zoo', Class.new {
8+
include React::Component
9+
param :foo, type: React::Observable
10+
before_mount do
11+
params.foo! 4
12+
end
13+
14+
def render
15+
nil
16+
end
17+
}
18+
19+
stub_const 'Foo', Class.new
20+
Foo.class_eval do
21+
include React::Component
22+
23+
def render
24+
div do
25+
Zoo(foo: state.foo! )
26+
span { state.foo.to_s }
27+
end
28+
end
29+
end
30+
31+
instance = React::Test::Utils.render_into_document(React.create_element(Foo))
32+
html = `#{instance.dom_node}.innerHTML`
33+
# data-reactid appear in earlier versions of reactjs
34+
%x{
35+
var REGEX_REMOVE_IDS = /\s?data-reactid="[^"]+"/g;
36+
html = html.replace(REGEX_REMOVE_IDS, '');
37+
}
38+
expect(html).to eq('<span></span><span>4</span>')
39+
end
640
end
41+
742
end

0 commit comments

Comments
 (0)