page_layout_helper_spec.rb 3.7 KB
Newer Older
R
Robert Speicher 已提交
1 2 3 4
require 'rails_helper'

describe PageLayoutHelper do
  describe 'page_description' do
5 6
    it 'defaults to nil' do
      expect(helper.page_description).to eq nil
R
Robert Speicher 已提交
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
    end

    it 'returns the last-pushed description' do
      helper.page_description('Foo')
      helper.page_description('Bar')
      helper.page_description('Baz')

      expect(helper.page_description).to eq 'Baz'
    end

    it 'squishes multiple newlines' do
      helper.page_description("Foo\nBar\nBaz")

      expect(helper.page_description).to eq 'Foo Bar Baz'
    end

23 24 25 26 27 28 29 30 31 32 33 34 35
    it 'truncates' do
      helper.page_description <<-LOREM.strip_heredoc
        Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo
        ligula eget dolor. Aenean massa. Cum sociis natoque penatibus et magnis
        dis parturient montes, nascetur ridiculus mus. Donec quam felis,
        ultricies nec, pellentesque eu, pretium quis, sem. Nulla consequat massa
        quis enim. Donec pede justo, fringilla vel, aliquet nec, vulputate eget,
        arcu.
      LOREM

      expect(helper.page_description).to end_with 'quam felis,...'
    end

R
Robert Speicher 已提交
36 37 38 39 40
    it 'sanitizes all HTML' do
      helper.page_description("<b>Bold</b> <h1>Header</h1>")

      expect(helper.page_description).to eq 'Bold Header'
    end
41 42 43 44 45 46 47 48

    it 'truncates before sanitizing' do
      helper.page_description('<b>Bold</b> <img> <img> <img> <h1>Header</h1> ' * 10)

      # 12 words because <img> was counted as a word
      expect(helper.page_description)
        .to eq('Bold    Header Bold    Header Bold    Header Bold    Header Bold    Header Bold    Header...')
    end
R
Robert Speicher 已提交
49 50 51 52
  end

  describe 'page_image' do
    it 'defaults to the GitLab logo' do
53
      expect(helper.page_image).to match_asset_path 'assets/gitlab_logo.png'
R
Robert Speicher 已提交
54 55
    end

56 57 58
    %w(project user group).each do |type|
      context "with @#{type} assigned" do
        it "uses #{type.titlecase} avatar if available" do
59
          object = double(avatar_url: 'http://example.com/uploads/-/system/avatar.png')
60
          assign(type, object)
R
Robert Speicher 已提交
61

62 63
          expect(helper.page_image).to eq object.avatar_url
        end
R
Robert Speicher 已提交
64

65 66 67
        it 'falls back to the default when avatar_url is nil' do
          object = double(avatar_url: nil)
          assign(type, object)
R
Robert Speicher 已提交
68

69
          expect(helper.page_image).to match_asset_path 'assets/gitlab_logo.png'
70
        end
R
Robert Speicher 已提交
71
      end
72

73 74
      context "with no assignments" do
        it 'falls back to the default' do
75
          expect(helper.page_image).to match_asset_path 'assets/gitlab_logo.png'
76
        end
77 78
      end
    end
R
Robert Speicher 已提交
79
  end
80 81 82 83 84

  describe 'page_card_attributes' do
    it 'raises ArgumentError when given more than two attributes' do
      map = { foo: 'foo', bar: 'bar', baz: 'baz' }

85 86
      expect { helper.page_card_attributes(map) }
        .to raise_error(ArgumentError, /more than two attributes/)
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
    end

    it 'rejects blank values' do
      map = { foo: 'foo', bar: '' }
      helper.page_card_attributes(map)

      expect(helper.page_card_attributes).to eq({ foo: 'foo' })
    end
  end

  describe 'page_card_meta_tags' do
    it 'returns the twitter:label and twitter:data tags' do
      allow(helper).to receive(:page_card_attributes).and_return(foo: 'bar')

      tags = helper.page_card_meta_tags

      aggregate_failures do
        expect(tags).to include %q(<meta property="twitter:label1" content="foo" />)
        expect(tags).to include %q(<meta property="twitter:data1" content="bar" />)
      end
    end
R
Robert Speicher 已提交
108 109 110 111 112 113 114 115 116

    it 'escapes content' do
      allow(helper).to receive(:page_card_attributes)
        .and_return(foo: %q{foo" http-equiv="refresh}.html_safe)

      tags = helper.page_card_meta_tags

      expect(tags).to include(%q{content="foo&quot; http-equiv=&quot;refresh"})
    end
117
  end
R
Robert Speicher 已提交
118
end