summaryrefslogtreecommitdiff
path: root/spec/helpers/appearances_helper_spec.rb
blob: 8673353996e127208bceca9fb7148b54db85f196 (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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe AppearancesHelper do
  let_it_be(:gitlab_logo) { ActionController::Base.helpers.image_path('logo.svg') }

  before do
    user = create(:user)
    allow(helper).to receive(:current_user).and_return(user)
  end

  describe '#appearance_short_name' do
    it 'returns the default value' do
      create(:appearance)

      expect(helper.appearance_short_name).to match('GitLab')
    end

    it 'returns the customized value' do
      create(:appearance, pwa_short_name: 'Short')

      expect(helper.appearance_short_name).to match('Short')
    end
  end

  describe '.current_appearance' do
    it 'memoizes empty appearance' do
      expect(Appearance).to receive(:current).once

      2.times { helper.current_appearance }
    end

    it 'memoizes custom appearance' do
      create(:appearance)

      expect(Appearance).to receive(:current).once.and_call_original

      2.times { helper.current_appearance }
    end
  end

  describe '#header_message' do
    it 'returns nil when header message field is not set' do
      create(:appearance)

      expect(helper.header_message).to be_nil
    end

    context 'when header message is set' do
      it 'includes current message' do
        message = "Foo bar"
        create(:appearance, header_message: message)

        expect(helper.header_message).to include(message)
      end
    end
  end

  describe '#footer_message' do
    it 'returns nil when footer message field is not set' do
      create(:appearance)

      expect(helper.footer_message).to be_nil
    end

    context 'when footer message is set' do
      it 'includes current message' do
        message = "Foo bar"
        create(:appearance, footer_message: message)

        expect(helper.footer_message).to include(message)
      end
    end
  end

  describe '#brand_image' do
    context 'when there is a logo' do
      let!(:appearance) { create(:appearance, :with_logo) }

      it 'returns a path' do
        expect(helper.brand_image).to match(%r(img .* data-src="/uploads/-/system/appearance/.*png))
      end

      context 'when there is no associated upload' do
        before do
          # Legacy attachments were not tracked in the uploads table
          appearance.logo.upload.destroy!
          appearance.reload
        end

        it 'falls back to using the original path' do
          expect(helper.brand_image).to match(%r(img .* data-src="/uploads/-/system/appearance/.*png))
        end
      end
    end

    context 'when there is no logo' do
      it 'returns path of GitLab logo' do
        expect(helper.brand_image).to match(%r(img .* data-src="#{gitlab_logo}))
      end
    end

    context 'when there is a title' do
      let!(:appearance) { create(:appearance, title: 'My title') }

      it 'returns the title' do
        expect(helper.brand_image).to match(%r(img alt="My title"))
      end
    end

    context 'when there is no title' do
      it 'returns the default title' do
        expect(helper.brand_image).to match(%r(img alt="GitLab))
      end
    end
  end

  describe '#brand_image_path' do
    context 'with a custom logo' do
      let!(:appearance) { create(:appearance, :with_logo) }

      it 'returns path of custom logo' do
        expect(helper.brand_image_path).to match(%r(/uploads/-/system/appearance/.*/dk.png))
      end
    end

    context 'with no custom logo' do
      it 'returns path of GitLab logo' do
        expect(helper.brand_image_path).to eq(gitlab_logo)
      end
    end
  end

  describe '#brand_header_logo' do
    let(:options) { {} }

    subject do
      helper.brand_header_logo(options)
    end

    context 'with header logo' do
      let!(:appearance) { create(:appearance, :with_header_logo) }

      it 'renders image tag' do
        expect(helper).to receive(:image_tag).with(appearance.header_logo_path, class: 'brand-header-logo')

        subject
      end
    end

    context 'with add_gitlab_white_text option' do
      let(:options) { { add_gitlab_white_text: true } }

      it 'renders shared/logo_with_white_text partial' do
        expect(helper).to receive(:render).with(partial: 'shared/logo_with_white_text', formats: :svg)

        subject
      end
    end

    context 'with add_gitlab_black_text option' do
      let(:options) { { add_gitlab_black_text: true } }

      it 'renders shared/logo_with_black_text partial' do
        expect(helper).to receive(:render).with(partial: 'shared/logo_with_black_text', formats: :svg)

        subject
      end
    end

    it 'renders shared/logo by default' do
      expect(helper).to receive(:render).with(partial: 'shared/logo', formats: :svg)

      subject
    end
  end

  describe '#brand_title' do
    it 'returns the default title when no appearance is present' do
      allow(helper).to receive(:current_appearance).and_return(nil)

      expect(helper.brand_title).to eq(helper.default_brand_title)
    end
  end

  describe '#default_brand_title' do
    it 'returns the default title' do
      edition = Gitlab.ee? ? 'Enterprise' : 'Community'
      expected_default_brand_title = "GitLab #{edition} Edition"

      expect(helper.default_brand_title).to eq _(expected_default_brand_title)
    end
  end
end