diff options
Diffstat (limited to 'spec/helpers/safe_format_helper_spec.rb')
-rw-r--r-- | spec/helpers/safe_format_helper_spec.rb | 135 |
1 files changed, 111 insertions, 24 deletions
diff --git a/spec/helpers/safe_format_helper_spec.rb b/spec/helpers/safe_format_helper_spec.rb index 3639494060d..b5db623f14d 100644 --- a/spec/helpers/safe_format_helper_spec.rb +++ b/spec/helpers/safe_format_helper_spec.rb @@ -4,31 +4,73 @@ require 'spec_helper' RSpec.describe SafeFormatHelper, feature_category: :shared do describe '#safe_format' do - shared_examples 'safe formatting' do |format, args:, result:| - subject { helper.safe_format(format, **args) } + shared_examples 'safe formatting' do + subject { helper.safe_format(format, args) } it { is_expected.to eq(result) } it { is_expected.to be_html_safe } end - it_behaves_like 'safe formatting', '', args: {}, result: '' - it_behaves_like 'safe formatting', 'Foo', args: {}, result: 'Foo' + it_behaves_like 'safe formatting' do + let(:format) { '' } + let(:args) { {} } + let(:result) { '' } + end - it_behaves_like 'safe formatting', '<b>strong</b>', args: {}, - result: '<b>strong</b>' + it_behaves_like 'safe formatting' do + let(:format) { 'Foo' } + let(:args) { {} } + let(:result) { 'Foo' } + end - it_behaves_like 'safe formatting', '%{open}strong%{close}', - args: { open: '<b>'.html_safe, close: '</b>'.html_safe }, - result: '<b>strong</b>' + it_behaves_like 'safe formatting' do + let(:format) { '<b>strong</b>' } + let(:args) { {} } + let(:result) { '<b>strong</b>' } + end + + it_behaves_like 'safe formatting' do + let(:format) { '%{open}strong%{close}' } + let(:args) { { open: '<b>'.html_safe, close: '</b>'.html_safe } } + let(:result) { '<b>strong</b>' } + end - it_behaves_like 'safe formatting', '%{open}strong%{close} %{user_input}', - args: { open: '<b>'.html_safe, close: '</b>'.html_safe, - user_input: '<a href="">link</a>' }, - result: '<b>strong</b> <a href="">link</a>' + it_behaves_like 'safe formatting' do + let(:format) { '%{open}strong%{close} %{user_input}' } + + let(:args) do + { open: '<b>'.html_safe, close: '</b>'.html_safe, + user_input: '<a href="">link</a>' } + end + + let(:result) { '<b>strong</b> <a href="">link</a>' } + end context 'when format is marked as html_safe' do - it_behaves_like 'safe formatting', '<b>strong</b>'.html_safe, args: {}, - result: '<b>strong</b>' + it_behaves_like 'safe formatting' do + let(:format) { '<b>strong</b>'.html_safe } + let(:args) { {} } + let(:result) { '<b>strong</b>' } + end + end + + context 'with multiple args' do + it_behaves_like 'safe formatting' do + let(:format) { '%{a}c%{b} %{x}z%{y}' } + + let(:args) do + [ + { a: '<a>'.html_safe, b: '</a>'.html_safe }, + # Demonstrate shadowing + { x: '<XX>'.html_safe, y: '</XX>'.html_safe }, + { x: '<x>'.html_safe, y: '</x>'.html_safe } + ] + end + + let(:result) { '<a>c</a> <x>z</x>' } + + subject { helper.safe_format(format, *args) } + end end context 'with a view component' do @@ -49,17 +91,62 @@ RSpec.describe SafeFormatHelper, feature_category: :shared do end context 'with format containing escaped entities' do - it_behaves_like 'safe formatting', 'In < hour', - args: {}, - result: 'In < hour' + it_behaves_like 'safe formatting' do + let(:format) { 'In < hour' } + let(:args) { {} } + let(:result) { 'In < hour' } + end + + it_behaves_like 'safe formatting' do + let(:format) { '"air"' } + let(:args) { {} } + let(:result) { '"air"' } + end + + it_behaves_like 'safe formatting' do + let(:format) { 'Mix & match > all' } + let(:args) { {} } + let(:result) { 'Mix & match > all' } + end + end + end + + describe '#tag_pair' do + using RSpec::Parameterized::TableSyntax - it_behaves_like 'safe formatting', '"air"', - args: {}, - result: '"air"' + let(:tag) { plain_tag.html_safe } + let(:open_name) { :tag_open } + let(:close_name) { :tag_close } - it_behaves_like 'safe formatting', 'Mix & match > all', - args: {}, - result: 'Mix & match > all' + subject(:result) { tag_pair(tag, open_name, close_name) } + + where(:plain_tag, :open, :close) do + '' | nil | nil + 'a' | nil | nil + '<a' | nil | nil + '<a>' | nil | nil + '<a><a>' | nil | nil + '<input/>' | nil | nil + '<a></a>' | '<a>' | '</a>' + '<a href="">x</a>' | '<a href="">' | '</a>' + end + + with_them do + if params[:open] && params[:close] + it { is_expected.to eq({ open_name => open, close_name => close }) } + specify { expect(result.values).to be_all(&:html_safe?) } + else + it { is_expected.to eq({}) } + end + end + + context 'when tag is not html_safe' do + # `to_str` turns a html_safe string into a plain String. + let(:tag) { helper.tag.strong.to_str } + + it 'raises an ArgumentError' do + expect { result }.to raise_error ArgumentError, 'Argument `tag` must be `html_safe`!' + end end end end |