Skip to content

Commit bce5448

Browse files
committed
[React::Test::Session] Minimize API surface & refactor
1 parent f6548d5 commit bce5448

File tree

2 files changed

+18
-31
lines changed

2 files changed

+18
-31
lines changed

lib/react/test/session.rb

Lines changed: 15 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
module React
22
module Test
33
class Session
4-
DSL_METHODS = %i[mount instance native element update_params
5-
force_update! html].freeze
4+
DSL_METHODS = %i[mount instance native update_params html].freeze
5+
6+
attr_reader :native
67

78
def mount(component_klass, params = {})
89
@element = React.create_element(component_klass, params)
@@ -11,35 +12,28 @@ def mount(component_klass, params = {})
1112

1213
def instance
1314
unless @instance
14-
@native = Native(`React.addons.TestUtils.renderIntoDocument(#{element.to_n})`)
15-
@instance = `#{@native.to_n}._getOpalInstance()`
15+
@native = `React.addons.TestUtils.renderIntoDocument(#{@element.to_n})`
16+
@instance = `#@native._getOpalInstance()`
1617
end
1718
@instance
1819
end
1920

20-
def native
21-
@native
22-
end
23-
24-
def element
25-
@element
26-
end
27-
2821
def update_params(params)
29-
cloned_element = React::Element.new(`React.cloneElement(#{self.element.to_n}, #{params.to_n})`)
30-
prev_container = `#{self.instance.dom_node}.parentNode`
22+
cloned_element = React::Element.new(`React.cloneElement(#{@element.to_n}, #{params.to_n})`)
23+
prev_container = `#{@instance.dom_node}.parentNode`
3124
React.render(cloned_element, prev_container)
3225
nil
3326
end
3427

35-
def force_update!
36-
native.force_update!
37-
end
38-
3928
def html
40-
# How can we get the current ReactElement w/o violating private APIs?
41-
elem = Native(native[:_reactInternalInstance][:_currentElement])
42-
React.render_to_static_markup(elem)
29+
html = `#{@instance.dom_node}.parentNode.innerHTML`
30+
%x{
31+
var REGEX_REMOVE_ROOT_IDS = /\s?data-reactroot="[^"]*"/g;
32+
var REGEX_REMOVE_IDS = /\s?data-reactid="[^"]+"/g;
33+
html = html.replace(REGEX_REMOVE_ROOT_IDS, '');
34+
html = html.replace(REGEX_REMOVE_IDS, '');
35+
}
36+
return html
4337
end
4438
end
4539
end

spec/react/test/session_spec.rb

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -41,18 +41,11 @@ def render
4141
end
4242
end
4343

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-
5144
describe '#native' do
5245
it 'returns the React native instance of the component' do
5346
instance = subject.mount(Greeter)
5447
native = instance.instance_variable_get('@native')
55-
expect(subject.native).to eq(native)
48+
expect(`#{subject.native} === #{native}`).to eq(true)
5649
end
5750
end
5851

@@ -89,11 +82,11 @@ def render
8982
end
9083
end
9184

92-
describe '#force_update' do
85+
describe 'instance#force_update!' do
9386
it 'causes the component to render' do
9487
instance = subject.mount(Greeter)
9588
expect(instance).to receive(:render)
96-
subject.force_update!
89+
subject.instance.force_update!
9790
end
9891
end
9992
end

0 commit comments

Comments
 (0)