Sha256: bce58d98e15c21fb17e3cec40d545321af6caf381fcf81f1baf0fde529329cfb

Contents?: true

Size: 877 Bytes

Versions: 10

Compression:

Stored size: 877 Bytes

Contents

require 'capybara'

module HaveTextMatcher
  def have_text(text)
    HaveText.new(text)
  end

  class HaveText
    def initialize(text)
      @text = text
    end

    def in(css)
      @css = css
      self
    end

    def matches?(subject)
      @subject = Capybara.string(subject)

      @subject.has_css?(@css || "*", text: @text)
    end

    def failure_message
      "expected to find #{@text.inspect} #{within}"
    end

    def failure_message_when_negated
      "expected not to find #{@text.inspect} #{within}"
    end

    private

    def within
      if @css && @subject.has_css?(@css)
        "within\n#{@subject.find(@css).native}"
      else
        "#{inside} within\n#{@subject.native}"
      end
    end

    def inside
      @css ? "inside #{@css.inspect}" : "anywhere"
    end
  end
end

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

Version data entries

10 entries across 10 versions & 2 rubygems

Version Path
draper-4.0.4 spec/support/matchers/have_text.rb
draper-4.0.3 spec/support/matchers/have_text.rb
draper-4.0.2 spec/support/matchers/have_text.rb
draper-4.0.1 spec/support/matchers/have_text.rb
draper-4.0.0 spec/support/matchers/have_text.rb
draper-3.1.0 spec/support/matchers/have_text.rb
draper-3.0.1 spec/support/matchers/have_text.rb
draper-3.0.0 spec/support/matchers/have_text.rb
draper-3.0.0.pre1 spec/support/matchers/have_text.rb
draper_new-3.0.0 spec/support/matchers/have_text.rb