module HyperSpec::ComponentTestHelpers

Constants

TIME_COP_CLIENT_PATCH
TOP_LEVEL_COMPONENT_PATCH

Attributes

current_example[RW]
description_displayed[RW]

Public Class Methods

display_example_description() click to toggle source
# File lib/hyper-spec/component_test_helpers.rb, line 19
def display_example_description
  "<script type='text/javascript'>console.log('%c#{current_example.description}'"\
  ",'color:green; font-weight:bold; font-size: 200%')</script>"
end

Public Instance Methods

add_class(class_name, style) click to toggle source
# File lib/hyper-spec/component_test_helpers.rb, line 261
def add_class(class_name, style)
  @client_code = "#{@client_code}ComponentHelpers.add_class('#{class_name}', #{style})\n"
end
add_opal_block(str, block) click to toggle source
# File lib/hyper-spec/component_test_helpers.rb, line 122
def add_opal_block(str, block)
  # big assumption here is that we are going to follow this with a .to
  # hence .children.first followed by .children.last
  # probably should do some kind of "search" to make this work nicely
  return str unless block
  "#{str}\n"\
  "#{Unparser.unparse Parser::CurrentRuby.parse(block.source).children.first.children.last}"
end
build_test_url_for(controller) click to toggle source
# File lib/hyper-spec/component_test_helpers.rb, line 25
def build_test_url_for(controller)
  unless controller
    unless defined?(::ReactTestController)
      Object.const_set('ReactTestController', Class.new(::ActionController::Base))
    end

    controller = ::ReactTestController
  end

  route_root = controller.name.gsub(/Controller$/, '').underscore

  unless controller.method_defined?(:test)
    controller.class_eval do
      define_method(:test) do
        route_root = self.class.name.gsub(/Controller$/, '').underscore
        test_params = ::Rails.cache.read("/#{route_root}/#{params[:id]}")
        @component_name = test_params[0]
        @component_params = test_params[1]
        render_params = test_params[2]
        render_on = render_params.delete(:render_on) || :client_only
        _mock_time = render_params.delete(:mock_time)
        style_sheet = render_params.delete(:style_sheet)
        javascript = render_params.delete(:javascript)
        code = render_params.delete(:code)

        page = '<%= react_component @component_name, @component_params, '\
               "{ prerender: #{render_on != :client_only} } %>"
        unless render_on == :server_only
          page = "<script type='text/javascript'>\n#{TOP_LEVEL_COMPONENT_PATCH}\n</script>\n#{page}"
          page = "<script type='text/javascript'>\n#{code}\n</script>\n#{page}" if code
        end

        if render_on != :server_only || Lolex.initialized?
          page = "<script type='text/javascript'>\n#{TIME_COP_CLIENT_PATCH}\n</script>\n#{page}"
        end

        if (render_on != :server_only && !render_params[:layout]) || javascript
          page = "<%= javascript_include_tag '#{javascript || 'application'}' %>\n#{page}"
        end

        if !render_params[:layout] || style_sheet
          page = "<%= stylesheet_link_tag '#{style_sheet || 'application'}' %>\n#{page}"
        end
        page = "<script type='text/javascript'>go = function() "\
               "{window.hyper_spec_waiting_for_go = false}</script>\n#{page}"

        title = view_context.escape_javascript(ComponentTestHelpers.current_example.description)
        title = "#{title}...continued." if ComponentTestHelpers.description_displayed

        page = "<script type='text/javascript'>console.log('%c#{title}',"\
               "'color:green; font-weight:bold; font-size: 200%')</script>\n#{page}"

        ComponentTestHelpers.description_displayed = true
        render_params[:inline] = page
        render render_params
      end
    end

    begin
      routes = ::Rails.application.routes
      routes.disable_clear_and_finalize = true
      routes.clear!
      routes.draw do
        get "/#{route_root}/:id", to: "#{route_root}#test"
      end
      ::Rails.application.routes_reloader.paths.each { |path| load(path) }
      routes.finalize!
      ActiveSupport.on_load(:action_controller) { routes.finalize! }
    ensure
      routes.disable_clear_and_finalize = false
    end
  end

  "/#{route_root}/#{@test_id = (@test_id || 0) + 1}"
end
client_option(opts = {}) click to toggle source
# File lib/hyper-spec/component_test_helpers.rb, line 172
def client_option(opts = {})
  @client_options ||= {}
  @client_options.merge! opts
end
Also aliased as: client_options
client_options(opts = {})
Alias for: client_option
debugger() click to toggle source
# File lib/hyper-spec/component_test_helpers.rb, line 162
def debugger
  `debugger`
  nil
