Welcome to mirror list, hosted at ThFree Co, Russian Federation.

sorting_preference_spec.rb « concerns « controllers « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 82a920215caa68eeff4890ceab199fe8d54cd193 (plain)
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe SortingPreference do
  let(:user) { create(:user) }
  let(:params) { {} }

  let(:controller_class) do
    Class.new do
      def self.helper_method(name); end

      include SortingPreference
      include SortingHelper
    end
  end

  let(:controller) { controller_class.new }

  before do
    allow(controller).to receive(:params).and_return(ActionController::Parameters.new(params))
    allow(controller).to receive(:current_user).and_return(user)
    allow(controller).to receive(:legacy_sort_cookie_name).and_return('issuable_sort')
    allow(controller).to receive(:sorting_field).and_return(:issues_sort)
  end

  describe '#set_sort_order' do
    let(:group) { build(:group) }
    let(:issue_weights_available) { true }

    before do
      allow(controller).to receive(:default_sort_order).and_return('updated_desc')
      allow(controller).to receive(:action_name).and_return('issues')
      allow(controller).to receive(:can_sort_by_issue_weight?).and_return(issue_weights_available)
      user.user_preference.update!(issues_sort: sorting_field)
    end

    subject { controller.send(:set_sort_order) }

    context 'when user preference contains allowed sorting' do
      let(:sorting_field) { 'updated_asc' }

      it 'sets sort order from user_preference' do
        is_expected.to eq('updated_asc')
      end
    end

    context 'when user preference contains weight sorting' do
      let(:sorting_field) { 'weight_desc' }

      context 'when user can sort by issue weight' do
        it 'sets sort order from user_preference' do
          is_expected.to eq('weight_desc')
        end
      end

      context 'when user cannot sort by issue weight' do
        let(:issue_weights_available) { false }

        it 'sets default sort order' do
          is_expected.to eq('updated_desc')
        end
      end
    end
  end

  describe '#set_sort_order_from_user_preference' do
    subject { controller.send(:set_sort_order_from_user_preference) }

    context 'when sort param given' do
      let(:params) { { sort: 'updated_desc' } }

      context 'when sorting_field is defined' do
        it 'sets user_preference with the right value' do
          is_expected.to eq('updated_desc')
        end
      end

      context 'when no sorting_field is defined on the controller' do
        before do
          allow(controller).to receive(:sorting_field).and_return(nil)
        end

        it 'does not touch user_preference' do
          expect(user).not_to receive(:user_preference)

          subject
        end
      end
    end

    context 'when a user sorting preference exists' do
      before do
        user.user_preference.update!(issues_sort: 'updated_asc')
      end

      it 'returns the set preference' do
        is_expected.to eq('updated_asc')
      end
    end
  end

  describe '#set_set_order_from_cookie' do
    subject { controller.send(:set_sort_order_from_cookie) }

    before do
      allow(controller).to receive(:cookies).and_return(cookies)
    end

    context 'when sort param given' do
      let(:cookies) { {} }
      let(:params) { { sort: 'downvotes_asc' } }

      it 'sets the cookie with the right values and flags' do
        subject

        expect(cookies['issue_sort']).to eq(expires: nil, value: 'popularity', secure: false, httponly: false)
      end
    end

    context 'when cookie exists' do
      let(:cookies) { { 'issue_sort' => 'id_asc' } }

      it 'sets the cookie with the right values and flags' do
        subject

        expect(cookies['issue_sort']).to eq(expires: nil, value: 'created_asc', secure: false, httponly: false)
      end
    end
  end
end