-
Notifications
You must be signed in to change notification settings - Fork 328
/
integration_test_assertions.rb
76 lines (75 loc) · 2.79 KB
/
integration_test_assertions.rb
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
module Turbo
module TestAssertions
module IntegrationTestAssertions
# Assert that the Turbo Stream request's response body's HTML contains a
# `<turbo-stream>` element.
#
# ==== Options
#
# * <tt>:status</tt> [Integer, Symbol] the HTTP response status
# * <tt>:action</tt> [String] matches the element's <tt>[action]</tt>
# attribute
# * <tt>:target</tt> [String, #to_key] matches the element's
# <tt>[target]</tt> attribute. If the value responds to <tt>#to_key</tt>,
# the value will be transformed by calling <tt>dom_id</tt>
# * <tt>:targets</tt> [String] matches the element's <tt>[targets]</tt>
# attribute
#
# Given the following HTML response body:
#
# <turbo-stream action="remove" target="message_1"></turbo-stream>
#
# The following assertion would pass:
#
# assert_turbo_stream action: "remove", target: "message_1"
#
# You can also pass a block make assertions about the contents of the
# element. Given the following HTML response body:
#
# <turbo-stream action="replace" target="message_1">
# <template>
# <p>Hello!</p>
# <template>
# </turbo-stream>
#
# The following assertion would pass:
#
# assert_turbo_stream action: "replace", target: "message_1" do
# assert_select "template p", text: "Hello!"
# end
#
def assert_turbo_stream(status: :ok, **attributes, &block)
assert_response status
assert_equal Mime[:turbo_stream], response.media_type
super(**attributes, &block)
end
# Assert that the Turbo Stream request's response body's HTML does not
# contain a `<turbo-stream>` element.
#
# ==== Options
#
# * <tt>:status</tt> [Integer, Symbol] the HTTP response status
# * <tt>:action</tt> [String] matches the element's <tt>[action]</tt>
# attribute
# * <tt>:target</tt> [String, #to_key] matches the element's
# <tt>[target]</tt> attribute. If the value responds to <tt>#to_key</tt>,
# the value will be transformed by calling <tt>dom_id</tt>
# * <tt>:targets</tt> [String] matches the element's <tt>[targets]</tt>
# attribute
#
# Given the following HTML response body:
#
# <turbo-stream action="remove" target="message_1"></turbo-stream>
#
# The following assertion would fail:
#
# assert_no_turbo_stream action: "remove", target: "message_1"
#
def assert_no_turbo_stream(status: :ok, **attributes)
assert_response status
assert_equal Mime[:turbo_stream], response.media_type
super(**attributes)
end
end
end
end