Class: React::Test::Utils

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

Class Method Summary collapse

Class Method Details

.render_component_into_document(component, args = {}) ⇒ Object



4
5
6
7
# File 'lib/react/test/utils.rb', line 4

def self.render_component_into_document(component, args = {})
  element = React.create_element(component, args)
  render_into_document(element)
end

.render_into_document(element) ⇒ Object



9
10
11
12
13
# File 'lib/react/test/utils.rb', line 9

def self.render_into_document(element)
  raise "You should pass a valid React::Element" unless React.is_valid_element?(element)
  dom_el = `document.body.querySelector('div[data-react-class="React.TopLevelRailsComponent"]').appendChild(document.createElement('div'))`
  React.render(element, dom_el)
end

.simulate_click(element) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/react/test/utils.rb', line 15

def self.simulate_click(element)
  # element must be a component or a dom node or a element
  el =  if `typeof element.nodeType !== "undefined"`
          element
        elsif element.respond_to? :dom_node
          element.dom_node
        elsif element.is_a? React::Element
          `ReactDOM.findDOMNode(#{element.to_n}.native)`
        else
          element
        end
  %x{
    var evob = new MouseEvent('click', {
      view: window,
      bubbles: true,
      cancelable: true
    });
    el.dispatchEvent(evob);
  }
end

.simulate_keydown(element, key_name = "Enter") ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/react/test/utils.rb', line 36

def self.simulate_keydown(element, key_name = "Enter")
  # element must be a component or a dom node or a element
  el =  if `typeof element.nodeType !== "undefined"`
          element
        elsif element.respond_to? :dom_node
          element.dom_node
        elsif element.is_a? React::Element
          `ReactDOM.findDOMNode(#{element.to_n}.native)`
        else
          element
        end
  %x{
    var evob = new KeyboardEvent('keydown', { key: key_name, bubbles: true, cancelable: true });
    el.dispatchEvent(evob);
  }
end

.simulate_submit(element) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/react/test/utils.rb', line 53

def self.simulate_submit(element)
  # element must be a component or a dom node or a element
  el =  if `typeof element.nodeType !== "undefined"`
          element
        elsif element.respond_to? :dom_node
          element.dom_node
        elsif element.is_a? React::Element
          `ReactDOM.findDOMNode(#{element.to_n}.native)`
        else
          element
        end
  %x{
    var evob = new Event('submit', { bubbles: true, cancelable: true });
    el.dispatchEvent(evob);
  }
end