1
1
module React
2
2
module Test
3
3
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
6
7
7
8
def mount ( component_klass , params = { } )
8
9
@element = React . create_element ( component_klass , params )
@@ -11,35 +12,28 @@ def mount(component_klass, params = {})
11
12
12
13
def instance
13
14
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()`
16
17
end
17
18
@instance
18
19
end
19
20
20
- def native
21
- @native
22
- end
23
-
24
- def element
25
- @element
26
- end
27
-
28
21
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`
31
24
React . render ( cloned_element , prev_container )
32
25
nil
33
26
end
34
27
35
- def force_update!
36
- native . force_update!
37
- end
38
-
39
28
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
43
37
end
44
38
end
45
39
end
0 commit comments