Class: React::Test::Session

Inherits:
Object show all
Defined in:
lib/react/test/session.rb

Constant Summary collapse

DSL_METHODS =
%i[mount instance update_params html].freeze

Instance Method Summary collapse

Instance Method Details

#htmlObject



25
26
27
28
29
30
31
32
33
34
# File 'lib/react/test/session.rb', line 25

def html
  html = `#@container.innerHTML`
  %x{
      var REGEX_REMOVE_ROOT_IDS = /\s?data-reactroot="[^"]*"/g;
      var REGEX_REMOVE_IDS = /\s?data-reactid="[^"]+"/g;
      html = html.replace(REGEX_REMOVE_ROOT_IDS, '');
      html = html.replace(REGEX_REMOVE_IDS, '');
  }
  return html
end

#instanceObject



11
12
13
14
15
16
17
# File 'lib/react/test/session.rb', line 11

def instance
  unless @instance
    @container = `document.createElement('div')`
    @instance = React.render(@element, @container)
  end
  @instance
end

#mount(component_klass, params = {}) ⇒ Object



6
7
8
9
# File 'lib/react/test/session.rb', line 6

def mount(component_klass, params = {})
  @element = React.create_element(component_klass, params)
  instance
end

#update_params(params, &block) ⇒ Object



19
20
21
22
23
# File 'lib/react/test/session.rb', line 19

def update_params(params, &block)
  cloned_element = React::Element.new(`React.cloneElement(#{@element.to_n}, #{params.to_n})`)
  React.render(cloned_element, @container, &block)
  nil
end