end
evaluate_promise(str = '', opts = {}, &block) click to toggle source
# File lib/hyper-spec/component_test_helpers.rb, line 131
def evaluate_promise(str = '', opts = {}, &block)
  insure_mount
  str = "#{str}\n#{Unparser.unparse Parser::CurrentRuby.parse(block.source).children.last}" if block
  str = "#{str}.then { |args| args = [args]; `window.hyper_spec_promise_result = args` }"
  js = Opal.compile(str).gsub("\n","").gsub("(Opal);","(Opal)")
  page.evaluate_script("window.hyper_spec_promise_result = false")
  page.execute_script(js)
  Timeout.timeout(Capybara.default_max_wait_time) do
    loop do
      sleep 0.25
      break if page.evaluate_script("!!window.hyper_spec_promise_result")
    end
  end
  JSON.parse(page.evaluate_script("window.hyper_spec_promise_result.$to_json()"), opts).first
end
evaluate_ruby(str = '', opts = {}, &block) click to toggle source
# File lib/hyper-spec/component_test_helpers.rb, line 106
def evaluate_ruby(str = '', opts = {}, &block)
  insure_mount
  if block
    str = "#{str}\n#{Unparser.unparse Parser::CurrentRuby.parse(block.source).children.last}"
  end
  js = Opal.compile(str).delete("\n").gsub('(Opal);', '(Opal)')
  # workaround for firefox 58 and geckodriver 0.19.1, because firefox is unable to find .$to_json:
  # JSON.parse(evaluate_script("(function(){var a=Opal.Array.$new(); a[0]=#{js}; return a.$to_json();})();"), opts).first
  JSON.parse(evaluate_script("[#{js}].$to_json()"), opts).first
end
expect_evaluate_ruby(str = '', opts = {}, &block) click to toggle source
# File lib/hyper-spec/component_test_helpers.rb, line 117
def expect_evaluate_ruby(str = '', opts = {}, &block)
  insure_mount
  expect(evaluate_ruby(add_opal_block(str, block), opts))
end
expect_promise(str = '', opts = {}, &block) click to toggle source
# File lib/hyper-spec/component_test_helpers.rb, line 147
def expect_promise(str = '', opts = {}, &block)
  insure_mount
  expect(evaluate_promise(add_opal_block(str, block), opts))
end
insure_mount() click to toggle source
# File lib/hyper-spec/component_test_helpers.rb, line 167
def insure_mount
  # rescue in case page is not defined...
  mount unless page.instance_variable_get('@hyper_spec_mounted')
end
isomorphic() { || ... } click to toggle source
# File lib/hyper-spec/component_test_helpers.rb, line 101
def isomorphic(&block)
  yield
  on_client(&block)
end
mount(component_name = nil, params = nil, opts = {}, &block) click to toggle source
# File lib/hyper-spec/component_test_helpers.rb, line 179
    def mount(component_name = nil, params = nil, opts = {}, &block)
      unless params
        params = opts
        opts = {}
      end

      opts = client_options opts
      test_url = build_test_url_for(opts.delete(:controller))

      if block || @client_code || component_name.nil?
        block_with_helpers = <<-code
          module ComponentHelpers
            def self.js_eval(s)
              `eval(s)`
            end
            def self.dasherize(s)
              res = %x{
                s.replace(/[-_\\s]+/g, '-')
                .replace(/([A-Z\\d]+)([A-Z][a-z])/g, '$1-$2')
                .replace(/([a-z\\d])([A-Z])/g, '$1-$2')
                .toLowerCase()
              }
              res
            end
            def self.add_class(class_name, styles={})
              style = styles.collect { |attr, value| "\#{dasherize(attr)}:\#{value}" }.join("; ")
              cs = class_name.to_s
              %x{
                var style_el = document.createElement("style");
                var css = "." + cs + " { " + style + " }";
                style_el.type = "text/css";
                if (style_el.styleSheet){
                  style_el.styleSheet.cssText = css;
                } else {
                  style_el.appendChild(document.createTextNode(css));
                }
                document.head.appendChild(style_el);
              }
            end
          end
          class React::Component::HyperTestDummy < React::Component::Base
                def render; end
          end
          #{@client_code}
          #{Unparser.unparse(Parser::CurrentRuby.parse(block.source).children.last) if block}
        code
        opts[:code] = Opal.compile(block_with_helpers)
      end

      component_name ||= 'React::Component::HyperTestDummy'
      ::Rails.cache.write(test_url, [component_name, params, opts])
      test_code_key = "hyper_spec_prerender_test_code.js"
      @@original_server_render_files ||= ::Rails.configuration.react.server_renderer_options[:files]
      if opts[:render_on] == :both || opts[:render_on] == :server_only
        unless opts[:code].blank?
          ::Rails.cache.write(test_code_key, opts[:code])
          ::Rails.configuration.react.server_renderer_options[:files] = @@original_server_render_files + [test_code_key]
          ::React::ServerRendering.reset_pool # make sure contexts are reloaded so they dont use code from cache, as the rails filewatcher doesnt look for cache changes
        else
          ::Rails.cache.delete(test_code_key)
          ::Rails.configuration.react.server_renderer_options[:files] = @@original_server_render_files
          ::React::ServerRendering.reset_pool # make sure contexts are reloaded so they dont use code from cache, as the rails filewatcher doesnt look for cache changes
        end
      end
      visit test_url
      wait_for_ajax unless opts[:no_wait]
      page.instance_variable_set('@hyper_spec_mounted', true)
      Lolex.init(self, client_options[:time_zone], client_options[:clock_resolution])
    end
