Module: RailsStuff::RSpecHelpers::Matchers::RedirectWithTurbolinks

Includes:
RSpec::Matchers
Defined in:
lib/rails_stuff/rspec_helpers/matchers/redirect_with_turbolinks.rb

Defined Under Namespace

Classes: XhrFailure

Instance Method Summary collapse

Instance Method Details

#active_support_assertion(location) ⇒ Object



39
40
41
42
43
44
45
46
47
# File 'lib/rails_stuff/rspec_helpers/matchers/redirect_with_turbolinks.rb', line 39

def active_support_assertion(location)
  redirect_is       = @scope.send(:normalize_argument_to_redirection, location)
  redirect_expected = @scope.send(:normalize_argument_to_redirection, @expected)
  message = "Expected response to be a Turbolinks redirect to <#{redirect_expected}>" \
    " but was a redirect to <#{redirect_is}>"
  @scope.assert_operator redirect_expected, :===, redirect_is, message
rescue ActiveSupport::TestCase::Assertion => e
  raise XhrFailure, e
end

#failure_messageObject



18
19
20
# File 'lib/rails_stuff/rspec_helpers/matchers/redirect_with_turbolinks.rb', line 18

def failure_message
  @scope.request ? super : 'Request was not performed'
end

#matches?(response) ⇒ Boolean

Returns:

  • (Boolean)


9
10
11
12
13
14
15
16
# File 'lib/rails_stuff/rspec_helpers/matchers/redirect_with_turbolinks.rb', line 9

def matches?(response)
  return unless @scope.request
  if @scope.request.xhr?
    match_unless_raises(XhrFailure) { matches_xhr?(response) }
  else
    super
  end
end

#matches_xhr?(response) ⇒ Boolean

Returns:

  • (Boolean)

Raises:



22
23
24
25
26
27
28
29
# File 'lib/rails_stuff/rspec_helpers/matchers/redirect_with_turbolinks.rb', line 22

def matches_xhr?(response)
  unless be_ok.matches?(response)
    raise XhrFailure, "Expect #{response.inspect} to be OK for Turbolinks redirect"
  end
  location, _options = turbolinks_location(response)
  raise XhrFailure, "No Turbolinks redirect in\n\n#{response.body}" unless location
  active_support_assertion(location)
end


31
32
33
34
35
36
37
# File 'lib/rails_stuff/rspec_helpers/matchers/redirect_with_turbolinks.rb', line 31

def turbolinks_location(response)
  body = response.body
  return unless body
  match_data = /Turbolinks\.visit\(([^,]+)(,\s*([^)]+))?\)/.match(body)
  return unless match_data
  [match_data[1], match_data[3]].map { |x| JSON.parse("[#{x}]")[0] }
end