2018-10-09 17:10:07 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2019-01-04 19:43:11 -05:00
|
|
|
require "test_helper"
|
2018-10-09 17:10:07 -04:00
|
|
|
|
|
|
|
class ActionText::FormHelperTest < ActionView::TestCase
|
|
|
|
tests ActionText::TagHelper
|
|
|
|
|
2019-09-24 00:47:35 -04:00
|
|
|
def form_with(*, **)
|
2019-02-21 11:43:57 -05:00
|
|
|
@output_buffer = super
|
|
|
|
end
|
|
|
|
|
|
|
|
teardown do
|
|
|
|
I18n.backend.reload!
|
|
|
|
end
|
|
|
|
|
|
|
|
setup do
|
|
|
|
I18n.backend.store_translations("placeholder",
|
|
|
|
activerecord: {
|
|
|
|
attributes: {
|
|
|
|
message: {
|
|
|
|
title: "Story title"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2021-02-28 11:20:59 -05:00
|
|
|
test "rich text area tag" do
|
|
|
|
message = Message.new
|
|
|
|
|
|
|
|
form_with model: message, scope: :message do |form|
|
|
|
|
rich_text_area_tag :content, message.content, { input: "trix_input_1" }
|
|
|
|
end
|
|
|
|
|
|
|
|
assert_dom_equal \
|
2021-05-24 18:44:36 -04:00
|
|
|
'<form action="/messages" accept-charset="UTF-8" method="post">' \
|
2021-02-28 11:20:59 -05:00
|
|
|
'<input type="hidden" name="content" id="trix_input_1" />' \
|
|
|
|
'<trix-editor input="trix_input_1" class="trix-content" data-direct-upload-url="http://test.host/rails/active_storage/direct_uploads" data-blob-url-template="http://test.host/rails/active_storage/blobs/redirect/:signed_id/:filename">' \
|
|
|
|
"</trix-editor>" \
|
|
|
|
"</form>",
|
|
|
|
output_buffer
|
|
|
|
end
|
|
|
|
|
2018-12-31 12:16:21 -05:00
|
|
|
test "form with rich text area" do
|
|
|
|
form_with model: Message.new, scope: :message do |form|
|
|
|
|
form.rich_text_area :content
|
|
|
|
end
|
|
|
|
|
|
|
|
assert_dom_equal \
|
2021-05-24 18:44:36 -04:00
|
|
|
'<form action="/messages" accept-charset="UTF-8" method="post">' \
|
2018-12-31 12:16:21 -05:00
|
|
|
'<input type="hidden" name="message[content]" id="message_content_trix_input_message" />' \
|
2020-05-11 16:21:58 -04:00
|
|
|
'<trix-editor id="message_content" input="message_content_trix_input_message" class="trix-content" data-direct-upload-url="http://test.host/rails/active_storage/direct_uploads" data-blob-url-template="http://test.host/rails/active_storage/blobs/redirect/:signed_id/:filename">' \
|
2019-01-04 19:43:11 -05:00
|
|
|
"</trix-editor>" \
|
|
|
|
"</form>",
|
2018-12-31 12:16:21 -05:00
|
|
|
output_buffer
|
2018-10-09 17:10:07 -04:00
|
|
|
end
|
|
|
|
|
2018-12-31 12:16:21 -05:00
|
|
|
test "form with rich text area having class" do
|
|
|
|
form_with model: Message.new, scope: :message do |form|
|
|
|
|
form.rich_text_area :content, class: "custom-class"
|
2018-10-09 17:10:07 -04:00
|
|
|
end
|
|
|
|
|
2018-12-31 12:16:21 -05:00
|
|
|
assert_dom_equal \
|
2021-05-24 18:44:36 -04:00
|
|
|
'<form action="/messages" accept-charset="UTF-8" method="post">' \
|
2018-12-31 12:16:21 -05:00
|
|
|
'<input type="hidden" name="message[content]" id="message_content_trix_input_message" />' \
|
2020-05-11 16:21:58 -04:00
|
|
|
'<trix-editor id="message_content" input="message_content_trix_input_message" class="custom-class" data-direct-upload-url="http://test.host/rails/active_storage/direct_uploads" data-blob-url-template="http://test.host/rails/active_storage/blobs/redirect/:signed_id/:filename">' \
|
2019-01-04 19:43:11 -05:00
|
|
|
"</trix-editor>" \
|
|
|
|
"</form>",
|
2018-12-31 12:16:21 -05:00
|
|
|
output_buffer
|
2018-10-09 17:10:07 -04:00
|
|
|
end
|
2018-10-10 16:03:54 -04:00
|
|
|
|
2018-12-31 12:16:21 -05:00
|
|
|
test "form with rich text area for non-attribute" do
|
|
|
|
form_with model: Message.new, scope: :message do |form|
|
|
|
|
form.rich_text_area :not_an_attribute
|
2018-10-10 16:03:54 -04:00
|
|
|
end
|
|
|
|
|
2018-12-31 12:16:21 -05:00
|
|
|
assert_dom_equal \
|
2021-05-24 18:44:36 -04:00
|
|
|
'<form action="/messages" accept-charset="UTF-8" method="post">' \
|
2018-12-31 12:16:21 -05:00
|
|
|
'<input type="hidden" name="message[not_an_attribute]" id="message_not_an_attribute_trix_input_message" />' \
|
2020-05-11 16:21:58 -04:00
|
|
|
'<trix-editor id="message_not_an_attribute" input="message_not_an_attribute_trix_input_message" class="trix-content" data-direct-upload-url="http://test.host/rails/active_storage/direct_uploads" data-blob-url-template="http://test.host/rails/active_storage/blobs/redirect/:signed_id/:filename">' \
|
2019-01-04 19:43:11 -05:00
|
|
|
"</trix-editor>" \
|
|
|
|
"</form>",
|
2018-12-31 12:16:21 -05:00
|
|
|
output_buffer
|
2018-10-10 16:03:54 -04:00
|
|
|
end
|
|
|
|
|
2018-12-31 12:16:21 -05:00
|
|
|
test "modelless form with rich text area" do
|
|
|
|
form_with url: "/messages", scope: :message do |form|
|
2021-02-28 11:20:59 -05:00
|
|
|
form.rich_text_area :content, { input: "trix_input_2" }
|
2018-10-10 16:03:54 -04:00
|
|
|
end
|
|
|
|
|
2018-12-31 12:16:21 -05:00
|
|
|
assert_dom_equal \
|
2021-05-24 18:44:36 -04:00
|
|
|
'<form action="/messages" accept-charset="UTF-8" method="post">' \
|
2021-02-28 11:20:59 -05:00
|
|
|
'<input type="hidden" name="message[content]" id="trix_input_2" />' \
|
|
|
|
'<trix-editor id="message_content" input="trix_input_2" class="trix-content" data-direct-upload-url="http://test.host/rails/active_storage/direct_uploads" data-blob-url-template="http://test.host/rails/active_storage/blobs/redirect/:signed_id/:filename">' \
|
2019-01-04 19:43:11 -05:00
|
|
|
"</trix-editor>" \
|
|
|
|
"</form>",
|
2018-12-31 12:16:21 -05:00
|
|
|
output_buffer
|
|
|
|
end
|
|
|
|
|
2019-02-21 11:43:57 -05:00
|
|
|
test "form with rich text area having placeholder without locale" do
|
|
|
|
form_with model: Message.new, scope: :message do |form|
|
|
|
|
form.rich_text_area :content, placeholder: true
|
|
|
|
end
|
|
|
|
|
|
|
|
assert_dom_equal \
|
2021-05-24 18:44:36 -04:00
|
|
|
'<form action="/messages" accept-charset="UTF-8" method="post">' \
|
2019-02-21 11:43:57 -05:00
|
|
|
'<input type="hidden" name="message[content]" id="message_content_trix_input_message" />' \
|
2020-05-11 16:21:58 -04:00
|
|
|
'<trix-editor placeholder="Content" id="message_content" input="message_content_trix_input_message" class="trix-content" data-direct-upload-url="http://test.host/rails/active_storage/direct_uploads" data-blob-url-template="http://test.host/rails/active_storage/blobs/redirect/:signed_id/:filename">' \
|
2019-02-21 11:43:57 -05:00
|
|
|
"</trix-editor>" \
|
|
|
|
"</form>",
|
|
|
|
output_buffer
|
|
|
|
end
|
|
|
|
|
|
|
|
test "form with rich text area having placeholder with locale" do
|
|
|
|
I18n.with_locale :placeholder do
|
|
|
|
form_with model: Message.new, scope: :message do |form|
|
|
|
|
form.rich_text_area :title, placeholder: true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
assert_dom_equal \
|
2021-05-24 18:44:36 -04:00
|
|
|
'<form action="/messages" accept-charset="UTF-8" method="post">' \
|
2019-02-21 11:43:57 -05:00
|
|
|
'<input type="hidden" name="message[title]" id="message_title_trix_input_message" />' \
|
2020-05-11 16:21:58 -04:00
|
|
|
'<trix-editor placeholder="Story title" id="message_title" input="message_title_trix_input_message" class="trix-content" data-direct-upload-url="http://test.host/rails/active_storage/direct_uploads" data-blob-url-template="http://test.host/rails/active_storage/blobs/redirect/:signed_id/:filename">' \
|
2019-02-21 11:43:57 -05:00
|
|
|
"</trix-editor>" \
|
|
|
|
"</form>",
|
|
|
|
output_buffer
|
2018-10-10 16:03:54 -04:00
|
|
|
end
|
2020-09-01 21:43:07 -04:00
|
|
|
|
|
|
|
test "form with rich text area with value" do
|
|
|
|
form_with model: Message.new, scope: :message do |form|
|
|
|
|
form.rich_text_area :title, value: "<h1>hello world</h1>"
|
|
|
|
end
|
|
|
|
|
|
|
|
assert_dom_equal \
|
2021-05-24 18:44:36 -04:00
|
|
|
'<form action="/messages" accept-charset="UTF-8" method="post">' \
|
2020-09-01 21:43:07 -04:00
|
|
|
'<input type="hidden" name="message[title]" id="message_title_trix_input_message" value="<h1>hello world</h1>" />' \
|
|
|
|
'<trix-editor id="message_title" input="message_title_trix_input_message" class="trix-content" data-direct-upload-url="http://test.host/rails/active_storage/direct_uploads" data-blob-url-template="http://test.host/rails/active_storage/blobs/redirect/:signed_id/:filename">' \
|
|
|
|
"</trix-editor>" \
|
|
|
|
"</form>",
|
|
|
|
output_buffer
|
|
|
|
end
|
2021-04-25 08:11:17 -04:00
|
|
|
|
|
|
|
test "form with rich text area with form attribute" do
|
|
|
|
form_with model: Message.new, scope: :message do |form|
|
|
|
|
form.rich_text_area :title, form: "other_form"
|
|
|
|
end
|
|
|
|
|
|
|
|
assert_dom_equal \
|
2021-05-24 18:44:36 -04:00
|
|
|
'<form action="/messages" accept-charset="UTF-8" method="post">' \
|
2021-04-25 08:11:17 -04:00
|
|
|
'<input type="hidden" name="message[title]" id="message_title_trix_input_message" form="other_form" />' \
|
|
|
|
'<trix-editor id="message_title" input="message_title_trix_input_message" class="trix-content" data-direct-upload-url="http://test.host/rails/active_storage/direct_uploads" data-blob-url-template="http://test.host/rails/active_storage/blobs/redirect/:signed_id/:filename">' \
|
|
|
|
"</trix-editor>" \
|
|
|
|
"</form>",
|
|
|
|
output_buffer
|
|
|
|
end
|
2021-09-19 14:56:17 -04:00
|
|
|
|
|
|
|
test "form with rich text area with data[direct_upload_url]" do
|
|
|
|
form_with model: Message.new, scope: :message do |form|
|
|
|
|
form.rich_text_area :content, data: { direct_upload_url: "http://test.host/direct_uploads" }
|
|
|
|
end
|
|
|
|
|
|
|
|
assert_dom_equal \
|
|
|
|
'<form action="/messages" accept-charset="UTF-8" method="post">' \
|
|
|
|
'<input type="hidden" name="message[content]" id="message_content_trix_input_message" />' \
|
|
|
|
'<trix-editor id="message_content" input="message_content_trix_input_message" class="trix-content" data-direct-upload-url="http://test.host/direct_uploads" data-blob-url-template="http://test.host/rails/active_storage/blobs/redirect/:signed_id/:filename">' \
|
|
|
|
"</trix-editor>" \
|
|
|
|
"</form>",
|
|
|
|
output_buffer
|
|
|
|
end
|
|
|
|
|
|
|
|
test "form with rich text area with data[blob_url_template]" do
|
|
|
|
form_with model: Message.new, scope: :message do |form|
|
|
|
|
form.rich_text_area :content, data: { blob_url_template: "http://test.host/blobs/:signed_id/:filename" }
|
|
|
|
end
|
|
|
|
|
|
|
|
assert_dom_equal \
|
|
|
|
'<form action="/messages" accept-charset="UTF-8" method="post">' \
|
|
|
|
'<input type="hidden" name="message[content]" id="message_content_trix_input_message" />' \
|
|
|
|
'<trix-editor id="message_content" input="message_content_trix_input_message" class="trix-content" data-direct-upload-url="http://test.host/rails/active_storage/direct_uploads" data-blob-url-template="http://test.host/blobs/:signed_id/:filename">' \
|
|
|
|
"</trix-editor>" \
|
|
|
|
"</form>",
|
|
|
|
output_buffer
|
|
|
|
end
|
2018-10-09 17:10:07 -04:00
|
|
|
end
|