diff options
Diffstat (limited to 'spec/helpers/safe_format_helper_spec.rb')
-rw-r--r-- | spec/helpers/safe_format_helper_spec.rb | 41 |
1 files changed, 41 insertions, 0 deletions
diff --git a/spec/helpers/safe_format_helper_spec.rb b/spec/helpers/safe_format_helper_spec.rb new file mode 100644 index 00000000000..ced48b0c9c1 --- /dev/null +++ b/spec/helpers/safe_format_helper_spec.rb @@ -0,0 +1,41 @@ +# frozen_string_literal: true + +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) } + + 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', '<b>strong</b>', args: {}, + result: '<b>strong</b>' + + 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', '%{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>' + + context 'when format is marked as html_safe' do + let(:format) { '<b>strong</b>'.html_safe } + let(:args) { {} } + + it 'raises an error' do + message = 'Argument `format` must not be marked as html_safe!' + + expect { helper.safe_format(format, **args) } + .to raise_error ArgumentError, message + end + end + end +end |