If we don't use the `exact` option, tests will pass even if filling in CKEditor adds the content twice or adds the new content to the existing content, which has actually happened and has gone mostly unnoticed while testing several ways to fill in CKEditor with Capybara (particularly, when using Capybara's `send_keys` method). The problem was detected by just one test, which checked the original content wasn't present anymore after updating a record.
32 lines
762 B
Ruby
32 lines
762 B
Ruby
RSpec::Matchers.define :have_ckeditor do |label, with:|
|
|
define_method :textarea_id do
|
|
find("label", text: label)[:for]
|
|
end
|
|
|
|
define_method :ckeditor_id do
|
|
"#cke_#{textarea_id}"
|
|
end
|
|
|
|
define_method :has_ckeditor? do
|
|
has_css?("label", text: label) && has_css?(ckeditor_id)
|
|
end
|
|
|
|
match do
|
|
return false unless has_ckeditor?
|
|
|
|
page.within(ckeditor_id) do
|
|
within_frame(0) { has_content?(with, exact: true) }
|
|
end
|
|
end
|
|
|
|
failure_message do
|
|
if has_ckeditor?
|
|
text = page.within(ckeditor_id) { within_frame(0) { page.text } }
|
|
|
|
"expected to find visible CKEditor '#{label}' with '#{with}', but had '#{text}'"
|
|
else
|
|
"expected to find visible CKEditor '#{label}' but there were no matches."
|
|
end
|
|
end
|
|
end
|