Sha256: ab5dda446a580227bde8ade2d01d7704752f2040746c1dca5ca340453e55603f

Contents?: true

Size: 1.48 KB

Versions: 7

Compression:

Stored size: 1.48 KB

Contents

require 'capybara/rspec'
require 'capybara/apparition'

Capybara.javascript_driver = :apparition

Capybara.register_server :puma do |app, port, host|
  require 'rack/handler/puma'
  Rack::Handler::Puma.run(app, Host: host, Port: port, Threads: "0:4", Silent: true)
end

Capybara.default_max_wait_time = 5

module OpalHelper
  def compile_opal(code)
    Opal.compile(code, requireable: false)
  end

  def execute_opal(code)
    execute_script compile_opal(code)
  end

  def evaluate_opal(code)
    # Remove the initial comment that prevents evaluate from worning
    evaluate_script compile_opal(code).strip.lines[1..-1].join("\n")
  end

  def expect_opal(code)
    expect(evaluate_opal(code))
  end

  def expect_script(code)
    expect(evaluate_script(code))
  end

  def opal_nil
    @opal_nil ||= evaluate_opal 'nil'
  end

  def wait_for_dom_ready(expire_in: Capybara.default_max_wait_time)
    timer = Capybara::Helpers.timer(expire_in: expire_in)

    until evaluate_script('document.readyState') == 'complete'
      raise "reached max time (#{expire_in}s)" if timer.expired?
      sleep 0.01
    end
  end

  def wait_for_opal_ready(expire_in: Capybara.default_max_wait_time)
    timer = Capybara::Helpers.timer(expire_in: expire_in)

    until evaluate_script('!!(window.Opal && window.Opal.gvars.ready)')
      raise "reached max time (#{expire_in}s)" if timer.expired?
      sleep 0.01
    end
  end

  def reset_dom
    visit '/'
  end
end

RSpec.configure do |config|
  config.include OpalHelper
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
opal-rails-2.0.2 spec/support/capybara.rb
opal-rails-2.0.1 spec/support/capybara.rb
opal-rails-2.0.0 spec/support/capybara.rb
opal-rails-1.1.2 spec/support/capybara.rb
opal-rails-1.1.1 spec/support/capybara.rb
opal-rails-1.1.0 spec/support/capybara.rb
opal-rails-1.0.1 spec/support/capybara.rb