Welcome to mirror list, hosted at ThFree Co, Russian Federation.

safe_format_helper_spec.rb « helpers « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: b5db623f14d00a344a004c02b5b34fa7da03a617 (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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe SafeFormatHelper, feature_category: :shared do
  describe '#safe_format' do
    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' do
      let(:format) { '' }
      let(:args) { {} }
      let(:result) { '' }
    end

    it_behaves_like 'safe formatting' do
      let(:format) { 'Foo' }
      let(:args) { {} }
      let(:result) { 'Foo' }
    end

    it_behaves_like 'safe formatting' do
      let(:format) { '<b>strong</b>' }
      let(:args) { {} }
      let(:result) { '&lt;b&gt;strong&lt;/b&gt;' }
    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' 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> &lt;a href=&quot;&quot;&gt;link&lt;/a&gt;' }
    end

    context 'when format is marked as html_safe' do
      it_behaves_like 'safe formatting' do
        let(:format) { '<b>strong</b>'.html_safe }
        let(:args) { {} }
        let(:result) { '&lt;b&gt;strong&lt;/b&gt;' }
      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
      let(:view_component) do
        Class.new(ViewComponent::Base) do
          include SafeFormatHelper

          def call
            safe_format('<b>%{value}</b>', value: '<br>')
          end
        end
      end

      it 'safetly formats' do
        expect(view_component.new.call)
          .to eq('&lt;b&gt;&lt;br&gt;&lt;/b&gt;')
      end
    end

    context 'with format containing escaped entities' do
      it_behaves_like 'safe formatting' do
        let(:format) { 'In &lt; hour' }
        let(:args) { {} }
        let(:result) { 'In &lt; hour' }
      end

      it_behaves_like 'safe formatting' do
        let(:format) { '&quot;air&quot;' }
        let(:args) { {} }
        let(:result) { '&quot;air&quot;' }
      end

      it_behaves_like 'safe formatting' do
        let(:format) { 'Mix & match &gt; all' }
        let(:args) { {} }
        let(:result) { 'Mix &amp; match &gt; all' }
      end
    end
  end

  describe '#tag_pair' do
    using RSpec::Parameterized::TableSyntax

    let(:tag) { plain_tag.html_safe }
    let(:open_name) { :tag_open }
    let(:close_name) { :tag_close }

    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