sorting_list_spec.rb 7.6 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 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 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 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226
# frozen_string_literal: true
require 'spec_helper'

describe 'Sort Issuable List' do
  let(:project) { create(:project, :public) }

  let(:first_created_issuable) { issuables.order_created_asc.first }
  let(:last_created_issuable) { issuables.order_created_desc.first }

  let(:first_updated_issuable) { issuables.order_updated_asc.first }
  let(:last_updated_issuable) { issuables.order_updated_desc.first }

  context 'for merge requests' do
    include MergeRequestHelpers

    let!(:issuables) do
      timestamps = [{ created_at: 3.minutes.ago, updated_at: 20.seconds.ago },
                    { created_at: 2.minutes.ago, updated_at: 30.seconds.ago },
                    { created_at: 4.minutes.ago, updated_at: 10.seconds.ago }]

      timestamps.each_with_index do |ts, i|
        create issuable_type, { title: "#{issuable_type}_#{i}",
                                source_branch: "#{issuable_type}_#{i}",
                                source_project: project }.merge(ts)
      end

      MergeRequest.all
    end

    context 'default sort order' do
      context 'in the "merge requests" tab', :js do
        let(:issuable_type) { :merge_request }

        it 'is "last created"' do
          visit_merge_requests project

          expect(first_merge_request).to include(last_created_issuable.title)
          expect(last_merge_request).to include(first_created_issuable.title)
        end
      end

      context 'in the "merge requests / open" tab', :js do
        let(:issuable_type) { :merge_request }

        it 'is "created date"' do
          visit_merge_requests_with_state(project, 'open')

          expect(selected_sort_order).to eq('created date')
          expect(first_merge_request).to include(last_created_issuable.title)
          expect(last_merge_request).to include(first_created_issuable.title)
        end
      end

      context 'in the "merge requests / merged" tab', :js do
        let(:issuable_type) { :merged_merge_request }

        it 'is "last updated"' do
          visit_merge_requests_with_state(project, 'merged')

          expect(find('.issues-other-filters')).to have_content('Last updated')
          expect(first_merge_request).to include(last_updated_issuable.title)
          expect(last_merge_request).to include(first_updated_issuable.title)
        end
      end

      context 'in the "merge requests / closed" tab', :js do
        let(:issuable_type) { :closed_merge_request }

        it 'is "last updated"' do
          visit_merge_requests_with_state(project, 'closed')

          expect(find('.issues-other-filters')).to have_content('Last updated')
          expect(first_merge_request).to include(last_updated_issuable.title)
          expect(last_merge_request).to include(first_updated_issuable.title)
        end
      end

      context 'in the "merge requests / all" tab', :js do
        let(:issuable_type) { :merge_request }

        it 'is "created date"' do
          visit_merge_requests_with_state(project, 'all')

          expect(find('.issues-other-filters')).to have_content('Created date')
          expect(first_merge_request).to include(last_created_issuable.title)
          expect(last_merge_request).to include(first_created_issuable.title)
        end
      end

      context 'custom sorting' do
        let(:issuable_type) { :merge_request }

        it 'supports sorting in asc and desc order' do
          visit_merge_requests_with_state(project, 'open')

          page.within('.issues-other-filters') do
            click_button('Created date')
            click_link('Last updated')
          end

          expect(first_merge_request).to include(last_updated_issuable.title)
          expect(last_merge_request).to include(first_updated_issuable.title)

          find('.issues-other-filters .filter-dropdown-container .qa-reverse-sort').click

          expect(first_merge_request).to include(first_updated_issuable.title)
          expect(last_merge_request).to include(last_updated_issuable.title)
        end
      end
    end
  end

  context 'for issues' do
    include IssueHelpers

    let!(:issuables) do
      timestamps = [{ created_at: 3.minutes.ago, updated_at: 20.seconds.ago },
                    { created_at: 2.minutes.ago, updated_at: 30.seconds.ago },
                    { created_at: 4.minutes.ago, updated_at: 10.seconds.ago }]

      timestamps.each_with_index do |ts, i|
        create issuable_type, { title: "#{issuable_type}_#{i}",
                                project: project }.merge(ts)
      end

      Issue.all
    end

    context 'default sort order' do
      context 'in the "issues" tab', :js do
        let(:issuable_type) { :issue }

        it 'is "created date"' do
          visit_issues project

          expect(find('.issues-other-filters')).to have_content('Created date')
          expect(first_issue).to include(last_created_issuable.title)
          expect(last_issue).to include(first_created_issuable.title)
        end
      end

      context 'in the "issues / open" tab', :js do
        let(:issuable_type) { :issue }

        it 'is "created date"' do
          visit_issues_with_state(project, 'open')

          expect(find('.issues-other-filters')).to have_content('Created date')
          expect(first_issue).to include(last_created_issuable.title)
          expect(last_issue).to include(first_created_issuable.title)
        end
      end

      context 'in the "issues / closed" tab', :js do
        let(:issuable_type) { :closed_issue }

        it 'is "last updated"' do
          visit_issues_with_state(project, 'closed')

          expect(find('.issues-other-filters')).to have_content('Last updated')
          expect(first_issue).to include(last_updated_issuable.title)
          expect(last_issue).to include(first_updated_issuable.title)
        end
      end

      context 'in the "issues / all" tab', :js do
        let(:issuable_type) { :issue }

        it 'is "created date"' do
          visit_issues_with_state(project, 'all')

          expect(find('.issues-other-filters')).to have_content('Created date')
          expect(first_issue).to include(last_created_issuable.title)
          expect(last_issue).to include(first_created_issuable.title)
        end
      end

      context 'when the sort in the URL is id_desc' do
        let(:issuable_type) { :issue }

        before do
          visit_issues(project, sort: 'id_desc')
        end

        it 'shows the sort order as created date' do
          expect(find('.issues-other-filters')).to have_content('Created date')
          expect(first_issue).to include(last_created_issuable.title)
          expect(last_issue).to include(first_created_issuable.title)
        end
      end
    end

    context 'custom sorting' do
      let(:issuable_type) { :issue }

      it 'supports sorting in asc and desc order' do
        visit_issues_with_state(project, 'open')

        page.within('.issues-other-filters') do
          click_button('Created date')
          click_link('Last updated')
        end

        expect(first_issue).to include(last_updated_issuable.title)
        expect(last_issue).to include(first_updated_issuable.title)

        find('.issues-other-filters .filter-dropdown-container .qa-reverse-sort').click

        expect(first_issue).to include(first_updated_issuable.title)
        expect(last_issue).to include(last_updated_issuable.title)
      end
    end
  end

  def selected_sort_order
    find('.filter-dropdown-container .dropdown button').text.downcase
  end

  def visit_merge_requests_with_state(project, state)
    visit_merge_requests project, state: state
  end

  def visit_issues_with_state(project, state)
    visit_issues project, state: state
  end
end