Sha256: 1d19efb7267b6184641c7c9b479ca0ad88ba59901471172f2102adb18a678c6a

Contents?: true

Size: 1.79 KB

Versions: 1

Compression:

Stored size: 1.79 KB

Contents

require 'yaks-html'
require 'capybara/rspec'

module YaksHTML
  # This is a bit of a hack. The only way to add custom rack env
  # entries to Capybara/Rack::Test is through the driver options (see
  # Capybara.register_driver below). However this is only executed
  # once, so throughout the whole test suite the same instance of the
  # hash we pass will be used. To make it a bit easier to set/reset
  # this we add this layer of indirection. Now what we pass the driver
  # looks like a hash, but we can point it to a new hash with
  # RACK_ENV.__setobj__({})

  RACK_ENV = SimpleDelegator.new({})

  module CapybaraDSL
    def self.included(base)
      base.class_eval do
        let(:rel_prefix) { '' }
      end
    end

    def click_rel(rel)
      rel = [rel_prefix, rel].join unless rel.is_a? Symbol
      find("a[rel=\"#{rel}\"]").click
    end

    def click_first_rel(rel)
      rel = [rel_prefix, rel].join unless rel.is_a? Symbol
      all("a[rel=\"#{rel}\"]").first.click
    end

    def submit!
      find('input[type="submit"]').click
    end

    def within_form(name, &block)
      within(all("form[@name=\"#{name}\"]").first, &block)
    end

    def submit_form(name, &block)
      within(all("form[@name=\"#{name}\"]").first) do
        yield block
        submit!
      end
    end

    def refresh
      visit current_path
    end

    def env
      YaksHTML::RACK_ENV
    end
  end
end

RSpec.configure do |config|
  select = {type: :yaks_integration}

  config.include Capybara::DSL, select
  config.include Capybara::RSpecMatchers, select

  config.include YaksHTML::CapybaraDSL, select

  config.before(select) do
    YaksHTML::RACK_ENV.__setobj__('HTTP_ACCEPT' => 'text/html')
  end
end

Capybara.register_driver :rack_test do |app|
  Capybara::RackTest::Driver.new(app, headers: YaksHTML::RACK_ENV)
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
yaks-html-0.11.0 lib/yaks-html/rspec.rb