Class: TurboRspec::Matchers::MatchTurboStreamSnapshot

Inherits:
Object
  • Object
show all
Defined in:
lib/turbo_rspec/matchers/match_turbo_stream_snapshot.rb

Overview

RSpec matcher that records a turbo stream response on the first run and diffs against the stored snapshot on subsequent runs.

Snapshots are written to the directory configured by +TurboRspec.configuration.snapshot_dir+ (default: +spec/snapshots/turbo+). Each snapshot is stored as +name.turbo+.

Set +UPDATE_TURBO_SNAPSHOTS=1+ to overwrite existing snapshots.

Examples:

expect(response).to match_turbo_stream_snapshot("messages/new")

Instance Method Summary collapse

Constructor Details

#initialize(name) ⇒ MatchTurboStreamSnapshot

Returns a new instance of MatchTurboStreamSnapshot.



20
21
22
# File 'lib/turbo_rspec/matchers/match_turbo_stream_snapshot.rb', line 20

def initialize(name)
  @name = name
end

Instance Method Details

#descriptionString

Returns:

  • (String)


58
59
60
# File 'lib/turbo_rspec/matchers/match_turbo_stream_snapshot.rb', line 58

def description
  "match turbo stream snapshot #{@name.inspect}"
end

#does_not_match?(response_or_body) ⇒ Boolean

Parameters:

  • response_or_body (#body, String)

Returns:

  • (Boolean)


41
42
43
# File 'lib/turbo_rspec/matchers/match_turbo_stream_snapshot.rb', line 41

def does_not_match?(response_or_body)
  !matches?(response_or_body)
end

#failure_messageString

Returns:

  • (String)


46
47
48
49
50
# File 'lib/turbo_rspec/matchers/match_turbo_stream_snapshot.rb', line 46

def failure_message
  "expected response to match turbo stream snapshot #{@name.inspect}\n\n" \
    "diff (stored → actual):\n#{inline_diff}\n\n" \
    "To update: run with UPDATE_TURBO_SNAPSHOTS=1"
end

#failure_message_when_negatedString

Returns:

  • (String)


53
54
55
# File 'lib/turbo_rspec/matchers/match_turbo_stream_snapshot.rb', line 53

def failure_message_when_negated
  "expected response not to match turbo stream snapshot #{@name.inspect}"
end

#matches?(response_or_body) ⇒ Boolean

Parameters:

  • response_or_body (#body, String)

Returns:

  • (Boolean)


26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/turbo_rspec/matchers/match_turbo_stream_snapshot.rb', line 26

def matches?(response_or_body)
  @actual = extract_body(response_or_body)
  @path = snapshot_path

  if update_snapshots? || !File.exist?(@path)
    write_snapshot(@actual)
    true
  else
    @stored = File.read(@path)
    @actual.strip == @stored.strip
  end
end