Module: Dommy::Rails::Minitest::Assertions

Included in:
Integration
Defined in:
lib/dommy/rails/minitest/assertions.rb

Instance Method Summary collapse

Instance Method Details

#assert_aria_snapshot(expected, actual, msg: nil) ⇒ Object

Assert the subject’s ARIA snapshot matches ‘expected` (Playwright-style subset; names may be /regex/).



47
48
49
50
51
# File 'lib/dommy/rails/minitest/assertions.rb', line 47

def assert_aria_snapshot(expected, actual, msg: nil)
  snapshot = dom_document_for(actual).aria_snapshot
  msg ||= "expected aria snapshot to match:\n#{expected}\ngot:\n#{snapshot}"
  assert(Dommy::Rails::AriaSnapshotMatching.matches?(snapshot, expected), msg)
end

#assert_dom_appends_turbo_stream(actual, target, msg: nil, &block) ⇒ Object



153
154
155
# File 'lib/dommy/rails/minitest/assertions.rb', line 153

def assert_dom_appends_turbo_stream(actual, target, msg: nil, &block)
  assert_dom_has_turbo_stream(actual, action: "append", target: target, msg: msg, &block)
end

#assert_dom_has_authenticity_token(actual, msg: nil) ⇒ Object



71
72
73
74
75
# File 'lib/dommy/rails/minitest/assertions.rb', line 71

def assert_dom_has_authenticity_token(actual, msg: nil)
  matched = Dommy::Rails::PageInspector.authenticity_token?(dom_document_for(actual))
  msg ||= "expected to find Rails authenticity token field"
  assert(matched, msg)
end

#assert_dom_has_checked_field(actual, name = nil, msg: nil) ⇒ Object



114
115
116
117
118
# File 'lib/dommy/rails/minitest/assertions.rb', line 114

def assert_dom_has_checked_field(actual, name = nil, msg: nil)
  matched = Dommy::Rails::PageInspector.checkable_fields(dom_document_for(actual), name: name, checked: true)
  msg ||= "expected to find checked field#{name ? " #{name.inspect}" : ""}"
  assert(matched.any?, msg)
end

#assert_dom_has_csrf_meta_tags(actual, msg: nil) ⇒ Object



65
66
67
68
69
# File 'lib/dommy/rails/minitest/assertions.rb', line 65

def assert_dom_has_csrf_meta_tags(actual, msg: nil)
  matched = Dommy::Rails::PageInspector.csrf_meta_tags?(dom_document_for(actual))
  msg ||= "expected to find Rails CSRF meta tags"
  assert(matched, msg)
end

#assert_dom_has_css(scope, selector, text: nil, count: nil, msg: nil) ⇒ Object



5
6
7
# File 'lib/dommy/rails/minitest/assertions.rb', line 5

def assert_dom_has_css(scope, selector, text: nil, count: nil, msg: nil)
  assert_dom_contains(scope, selector, text: text, count: count, msg: msg)
end

#assert_dom_has_form(actual, action: nil, method: nil, model: nil, msg: nil) ⇒ Object



126
127
128
129
130
131
# File 'lib/dommy/rails/minitest/assertions.rb', line 126

def assert_dom_has_form(actual, action: nil, method: nil, model: nil, msg: nil)
  document = dom_document_for(actual)
  matched = Dommy::Rails::FormInspector.matches?(document, action: action, method: method, model: model)
  msg ||= "expected to find form matching #{form_desc(action: action, method: method, model: model)}"
  assert(matched, msg)
end


77
78
79
80
81
# File 'lib/dommy/rails/minitest/assertions.rb', line 77

def assert_dom_has_link(actual, text = nil, href: nil, count: nil, msg: nil)
  matched = Dommy::Rails::PageInspector.links(dom_document_for(actual), text: text, href: href)
  msg ||= "expected to contain link#{text ? " with text #{text.inspect}" : ""}#{href ? " with href #{href.inspect}" : ""}, found #{matched.size}"
  assert(Dommy::Internal::DomMatching.count_matches?(matched.size, count), msg)
end

#assert_dom_has_meta(actual, name: nil, property: nil, content: nil, msg: nil) ⇒ Object



53
54
55
56
57
# File 'lib/dommy/rails/minitest/assertions.rb', line 53

def assert_dom_has_meta(actual, name: nil, property: nil, content: nil, msg: nil)
  matched = Dommy::Rails::PageInspector.meta_matches?(dom_document_for(actual), name: name, property: property, content: content)
  msg ||= "expected to find meta #{meta_desc(name: name, property: property, content: content)}"
  assert(matched, msg)
end

#assert_dom_has_select(actual, name = nil, label: nil, count: nil, msg: nil) ⇒ Object



102
103
104
105
106
# File 'lib/dommy/rails/minitest/assertions.rb', line 102

def assert_dom_has_select(actual, name = nil, label: nil, count: nil, msg: nil)
  matched = Dommy::Rails::PageInspector.selects(dom_document_for(actual), name: name, label: label)
  msg ||= "expected to contain select#{name ? " with name #{name.inspect}" : ""}#{label ? " with label #{label.inspect}" : ""}, found #{matched.size}"
  assert(Dommy::Internal::DomMatching.count_matches?(matched.size, count), msg)
end

#assert_dom_has_stimulus_action(actual, action, msg: nil) ⇒ Object



181
182
183
184
185
# File 'lib/dommy/rails/minitest/assertions.rb', line 181

def assert_dom_has_stimulus_action(actual, action, msg: nil)
  matched = Dommy::Rails::Stimulus.action?(dom_document_for(actual), action)
  msg ||= "expected to find element with Stimulus action '#{action}'"
  assert(matched, msg)
end

#assert_dom_has_stimulus_controller(actual, name, msg: nil) ⇒ Object



169
170
171
172
173
# File 'lib/dommy/rails/minitest/assertions.rb', line 169

def assert_dom_has_stimulus_controller(actual, name, msg: nil)
  matched = Dommy::Rails::Stimulus.controller?(dom_document_for(actual), name)
  msg ||= "expected to find element with Stimulus controller '#{name}'"
  assert(matched, msg)
end

#assert_dom_has_stimulus_target(actual, controller, target, msg: nil) ⇒ Object



193
194
195
196
197
# File 'lib/dommy/rails/minitest/assertions.rb', line 193

def assert_dom_has_stimulus_target(actual, controller, target, msg: nil)
  matched = Dommy::Rails::Stimulus.target?(dom_document_for(actual), controller, target)
  msg ||= "expected to find element with Stimulus target '#{controller}.#{target}'"
  assert(matched, msg)
end

#assert_dom_has_stimulus_value(actual, controller, key, value, msg: nil) ⇒ Object



205
206
207
208
209
# File 'lib/dommy/rails/minitest/assertions.rb', line 205

def assert_dom_has_stimulus_value(actual, controller, key, value, msg: nil)
  matched = Dommy::Rails::Stimulus.value?(dom_document_for(actual), controller, key, value)
  msg ||= "expected to find element with Stimulus value '#{controller}.#{key}' = #{value.inspect}"
  assert(matched, msg)
end

#assert_dom_has_text(scope, text, msg: nil) ⇒ Object



13
14
15
# File 'lib/dommy/rails/minitest/assertions.rb', line 13

def assert_dom_has_text(scope, text, msg: nil)
  assert_dom_contains_text(scope, text, msg: msg)
end

#assert_dom_has_title(actual, expected, msg: nil) ⇒ Object



33
34
35
36
37
# File 'lib/dommy/rails/minitest/assertions.rb', line 33

def assert_dom_has_title(actual, expected, msg: nil)
  matched = Dommy::Rails::PageInspector.title_matches?(dom_document_for(actual), expected)
  msg ||= "expected document title to match #{expected.inspect}"
  assert(matched, msg)
end

#assert_dom_has_turbo_frame(actual, id = nil, text: nil, count: nil, msg: nil) {|matched.first| ... } ⇒ Object

Yields:

  • (matched.first)


89
90
91
92
93
94
# File 'lib/dommy/rails/minitest/assertions.rb', line 89

def assert_dom_has_turbo_frame(actual, id = nil, text: nil, count: nil, msg: nil)
  matched = Dommy::Rails::PageInspector.turbo_frames(dom_document_for(actual), id, text: text)
  msg ||= "expected to contain turbo-frame#{id ? " ##{id}" : ""}#{text ? " with text #{text.inspect}" : ""}, found #{matched.size}"
  assert(Dommy::Internal::DomMatching.count_matches?(matched.size, count), msg)
  yield matched.first if block_given? && matched.any?
end

#assert_dom_has_turbo_stream(actual, action:, target:, msg: nil) {|Dommy::Rails::TurboStream.fragment_document(stream)| ... } ⇒ Object

Yields:



140
141
142
143
144
145
# File 'lib/dommy/rails/minitest/assertions.rb', line 140

def assert_dom_has_turbo_stream(actual, action:, target:, msg: nil)
  stream = Dommy::Rails::TurboStream.find(dom_body_for(actual), action: action, target: target)
  msg ||= "expected to find turbo-stream action=#{action} target=#{target}"
  assert(stream, msg)
  yield Dommy::Rails::TurboStream.fragment_document(stream) if block_given? && stream
end

#assert_dom_has_unchecked_field(actual, name = nil, msg: nil) ⇒ Object



120
121
122
123
124
# File 'lib/dommy/rails/minitest/assertions.rb', line 120

def assert_dom_has_unchecked_field(actual, name = nil, msg: nil)
  matched = Dommy::Rails::PageInspector.checkable_fields(dom_document_for(actual), name: name, checked: false)
  msg ||= "expected to find unchecked field#{name ? " #{name.inspect}" : ""}"
  assert(matched.any?, msg)
end

#assert_dom_has_xpath(actual, expression, text: nil, count: nil, msg: nil) ⇒ Object



21
22
23
24
25
# File 'lib/dommy/rails/minitest/assertions.rb', line 21

def assert_dom_has_xpath(actual, expression, text: nil, count: nil, msg: nil)
  matched = Dommy::Rails::PageInspector.xpath_matches(dom_document_for(actual), expression, text: text)
  msg ||= "expected to contain XPath #{expression.inspect}#{text ? " with text #{text.inspect}" : ""}, found #{matched.size}"
  assert(Dommy::Internal::DomMatching.count_matches?(matched.size, count), msg)
end

#assert_dom_no_duplicate_ids(actual, msg: nil) ⇒ Object



217
218
219
220
221
222
# File 'lib/dommy/rails/minitest/assertions.rb', line 217

def assert_dom_no_duplicate_ids(actual, msg: nil)
  document = dom_document_for(actual)
  duplicates = Dommy::Rails::Lint.duplicate_ids(document)
  msg ||= "expected no duplicate IDs, found: #{duplicates.join(', ')}"
  assert(duplicates.empty?, msg)
end


238
239
240
241
242
# File 'lib/dommy/rails/minitest/assertions.rb', line 238

def assert_dom_no_empty_links(actual, msg: nil)
  issues = Dommy::Rails::Lint.empty_links(dom_document_for(actual))
  msg ||= "expected no empty links, found #{issues.size} issues"
  assert(issues.empty?, msg)
end

#assert_dom_no_invalid_aria_references(actual, msg: nil) ⇒ Object



224
225
226
227
228
229
# File 'lib/dommy/rails/minitest/assertions.rb', line 224

def assert_dom_no_invalid_aria_references(actual, msg: nil)
  document = dom_document_for(actual)
  issues = Dommy::Rails::Lint.invalid_aria_references(document)
  msg ||= "expected no invalid ARIA references, found #{issues.size} issues"
  assert(issues.empty?, msg)
end

#assert_dom_no_missing_form_labels(actual, msg: nil) ⇒ Object



231
232
233
234
235
236
# File 'lib/dommy/rails/minitest/assertions.rb', line 231

def assert_dom_no_missing_form_labels(actual, msg: nil)
  document = dom_document_for(actual)
  issues = Dommy::Rails::Lint.missing_form_labels(document)
  msg ||= "expected no missing form labels, found #{issues.size} issues"
  assert(issues.empty?, msg)
end

#assert_dom_no_nested_interactive_elements(actual, msg: nil) ⇒ Object



244
245
246
247
248
# File 'lib/dommy/rails/minitest/assertions.rb', line 244

def assert_dom_no_nested_interactive_elements(actual, msg: nil)
  issues = Dommy::Rails::Lint.nested_interactive_elements(dom_document_for(actual))
  msg ||= "expected no nested interactive elements, found #{issues.size} issues"
  assert(issues.empty?, msg)
end

#assert_dom_removes_turbo_stream(actual, target, msg: nil, &block) ⇒ Object



165
166
167
# File 'lib/dommy/rails/minitest/assertions.rb', line 165

def assert_dom_removes_turbo_stream(actual, target, msg: nil, &block)
  assert_dom_has_turbo_stream(actual, action: "remove", target: target, msg: msg, &block)
end

#assert_dom_replaces_turbo_stream(actual, target, msg: nil, &block) ⇒ Object



157
158
159
# File 'lib/dommy/rails/minitest/assertions.rb', line 157

def assert_dom_replaces_turbo_stream(actual, target, msg: nil, &block)
  assert_dom_has_turbo_stream(actual, action: "replace", target: target, msg: msg, &block)
end

#assert_dom_updates_turbo_stream(actual, target, msg: nil, &block) ⇒ Object



161
162
163
# File 'lib/dommy/rails/minitest/assertions.rb', line 161

def assert_dom_updates_turbo_stream(actual, target, msg: nil, &block)
  assert_dom_has_turbo_stream(actual, action: "update", target: target, msg: msg, &block)
end


250
251
252
253
254
255
# File 'lib/dommy/rails/minitest/assertions.rb', line 250

def assert_mail_has_html_link(mail, text = nil, href: nil, count: nil, msg: nil)
  document = Dommy::Rails::MailPart.html_document(mail)
  msg ||= "expected mail to have an HTML part"
  assert(document, msg)
  assert_dom_has_link(document, text, href: href, count: count, msg: msg)
end

#assert_mail_has_html_text(mail, text, msg: nil) ⇒ Object



257
258
259
260
261
262
# File 'lib/dommy/rails/minitest/assertions.rb', line 257

def assert_mail_has_html_text(mail, text, msg: nil)
  document = Dommy::Rails::MailPart.html_document(mail)
  msg ||= "expected mail HTML to contain #{text.inspect}"
  assert(document, "expected mail to have an HTML part")
  assert_dom_has_text(document, text, msg: msg)
end

#assert_mail_has_plain_text(mail, text, msg: nil) ⇒ Object



264
265
266
267
268
# File 'lib/dommy/rails/minitest/assertions.rb', line 264

def assert_mail_has_plain_text(mail, text, msg: nil)
  body = Dommy::Rails::MailPart.plain_body(mail).to_s
  msg ||= "expected mail plain text to contain #{text.inspect}, got #{body.inspect}"
  assert(Dommy::Internal::DomMatching.text_matches?(body, text), msg)
end

#refute_dom_has_css(scope, selector, text: nil, count: nil, msg: nil) ⇒ Object



9
10
11
# File 'lib/dommy/rails/minitest/assertions.rb', line 9

def refute_dom_has_css(scope, selector, text: nil, count: nil, msg: nil)
  refute_dom_contains(scope, selector, text: text, count: count, msg: msg)
end

#refute_dom_has_form(actual, action: nil, method: nil, model: nil, msg: nil) ⇒ Object



133
134
135
136
137
138
# File 'lib/dommy/rails/minitest/assertions.rb', line 133

def refute_dom_has_form(actual, action: nil, method: nil, model: nil, msg: nil)
  document = dom_document_for(actual)
  matched = Dommy::Rails::FormInspector.matches?(document, action: action, method: method, model: model)
  msg ||= "expected NOT to find form matching #{form_desc(action: action, method: method, model: model)}"
  refute(matched, msg)
end


83
84
85
86
87
# File 'lib/dommy/rails/minitest/assertions.rb', line 83

def refute_dom_has_link(actual, text = nil, href: nil, count: nil, msg: nil)
  matched = Dommy::Rails::PageInspector.links(dom_document_for(actual), text: text, href: href)
  msg ||= "expected NOT to contain link#{text ? " with text #{text.inspect}" : ""}#{href ? " with href #{href.inspect}" : ""}, found #{matched.size}"
  refute(Dommy::Internal::DomMatching.count_matches?(matched.size, count), msg)
end

#refute_dom_has_meta(actual, name: nil, property: nil, content: nil, msg: nil) ⇒ Object



59
60
61
62
63
# File 'lib/dommy/rails/minitest/assertions.rb', line 59

def refute_dom_has_meta(actual, name: nil, property: nil, content: nil, msg: nil)
  matched = Dommy::Rails::PageInspector.meta_matches?(dom_document_for(actual), name: name, property: property, content: content)
  msg ||= "expected NOT to find meta #{meta_desc(name: name, property: property, content: content)}"
  refute(matched, msg)
end

#refute_dom_has_select(actual, name = nil, label: nil, count: nil, msg: nil) ⇒ Object



108
109
110
111
112
# File 'lib/dommy/rails/minitest/assertions.rb', line 108

def refute_dom_has_select(actual, name = nil, label: nil, count: nil, msg: nil)
  matched = Dommy::Rails::PageInspector.selects(dom_document_for(actual), name: name, label: label)
  msg ||= "expected NOT to contain select#{name ? " with name #{name.inspect}" : ""}#{label ? " with label #{label.inspect}" : ""}, found #{matched.size}"
  refute(Dommy::Internal::DomMatching.count_matches?(matched.size, count), msg)
end

#refute_dom_has_stimulus_action(actual, action, msg: nil) ⇒ Object



187
188
189
190
191
# File 'lib/dommy/rails/minitest/assertions.rb', line 187

def refute_dom_has_stimulus_action(actual, action, msg: nil)
  matched = Dommy::Rails::Stimulus.action?(dom_document_for(actual), action)
  msg ||= "expected NOT to find element with Stimulus action '#{action}'"
  refute(matched, msg)
end

#refute_dom_has_stimulus_controller(actual, name, msg: nil) ⇒ Object



175
176
177
178
179
# File 'lib/dommy/rails/minitest/assertions.rb', line 175

def refute_dom_has_stimulus_controller(actual, name, msg: nil)
  matched = Dommy::Rails::Stimulus.controller?(dom_document_for(actual), name)
  msg ||= "expected NOT to find element with Stimulus controller '#{name}'"
  refute(matched, msg)
end

#refute_dom_has_stimulus_target(actual, controller, target, msg: nil) ⇒ Object



199
200
201
202
203
# File 'lib/dommy/rails/minitest/assertions.rb', line 199

def refute_dom_has_stimulus_target(actual, controller, target, msg: nil)
  matched = Dommy::Rails::Stimulus.target?(dom_document_for(actual), controller, target)
  msg ||= "expected NOT to find element with Stimulus target '#{controller}.#{target}'"
  refute(matched, msg)
end

#refute_dom_has_stimulus_value(actual, controller, key, value, msg: nil) ⇒ Object



211
212
213
214
215
# File 'lib/dommy/rails/minitest/assertions.rb', line 211

def refute_dom_has_stimulus_value(actual, controller, key, value, msg: nil)
  matched = Dommy::Rails::Stimulus.value?(dom_document_for(actual), controller, key, value)
  msg ||= "expected NOT to find element with Stimulus value '#{controller}.#{key}' = #{value.inspect}"
  refute(matched, msg)
end

#refute_dom_has_text(scope, text, msg: nil) ⇒ Object



17
18
19
# File 'lib/dommy/rails/minitest/assertions.rb', line 17

def refute_dom_has_text(scope, text, msg: nil)
  refute_dom_contains_text(scope, text, msg: msg)
end

#refute_dom_has_title(actual, expected, msg: nil) ⇒ Object



39
40
41
42
43
# File 'lib/dommy/rails/minitest/assertions.rb', line 39

def refute_dom_has_title(actual, expected, msg: nil)
  matched = Dommy::Rails::PageInspector.title_matches?(dom_document_for(actual), expected)
  msg ||= "expected document title NOT to match #{expected.inspect}"
  refute(matched, msg)
end

#refute_dom_has_turbo_frame(actual, id = nil, text: nil, count: nil, msg: nil) ⇒ Object



96
97
98
99
100
# File 'lib/dommy/rails/minitest/assertions.rb', line 96

def refute_dom_has_turbo_frame(actual, id = nil, text: nil, count: nil, msg: nil)
  matched = Dommy::Rails::PageInspector.turbo_frames(dom_document_for(actual), id, text: text)
  msg ||= "expected NOT to contain turbo-frame#{id ? " ##{id}" : ""}#{text ? " with text #{text.inspect}" : ""}, found #{matched.size}"
  refute(Dommy::Internal::DomMatching.count_matches?(matched.size, count), msg)
end

#refute_dom_has_turbo_stream(actual, action:, target:, msg: nil) ⇒ Object



147
148
149
150
151
# File 'lib/dommy/rails/minitest/assertions.rb', line 147

def refute_dom_has_turbo_stream(actual, action:, target:, msg: nil)
  matched = Dommy::Rails::TurboStream.matches?(dom_body_for(actual), action: action, target: target)
  msg ||= "expected NOT to find turbo-stream action=#{action} target=#{target}"
  refute(matched, msg)
end

#refute_dom_has_xpath(actual, expression, text: nil, count: nil, msg: nil) ⇒ Object



27
28
29
30
31
# File 'lib/dommy/rails/minitest/assertions.rb', line 27

def refute_dom_has_xpath(actual, expression, text: nil, count: nil, msg: nil)
  matched = Dommy::Rails::PageInspector.xpath_matches(dom_document_for(actual), expression, text: text)
  msg ||= "expected NOT to contain XPath #{expression.inspect}#{text ? " with text #{text.inspect}" : ""}, found #{matched.size}"
  refute(Dommy::Internal::DomMatching.count_matches?(matched.size, count), msg)
end