11module 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
0 commit comments