BigW Consortium Gitlab

avatars_helper_spec.rb 4.39 KB
Newer Older
1 2 3
require 'rails_helper'

describe AvatarsHelper do
4 5
  include ApplicationHelper

6 7 8 9 10 11 12 13 14 15 16 17 18 19
  let(:user) { create(:user) }

  describe '#user_avatar' do
    subject { helper.user_avatar(user: user) }

    it "links to the user's profile" do
      is_expected.to include("href=\"#{user_path(user)}\"")
    end

    it "has the user's name as title" do
      is_expected.to include("title=\"#{user.name}\"")
    end

    it "contains the user's avatar image" do
20
      is_expected.to include(CGI.escapeHTML(user.avatar_url(size: 16)))
21 22
    end
  end
23 24 25 26 27 28

  describe '#user_avatar_without_link' do
    let(:options) { { user: user } }
    subject { helper.user_avatar_without_link(options) }

    it 'displays user avatar' do
29 30
      is_expected.to eq tag(
        :img,
31
        alt: "#{user.name}'s avatar",
32 33 34 35
        src: avatar_icon(user, 16),
        data: { container: 'body' },
        class: 'avatar s16 has-tooltip',
        title: user.name
36 37 38 39 40 41 42
      )
    end

    context 'with css_class parameter' do
      let(:options) { { user: user, css_class: '.cat-pics' } }

      it 'uses provided css_class' do
43 44
        is_expected.to eq tag(
          :img,
45
          alt: "#{user.name}'s avatar",
46 47 48 49
          src: avatar_icon(user, 16),
          data: { container: 'body' },
          class: "avatar s16 #{options[:css_class]} has-tooltip",
          title: user.name
50 51 52 53 54 55 56 57
        )
      end
    end

    context 'with size parameter' do
      let(:options) { { user: user, size: 99 } }

      it 'uses provided size' do
58 59
        is_expected.to eq tag(
          :img,
60
          alt: "#{user.name}'s avatar",
61 62 63 64
          src: avatar_icon(user, options[:size]),
          data: { container: 'body' },
          class: "avatar s#{options[:size]} has-tooltip",
          title: user.name
65 66 67 68 69 70 71 72
        )
      end
    end

    context 'with url parameter' do
      let(:options) { { user: user, url: '/over/the/rainbow.png' } }

      it 'uses provided url' do
73 74
        is_expected.to eq tag(
          :img,
75
          alt: "#{user.name}'s avatar",
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
          src: options[:url],
          data: { container: 'body' },
          class: "avatar s16 has-tooltip",
          title: user.name
        )
      end
    end

    context 'with lazy parameter' do
      let(:options) { { user: user, lazy: true } }

      it 'adds `lazy` class to class list, sets `data-src` with avatar URL and `src` with placeholder image' do
        is_expected.to eq tag(
          :img,
          alt: "#{user.name}'s avatar",
          src: LazyImageTagHelper.placeholder_image,
          data: { container: 'body', src: avatar_icon(user, 16) },
          class: "avatar s16 has-tooltip lazy",
          title: user.name
95 96 97 98
        )
      end
    end

99 100 101 102 103
    context 'with has_tooltip parameter' do
      context 'with has_tooltip set to true' do
        let(:options) { { user: user, has_tooltip: true } }

        it 'adds has-tooltip' do
104 105
          is_expected.to eq tag(
            :img,
106
            alt: "#{user.name}'s avatar",
107 108 109 110
            src: avatar_icon(user, 16),
            data: { container: 'body' },
            class: "avatar s16 has-tooltip",
            title: user.name
111 112 113 114 115 116 117 118
          )
        end
      end

      context 'with has_tooltip set to false' do
        let(:options) { { user: user, has_tooltip: false } }

        it 'does not add has-tooltip or data container' do
119 120
          is_expected.to eq tag(
            :img,
121
            alt: "#{user.name}'s avatar",
122 123 124
            src: avatar_icon(user, 16),
            class: "avatar s16",
            title: user.name
125 126 127 128 129
          )
        end
      end
    end

130 131 132 133 134 135 136
    context 'with user_name parameter' do
      let(:options) { { user_name: 'Tinky Winky', user_email: 'no@f.un' } }

      context 'with user parameter' do
        let(:options) { { user: user, user_name: 'Tinky Winky' } }

        it 'prefers user parameter' do
137 138
          is_expected.to eq tag(
            :img,
139
            alt: "#{user.name}'s avatar",
140 141 142 143
            src: avatar_icon(user, 16),
            data: { container: 'body' },
            class: "avatar s16 has-tooltip",
            title: user.name
144 145 146 147 148
          )
        end
      end

      it 'uses user_name and user_email parameter if user is not present' do
149 150
        is_expected.to eq tag(
          :img,
151
          alt: "#{options[:user_name]}'s avatar",
152 153 154 155
          src: avatar_icon(options[:user_email], 16),
          data: { container: 'body' },
          class: "avatar s16 has-tooltip",
          title: options[:user_name]
156 157 158 159
        )
      end
    end
  end
160
end