summaryrefslogtreecommitdiff
path: root/spec/helpers/safe_format_helper_spec.rb
blob: ced48b0c9c1e099b9cc6b9b5fc4beb2c169ea60b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
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: '&lt;b&gt;strong&lt;/b&gt;'

    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> &lt;a href=&quot;&quot;&gt;link&lt;/a&gt;'

    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