require 'rails_helper' # Adapted from Rails 5 to support Rails 4. # https://github.com/rails/rails/blob/9c35bf2a6a27431c6aa283db781c19f61c5155be/actionview/test/template/output_safety_helper_test.rb RSpec.describe ActiveAdmin::OutputSafetyHelper, type: :view do include described_class before do @string = "hello" end describe "to_sentence" do it "escapes non-html_safe values" do actual = to_sentence(%w(< > & ' ")) assert actual.html_safe? assert_equal("<, >, &, ', and "", actual) actual = to_sentence(%w( ") end it "does not escape html_safe values" do ptag = content_tag("p") do safe_join(["shady stuff", tag("br")]) end url = "https://example.com" expected = %(#{url} and

<marquee>shady stuff</marquee>

) actual = to_sentence([link_to(url, url), ptag]) assert actual.html_safe? assert_equal(expected, actual) end it "handles blank strings" do actual = to_sentence(["", "two", "three"]) assert actual.html_safe? assert_equal ", two, and three", actual end it "handles nil values" do actual = to_sentence([nil, "two", "three"]) assert actual.html_safe? assert_equal ", two, and three", actual end it "still supports ActiveSupports Array#to_sentence arguments" do assert_equal "one two, and three", to_sentence(["one", "two", "three"], words_connector: " ") assert_equal "one & two, and three", to_sentence(["one", "two", "three"], words_connector: " & ".html_safe) assert_equal "onetwo, and three", to_sentence(["one", "two", "three"], words_connector: nil) assert_equal "one, two, and also three", to_sentence(["one", "two", "three"], last_word_connector: ", and also ") assert_equal "one, twothree", to_sentence(["one", "two", "three"], last_word_connector: nil) assert_equal "one, two three", to_sentence(["one", "two", "three"], last_word_connector: " ") assert_equal "one, two and three", to_sentence(["one", "two", "three"], last_word_connector: " and ") end it "is not affected by $," do separator_was = $, $, = "|" begin assert_equal "one and two", to_sentence(["one", "two"]) assert_equal "one, two, and three", to_sentence(["one", "two", "three"]) ensure $, = separator_was end end end end