icons_helper_spec.rb 6.93 KB
Newer Older
1 2 3
require 'spec_helper'

describe IconsHelper do
4 5
  let(:icons_path) { ActionController::Base.helpers.image_path("icons.svg") }

Clement Ho's avatar
Clement Ho committed
6 7 8
  describe 'icon' do
    it 'returns aria-hidden by default' do
      star = icon('star')
Clement Ho's avatar
Clement Ho committed
9

Clement Ho's avatar
Clement Ho committed
10 11 12 13 14
      expect(star['aria-hidden']).to eq 'aria-hidden'
    end

    it 'does not return aria-hidden if aria-label is set' do
      up = icon('up', 'aria-label' => 'up')
Clement Ho's avatar
Clement Ho committed
15 16

      expect(up['aria-hidden']).to be_nil
Clement Ho's avatar
Clement Ho committed
17 18 19 20
      expect(up['aria-label']).to eq 'aria-label'
    end
  end

Tim Zallmann's avatar
Tim Zallmann committed
21
  describe 'sprite_icon_path' do
22
    it 'returns relative path' do
23 24
      expect(sprite_icon_path)
        .to eq icons_path
25 26 27 28 29 30 31 32 33 34
    end

    context 'when an asset_host is set in the config it will return an absolute local URL' do
      let(:asset_host) { 'http://assets' }

      before do
        allow(ActionController::Base).to receive(:asset_host).and_return(asset_host)
      end

      it 'returns an absolute URL on that asset host' do
Tim Zallmann's avatar
Tim Zallmann committed
35
        expect(sprite_icon_path)
Tim Zallmann's avatar
Tim Zallmann committed
36
          .to eq ActionController::Base.helpers.image_path("icons.svg", host: Gitlab.config.gitlab.url)
37 38 39 40
      end
    end
  end

41 42 43 44 45
  describe 'sprite_icon' do
    icon_name = 'clock'

    it 'returns svg icon html' do
      expect(sprite_icon(icon_name).to_s)
46
        .to eq "<svg><use xlink:href=\"#{icons_path}##{icon_name}\"></use></svg>"
47 48 49 50
    end

    it 'returns svg icon html + size classes' do
      expect(sprite_icon(icon_name, size: 72).to_s)
51
        .to eq "<svg class=\"s72\"><use xlink:href=\"#{icons_path}##{icon_name}\"></use></svg>"
52 53 54 55
    end

    it 'returns svg icon html + size classes + additional class' do
      expect(sprite_icon(icon_name, size: 72, css_class: 'icon-danger').to_s)
56
        .to eq "<svg class=\"s72 icon-danger\"><use xlink:href=\"#{icons_path}##{icon_name}\"></use></svg>"
57
    end
Lukas Eipert's avatar
Lukas Eipert committed
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80

    describe 'non existing icon' do
      non_existing = 'non_existing_icon_sprite'

      it 'should raise in development mode' do
        allow(Rails.env).to receive(:development?).and_return(true)

        expect { sprite_icon(non_existing) }.to raise_error(ArgumentError, /is not a known icon/)
      end

      it 'should raise in test mode' do
        allow(Rails.env).to receive(:test?).and_return(true)

        expect { sprite_icon(non_existing) }.to raise_error(ArgumentError, /is not a known icon/)
      end

      it 'should not raise in production mode' do
        allow(Rails.env).to receive(:test?).and_return(false)
        allow(Rails.env).to receive(:development?).and_return(false)

        expect { sprite_icon(non_existing) }.not_to raise_error
      end
    end
81 82
  end

83 84 85 86 87
  describe 'audit icon' do
    it 'returns right icon name for standard auth' do
      icon_name = 'standard'
      expect(audit_icon(icon_name).to_s)
          .to eq '<i class="fa fa-key"></i>'
Jan Beckmann's avatar
Jan Beckmann committed
88
    end
89 90 91 92

    it 'returns right icon name for two-factor auth' do
      icon_name = 'two-factor'
      expect(audit_icon(icon_name).to_s)
Jan Beckmann's avatar
Jan Beckmann committed
93
          .to eq '<i class="fa fa-key"></i>'
94 95 96 97 98 99 100 101 102
    end

    it 'returns right icon name for google_oauth2 auth' do
      icon_name = 'google_oauth2'
      expect(audit_icon(icon_name).to_s)
          .to eq '<i class="fa fa-google"></i>'
    end
  end

103 104 105 106 107 108
  describe 'file_type_icon_class' do
    it 'returns folder class' do
      expect(file_type_icon_class('folder', 0, 'folder_name')).to eq 'folder'
    end

    it 'returns share class' do
