Module: RenderTurboStream::Test::Request::Helpers

Defined in:
lib/render_turbo_stream/test/request/helpers.rb

Instance Method Summary collapse

Instance Method Details

#all_turbo_responsesObject

Helper for the developer for writing tests: Array with all attributes of all turbo-stream and turbo-channel actions that runned on the last request



35
36
37
# File 'lib/render_turbo_stream/test/request/helpers.rb', line 35

def all_turbo_responses
  RenderTurboStream::Test::Request::Libs.new(response).all_turbo_responses
end

#assert_once_targeted(*target_ids) ⇒ Object

Assert that each of given target_ids is targeted exactly once



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/render_turbo_stream/test/request/helpers.rb', line 8

def assert_once_targeted(*target_ids)
  responses = RenderTurboStream::Test::Request::Libs.new(response).all_turbo_responses
  id_counts = {}
  responses.each do |r|
    if r['target'].is_a?(Array)
      r['target'].each do |t|
        id = (t[0] == '#' ? t[1..-1] : t)
        id_counts[t.to_s] ||= 0
        id_counts[t.to_s] += 1
      end
    else
      id = (r['target'][0] == '#' ? r['target'][1..-1] : r['target'])
      id_counts[id] ||= 0
      id_counts[id] += 1
    end
  end

  assert(id_counts.keys.length == target_ids.length, "You checked for #{target_ids.length} but #{id_counts.keys.length} targeted: #{id_counts.keys.join(', ')}")
  target_ids.each do |id|
    assert(id_counts.key?(id), "The id #{id} is not within the targeted ids: #{id_counts.keys.join(', ')}")
    expect(id_counts[id]).to eq(1)
    assert(id_counts[id] == 1, "The id #{id} is targeted #{id_counts[id]} #{'time'.pluralize(id_counts[id])}")
  end
end

#assert_stream_action(action, target_id: nil, count: 1, &block) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
# File 'lib/render_turbo_stream/test/request/helpers.rb', line 75

def assert_stream_action(action, target_id: nil, count: 1, &block)

  libs = RenderTurboStream::Test::Request::Libs.new(response)

  r = libs.select_actions( false, action, &block)

  assert(
    r[:responses].length == count,
    libs.class.assert_error_message(count, r[:responses].length, r[:log])
  )
end

#assert_stream_response(target_id, action: nil, count: 1, &block) ⇒ Object

assert one or more specific actions to a target_id



63
64
65
66
67
68
69
70
71
72
73
# File 'lib/render_turbo_stream/test/request/helpers.rb', line 63

def assert_stream_response(target_id, action: nil, count: 1, &block)

  libs = RenderTurboStream::Test::Request::Libs.new(response)

  r = libs.select_responses( false, RenderTurboStream::Libs.target_id_to_target(target_id), action, &block)

  assert(
    r[:responses].length == count,
    libs.class.assert_error_message(count, r[:responses].length, r[:log])
  )
end

#assert_turbo_redirect_to(path) ⇒ Object

Returns the path sent to turbo_stream.redirect_to.



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/render_turbo_stream/test/request/helpers.rb', line 45

def assert_turbo_redirect_to(path)
  resps = RenderTurboStream::Test::Request::Libs.new(response).all_turbo_responses
  url = nil
  resps.each do |r|
    if r['type'] == 'stream-command'
      if r['array'].first == 'redirect_to'
        if url
          url = 'ERROR: REDIRECT CALLED MORE THAN ONCE'
        else
          url = r['array'].second
        end
      end
    end
  end
  assert url == path, "Expected turbo_redirect_to «#{path}» but got: «#{url}»"
end

#target_id(relative_view_path, object) ⇒ Object



87
88
89
90
# File 'lib/render_turbo_stream/test/request/helpers.rb', line 87

def target_id(relative_view_path, object)
  libs = RenderTurboStream::Libs
  libs.target_id(relative_view_path, object)
end

#target_id_css(relative_view_path, object) ⇒ Object



92
93
94
# File 'lib/render_turbo_stream/test/request/helpers.rb', line 92

def target_id_css(relative_view_path, object)
  "##{target_id(relative_view_path, object)}"
end

#turbo_targetsObject

Returns Array of all target_ids that arre affected at least once on the last response



40
41
42
# File 'lib/render_turbo_stream/test/request/helpers.rb', line 40

def turbo_targets
  RenderTurboStream::Test::Request::Libs.new(response).turbo_targets
end