blob: b1ade52882483bb440be7aea321eb88f7804f1b3 (
plain)
| 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
 | module SnaphostSpecHelper
  module Methods
    def set_invariant expr, val=nil
      val ||= expr
      chain = expr.split(".")
      method = chain.pop
      before(:each) do
        allow(eval(chain.join('.'))).to receive(method){ val }
      end
    end
  end
  def self.included into
    into.extend Methods
  end
end
RSpec.configure do |config|
  config.include SnaphostSpecHelper, type: :view
end
RSpec::Matchers.define :match_actions_links_snapshot do |name|
  match do |actual|
    @content = Capybara::Node::Simple.new(rendered).find('.page_header').native.inner_html
    expect(@content).to match_snapshot(name)
  end
  failure_message do |actual|
    out = ["Snapshots did not match."]
    snap_path = File.dirname(method_missing(:class).metadata[:file_path]) + "/__snapshots__/#{name}.snap"
    temp_path = Pathname.new "#{Rails.root}/tmp/__snapshots__/#{name}.failed.snap"
    FileUtils.mkdir_p temp_path.dirname
    tmp = File.new temp_path, "w"
    tmp.write @content
    tmp.close()
    expected = File.read snap_path
    out << "Expected: #{expected}"
    out << "Actual: #{@content}"
    out << "\n\n --- DIFF ---"
    out << differ.diff_as_string(@content, expected)
    out << "\n\n --- Previews : ---"
    out << "Expected: \n" + snapshot_url(snap: snap_path, layout: :actions_links)
    out << " \nActual:  \n" + snapshot_url(snap: tmp.path, layout: :actions_links)
    out.join("\n")
  end
  def snapshot_url snap:, layout:
    "http://localhost:3000/snap/?snap=#{URI.encode(snap.to_s)}&layout=#{URI.encode(layout.to_s)}"
  end
  def differ
    RSpec::Support::Differ.new(
        :object_preparer => lambda { |object| RSpec::Matchers::Composable.surface_descriptions_in(object) },
        :color => RSpec::Matchers.configuration.color?
    )
  end
end
 |