109
      expect(file_type_icon_class('file', '120000', 'link')).to eq 'share'
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 196 197 198 199 200 201 202 203 204 205 206 207
    end

    it 'returns file-pdf-o class with .pdf' do
      expect(file_type_icon_class('file', 0, 'filename.pdf')).to eq 'file-pdf-o'
    end

    it 'returns file-image-o class with .jpg' do
      expect(file_type_icon_class('file', 0, 'filename.jpg')).to eq 'file-image-o'
    end

    it 'returns file-image-o class with .JPG' do
      expect(file_type_icon_class('file', 0, 'filename.JPG')).to eq 'file-image-o'
    end

    it 'returns file-image-o class with .png' do
      expect(file_type_icon_class('file', 0, 'filename.png')).to eq 'file-image-o'
    end

    it 'returns file-archive-o class with .tar' do
      expect(file_type_icon_class('file', 0, 'filename.tar')).to eq 'file-archive-o'
    end

    it 'returns file-archive-o class with .TAR' do
      expect(file_type_icon_class('file', 0, 'filename.TAR')).to eq 'file-archive-o'
    end

    it 'returns file-archive-o class with .tar.gz' do
      expect(file_type_icon_class('file', 0, 'filename.tar.gz')).to eq 'file-archive-o'
    end

    it 'returns file-audio-o class with .mp3' do
      expect(file_type_icon_class('file', 0, 'filename.mp3')).to eq 'file-audio-o'
    end

    it 'returns file-audio-o class with .MP3' do
      expect(file_type_icon_class('file', 0, 'filename.MP3')).to eq 'file-audio-o'
    end

    it 'returns file-audio-o class with .wav' do
      expect(file_type_icon_class('file', 0, 'filename.wav')).to eq 'file-audio-o'
    end

    it 'returns file-video-o class with .avi' do
      expect(file_type_icon_class('file', 0, 'filename.avi')).to eq 'file-video-o'
    end

    it 'returns file-video-o class with .AVI' do
      expect(file_type_icon_class('file', 0, 'filename.AVI')).to eq 'file-video-o'
    end

    it 'returns file-video-o class with .mp4' do
      expect(file_type_icon_class('file', 0, 'filename.mp4')).to eq 'file-video-o'
    end

    it 'returns file-word-o class with .doc' do
      expect(file_type_icon_class('file', 0, 'filename.doc')).to eq 'file-word-o'
    end

    it 'returns file-word-o class with .DOC' do
      expect(file_type_icon_class('file', 0, 'filename.DOC')).to eq 'file-word-o'
    end

    it 'returns file-word-o class with .docx' do
      expect(file_type_icon_class('file', 0, 'filename.docx')).to eq 'file-word-o'
    end

    it 'returns file-excel-o class with .xls' do
      expect(file_type_icon_class('file', 0, 'filename.xls')).to eq 'file-excel-o'
    end

    it 'returns file-excel-o class with .XLS' do
      expect(file_type_icon_class('file', 0, 'filename.XLS')).to eq 'file-excel-o'
    end

    it 'returns file-excel-o class with .xlsx' do
      expect(file_type_icon_class('file', 0, 'filename.xlsx')).to eq 'file-excel-o'
    end

    it 'returns file-excel-o class with .ppt' do
      expect(file_type_icon_class('file', 0, 'filename.ppt')).to eq 'file-powerpoint-o'
    end

    it 'returns file-excel-o class with .PPT' do
      expect(file_type_icon_class('file', 0, 'filename.PPT')).to eq 'file-powerpoint-o'
    end

    it 'returns file-excel-o class with .pptx' do
      expect(file_type_icon_class('file', 0, 'filename.pptx')).to eq 'file-powerpoint-o'
    end

    it 'returns file-text-o class with .unknow' do
      expect(file_type_icon_class('file', 0, 'filename.unknow')).to eq 'file-text-o'
    end

    it 'returns file-text-o class with no extension' do
      expect(file_type_icon_class('file', 0, 'CHANGELOG')).to eq 'file-text-o'
    end
  end
haseeb's avatar
haseeb committed
208 209 210 211

  describe '#external_snippet_icon' do
    it 'returns external snippet icon' do
      expect(external_snippet_icon('download').to_s)
haseeb's avatar
haseeb committed
212
        .to eq("<span class=\"gl-snippet-icon gl-snippet-icon-download\"></span>")
haseeb's avatar
haseeb committed
213 214
    end
  end
215
end