on_client(&block) click to toggle source
# File lib/hyper-spec/component_test_helpers.rb, line 157
def on_client(&block)
  @client_code =
    "#{@client_code}#{Unparser.unparse Parser::CurrentRuby.parse(block.source).children.last}\n"
end
open_in_chrome() click to toggle source
# File lib/hyper-spec/component_test_helpers.rb, line 265
def open_in_chrome
  if false && ['linux', 'freebsd'].include?(`uname`.downcase)
    `google-chrome http://#{page.server.host}:#{page.server.port}#{page.current_path}`
  else
    `open http://#{page.server.host}:#{page.server.port}#{page.current_path}`
  end

  while true
    sleep 1.hour
  end
end
pause(message = nil) click to toggle source
# File lib/hyper-spec/component_test_helpers.rb, line 277
def pause(message = nil)
  if message
    puts message
    page.evaluate_ruby "puts #{message.inspect}.to_s + ' (type go() to continue)'"
  end

  page.evaluate_script('window.hyper_spec_waiting_for_go = true')

  loop do
    sleep 0.25
    break unless page.evaluate_script('window.hyper_spec_waiting_for_go')
  end
end
ppr(str) click to toggle source
# File lib/hyper-spec/component_test_helpers.rb, line 152
def ppr(str)
  js = Opal.compile(str).delete("\n").gsub('(Opal);', '(Opal)')
  execute_script("console.log(#{js})")
end
run_on_client(&block) click to toggle source
# File lib/hyper-spec/component_test_helpers.rb, line 256
def run_on_client(&block)
  script = Opal.compile(Unparser.unparse(Parser::CurrentRuby.parse(block.source).children.last))
  execute_script(script)
end
size_window(width = nil, height = nil) click to toggle source
# File lib/hyper-spec/component_test_helpers.rb, line 309
def size_window(width = nil, height = nil)
  # return if @window_cannot_be_resized
  # original_width = evaluate_script('window.innerWidth')
  # original_height = evaluate_script('window.innerHeight')
  width, height = [height, width] if width == :portrait
  width, height = width if width.is_a? Array
  portrait = true if height == :portrait

  case width
  when :small
    width, height = [480, 320]
  when :mobile
    width, height = [640, 480]
  when :tablet
    width, height = [960, 640]
  when :large
    width, height = [1920, 6000]
  when :default, nil
    width, height = [1024, 768]
  end

  width, height = [height, width] if portrait

  unless RSpec.configuration.debugger_width
    Capybara.current_session.current_window.resize_to(1000, 500)
    wait_for_size(1000, 500)
    inner_width = evaluate_script('window.innerWidth')
    RSpec.configuration.debugger_width = 1000 - inner_width
  end
  Capybara.current_session.current_window
          .resize_to(width + RSpec.configuration.debugger_width, height)
  wait_for_size(width + RSpec.configuration.debugger_width, height)
end
wait_for_size(width, height) click to toggle source
# File lib/hyper-spec/component_test_helpers.rb, line 291
def wait_for_size(width, height)
  start_time = Capybara::Helpers.monotonic_time
  stable_count_w = 0
  stable_count_h = 0
  prev_size = [0, 0]
  begin
    sleep 0.05
    curr_size = Capybara.current_session.current_window.size
    return if [width, height] == curr_size
    # some maximum or minimum is reached and size doesnt change anymore
    stable_count_w += 1 if prev_size[0] == curr_size[0]
    stable_count_h += 1 if prev_size[1] == curr_size[1]
    return if stable_count_w > 2 || stable_count_h > 2
    prev_size = curr_size
  end while (Capybara::Helpers.monotonic_time - start_time) < Capybara.current_session.config.default_max_wait_time
  raise Capybara::WindowError, "Window size not stable within #{Capybara.current_session.config.default_max_wait_time} seconds."
end