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

Commit ccdf5cc

Browse files
authored
Merge pull request #254 from Donnadieu/edge
Added to_key methods and tests
2 parents 9caec7b + f839d2b commit ccdf5cc

File tree

4 files changed

+77
-1
lines changed

4 files changed

+77
-1
lines changed

lib/react/api.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ class extends React.Component {
7171
this.__opalInstanceSyncSetState = true;
7272
this.__opalInstance.$component_will_mount();
7373
this.__opalInstanceSyncSetState = false;
74-
}
74+
}
7575
}
7676
componentDidMount() {
7777
this.__opalInstance.is_mounted = true
@@ -171,6 +171,10 @@ def self.convert_props(properties)
171171
props["className"] = value
172172
elsif ["style", "dangerously_set_inner_HTML"].include? key
173173
props[lower_camelize(key)] = value.to_n
174+
175+
elsif key == "key"
176+
props["key"] = value.to_key
177+
174178
elsif key == 'ref' && value.is_a?(Proc)
175179
props[key] = %x{
176180
function(dom_node){

lib/react/object.rb

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,19 @@ def const_missing(const_name)
1212
React::Component::Tags.html_tag_class_for(const_name) || raise(e)
1313
end
1414
end
15+
16+
def to_key
17+
object_id
18+
end
19+
end
20+
class Number
21+
def to_key
22+
self
23+
end
24+
end
25+
26+
class Boolean
27+
def to_key
28+
self
29+
end
1530
end

spec/react/key_to_react_spec.rb

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
require 'spec_helper'
2+
3+
describe 'key_to_react helper', js: true do
4+
it "will use the use the to_key method to get the react key" do
5+
mount "TestComponent" do
6+
class MyTestClass
7+
attr_reader :to_key_called
8+
def to_key
9+
@to_key_called = true
10+
super
11+
end
12+
end
13+
class TestComponent < Hyperloop::Component
14+
before_mount { @test_object = MyTestClass.new }
15+
render do
16+
DIV(key: @test_object) { TestComponent2(test_object: @test_object) }
17+
end
18+
end
19+
class TestComponent2 < Hyperloop::Component
20+
param :test_object
21+
render do
22+
"to key was called!" if params.test_object.to_key_called
23+
end
24+
end
25+
end
26+
pause
27+
expect(page).to have_content('to key was called!')
28+
end
29+
end

spec/react/to_key_spec.rb

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
require 'spec_helper'
2+
3+
describe 'to_key helper', js: true do
4+
it "has added 'to_key' method to Object and each key is different" do
5+
expect_evaluate_ruby do
6+
Object.new.to_key != Object.new.to_key
7+
end.to be_truthy
8+
end
9+
10+
it "to_key return 'self' for String objects" do
11+
expect_evaluate_ruby do
12+
debugger
13+
"hello".to_key == "hello"
14+
end.to be_truthy
15+
end
16+
17+
it "to_key return 'self' for Number objects" do
18+
expect_evaluate_ruby do
19+
12.to_key == 12
20+
end.to be_truthy
21+
end
22+
23+
it "to_key return 'self' for Boolean objects" do
24+
expect_evaluate_ruby do
25+
true.to_key == true && false.to_key == false
26+
end.to be_truthy
27+
end
28+
end

0 commit comments

Comments
 (0)