-
Notifications
You must be signed in to change notification settings - Fork 366
Expand file tree
/
Copy pathtest_assertions.rb
More file actions
157 lines (152 loc) · 5.55 KB
/
test_assertions.rb
File metadata and controls
157 lines (152 loc) · 5.55 KB
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
module Turbo
module TestAssertions
extend ActiveSupport::Concern
included do
# FIXME: Should happen in Rails at a different level
delegate :dom_id, :dom_class, to: ActionView::RecordIdentifier
end
# Assert that the rendered fragment of HTML contains a `<turbo-stream>`
# element.
#
# ==== Options
#
# * <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
# * <tt>:count</tt> [Integer] indicates how many turbo streams are expected.
# Defaults to <tt>1</tt>.
#
# Given the following HTML fragment:
#
# <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 fragment:
#
# <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(action:, target: nil, targets: nil, count: 1, &block)
selector = %(turbo-stream[action="#{action}"])
selector << %([target="#{target.respond_to?(:to_key) ? dom_id(target) : target}"]) if target
selector << %([targets="#{targets}"]) if targets
assert_select selector, count: count, &block
end
# Assert that the rendered fragment of HTML does not contain a `<turbo-stream>`
# element.
#
# ==== Options
#
# * <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 fragment:
#
# <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(action:, target: nil, targets: nil)
selector = %(turbo-stream[action="#{action}"])
selector << %([target="#{target.respond_to?(:to_key) ? dom_id(target) : target}"]) if target
selector << %([targets="#{targets}"]) if targets
assert_select selector, count: 0
end
# Assert that the rendered fragment of HTML contains a `<turbo-frame>`
# element.
#
# ==== Arguments
#
# * <tt>ids</tt> [String, Array<String, ActiveRecord::Base>] matches the element's <tt>[id]</tt> attribute
#
# ==== Options
#
# * <tt>:loading</tt> [String] matches the element's <tt>[loading]</tt>
# attribute
# * <tt>:src</tt> [String] matches the element's <tt>[src]</tt> attribute
# * <tt>:target</tt> [String] matches the element's <tt>[target]</tt>
# attribute
# * <tt>:count</tt> [Integer] indicates how many turbo frames are expected.
# Defaults to <tt>1</tt>.
#
# Given the following HTML fragment:
#
# <turbo-frame id="example" target="_top"></turbo-frame>
#
# The following assertion would pass:
#
# assert_turbo_frame id: "example", target: "_top"
#
# You can also pass a block make assertions about the contents of the
# element. Given the following HTML fragment:
#
# <turbo-frame id="example">
# <p>Hello!</p>
# </turbo-frame>
#
# The following assertion would pass:
#
# assert_turbo_frame id: "example" do
# assert_select "p", text: "Hello!"
# end
#
def assert_turbo_frame(*ids, loading: nil, src: nil, target: nil, count: 1, &block)
id = ids.first.respond_to?(:to_key) ? ActionView::RecordIdentifier.dom_id(*ids) : ids.join('_')
selector = %(turbo-frame[id="#{id}"])
selector << %([loading="#{loading}"]) if loading
selector << %([src="#{src}"]) if src
selector << %([target="#{target}"]) if target
assert_select selector, count: count, &block
end
# Assert that the rendered fragment of HTML does not contain a `<turbo-frame>`
# element.
#
# ==== Arguments
#
# * <tt>ids</tt> [String, Array<String, ActiveRecord::Base>] matches the <tt>[id]</tt> attribute
#
# ==== Options
#
# * <tt>:loading</tt> [String] matches the element's <tt>[loading]</tt>
# attribute
# * <tt>:src</tt> [String] matches the element's <tt>[src]</tt> attribute
# * <tt>:target</tt> [String] matches the element's <tt>[target]</tt>
# attribute
#
# Given the following HTML fragment:
#
# <turbo-frame id="example" target="_top"></turbo-frame>
#
# The following assertion would fail:
#
# assert_no_turbo_frame id: "example", target: "_top"
#
def assert_no_turbo_frame(*ids, **options, &block)
assert_turbo_frame(*ids, **options, count: 0, &block)
end
end
end