preferences_helper_spec.rb 3.09 KB
Newer Older
1 2 3
require 'spec_helper'

describe PreferencesHelper do
4 5 6 7 8 9 10
  describe 'dashboard_choices' do
    it 'raises an exception when defined choices may be missing' do
      expect(User).to receive(:dashboards).and_return(foo: 'foo')
      expect { helper.dashboard_choices }.to raise_error(RuntimeError)
    end

    it 'raises an exception when defined choices may be using the wrong key' do
11 12 13
      dashboards = User.dashboards.dup
      dashboards[:projects_changed] = dashboards.delete :projects
      expect(User).to receive(:dashboards).and_return(dashboards)
14 15 16 17 18 19
      expect { helper.dashboard_choices }.to raise_error(KeyError)
    end

    it 'provides better option descriptions' do
      expect(helper.dashboard_choices).to match_array [
        ['Your Projects (default)', 'projects'],
20 21
        ['Starred Projects',        'stars'],
        ["Your Projects' Activity", 'project_activity'],
22 23 24
        ["Starred Projects' Activity", 'starred_project_activity'],
        ["Your Groups", 'groups'],
        ["Your Todos", 'todos']
25 26 27 28
      ]
    end
  end

29 30 31
  describe 'user_application_theme' do
    context 'with a user' do
      it "returns user's theme's css_class" do
32 33 34
        stub_user(theme_id: 3)

        expect(helper.user_application_theme).to eq 'ui_green'
35 36 37
      end

      it 'returns the default when id is invalid' do
38
        stub_user(theme_id: Gitlab::Themes.count + 5)
39 40 41

        allow(Gitlab.config.gitlab).to receive(:default_theme).and_return(2)

42
        expect(helper.user_application_theme).to eq 'ui_charcoal'
43 44 45 46 47
      end
    end

    context 'without a user' do
      it 'returns the default theme' do
48 49 50
        stub_user

        expect(helper.user_application_theme).to eq Gitlab::Themes.default.css_class
51 52 53 54
      end
    end
  end

55 56 57 58 59 60 61 62 63 64 65 66 67
  describe 'user_color_scheme' do
    context 'with a user' do
      it "returns user's scheme's css_class" do
        allow(helper).to receive(:current_user).
          and_return(double(color_scheme_id: 3))

        expect(helper.user_color_scheme).to eq 'solarized-light'
      end

      it 'returns the default when id is invalid' do
        allow(helper).to receive(:current_user).
          and_return(double(color_scheme_id: Gitlab::ColorSchemes.count + 5))
      end
68
    end
69

70 71 72 73 74 75 76
    context 'without a user' do
      it 'returns the default theme' do
        stub_user

        expect(helper.user_color_scheme).
          to eq Gitlab::ColorSchemes.default.css_class
      end
77
    end
78
  end
79

80 81 82 83 84 85
  def stub_user(messages = {})
    if messages.empty?
      allow(helper).to receive(:current_user).and_return(nil)
    else
      allow(helper).to receive(:current_user).
        and_return(double('user', messages))
86 87
    end
  end
88 89 90

  describe 'default_project_view' do
    context 'user not signed in' do
91 92 93 94
      before do
        @project = create(:project)
        stub_user
      end
95 96 97 98 99 100

      it 'returns readme view if repository is not empty' do
        expect(helper.default_project_view).to eq('readme')
      end

      it 'returns activity if repository is empty' do
101
        expect(@project).to receive(:empty_repo?).and_return(true)
102

103
        expect(helper.default_project_view).to eq('empty')
104 105 106
      end
    end
  end
107
end