BigW Consortium Gitlab

preferences_controller_spec.rb 1.82 KB
Newer Older
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
require 'spec_helper'

describe Profiles::PreferencesController do
  let(:user) { create(:user) }

  before do
    sign_in(user)

    allow(subject).to receive(:current_user).and_return(user)
  end

  describe 'GET show' do
    it 'renders' do
      get :show
      expect(response).to render_template :show
    end

    it 'assigns user' do
      get :show
      expect(assigns[:user]).to eq user
    end
  end

  describe 'PATCH update' do
    def go(params: {}, format: :js)
      params.reverse_merge!(
        color_scheme_id: '1',
28
        dashboard: 'stars',
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
        theme_id: '1'
      )

      patch :update, user: params, format: format
    end

    context 'on successful update' do
      it 'sets the flash' do
        go
        expect(flash[:notice]).to eq 'Preferences saved.'
      end

      it "changes the user's preferences" do
        prefs = {
          color_scheme_id: '1',
44
          dashboard: 'stars',
45 46 47 48 49 50 51 52 53
          theme_id: '2'
        }.with_indifferent_access

        expect(user).to receive(:update_attributes).with(prefs)

        go params: prefs
      end
    end

54 55 56 57 58 59 60 61 62 63 64 65
    context 'on failed update' do
      it 'sets the flash' do
        expect(user).to receive(:update_attributes).and_return(false)

        go

        expect(flash[:alert]).to eq('Failed to save preferences.')
      end
    end

    context 'on invalid dashboard setting' do
      it 'sets the flash' do
66
        prefs = { dashboard: 'invalid' }
67 68 69 70 71

        go params: prefs

        expect(flash[:alert]).to match(/\AFailed to save preferences \(.+\)\.\z/)
      end
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
    end

    context 'as js' do
      it 'renders' do
        go
        expect(response).to render_template :update
      end
    end

    context 'as html' do
      it 'redirects' do
        go format: :html
        expect(response).to redirect_to(profile_preferences_path)
      end
    end
  end
end