BigW Consortium Gitlab

branches_spec.rb 3.25 KB
Newer Older
1 2
require 'spec_helper'

3
describe 'Branches' do
4
  let(:user) { create(:user) }
5
  let(:project) { create(:project, :public, :repository) }
6 7
  let(:repository) { project.repository }

8
  context 'logged in as developer' do
9
    before do
10 11
      sign_in(user)
      project.team << [user, :developer]
12
    end
13

14
    describe 'Initial branches page' do
15
      it 'shows all the branches sorted by last updated by default' do
16
        visit project_branches_path(project)
17

18
        expect(page).to have_content(sorted_branches(repository, count: 20, sort_by: :updated_desc))
19
      end
20

21
      it 'sorts the branches by name' do
22
        visit project_branches_path(project)
23

24
        click_button "Last updated" # Open sorting dropdown
25 26
        click_link "Name"

27
        expect(page).to have_content(sorted_branches(repository, count: 20, sort_by: :name))
28 29 30
      end

      it 'sorts the branches by oldest updated' do
31
        visit project_branches_path(project)
32

33
        click_button "Last updated" # Open sorting dropdown
34 35
        click_link "Oldest updated"

36
        expect(page).to have_content(sorted_branches(repository, count: 20, sort_by: :updated_asc))
37
      end
38 39

      it 'avoids a N+1 query in branches index' do
40
        control_count = ActiveRecord::QueryRecorder.new { visit project_branches_path(project) }.count
41

42
        %w(one two three four five).each { |ref| repository.add_branch(user, ref, 'master') }
43

44
        expect { visit project_branches_path(project) }.not_to exceed_query_limit(control_count)
45
      end
46 47 48
    end

    describe 'Find branches' do
49
      it 'shows filtered branches', :js do
50
        visit project_branches_path(project)
51 52 53 54 55 56 57

        fill_in 'branch-search', with: 'fix'
        find('#branch-search').native.send_keys(:enter)

        expect(page).to have_content('fix')
        expect(find('.all-branches')).to have_selector('li', count: 1)
      end
58
    end
59 60

    describe 'Delete unprotected branch' do
61
      it 'removes branch after confirmation', :js do
62
        visit project_branches_path(project)
63 64 65 66 67 68 69

        fill_in 'branch-search', with: 'fix'

        find('#branch-search').native.send_keys(:enter)

        expect(page).to have_content('fix')
        expect(find('.all-branches')).to have_selector('li', count: 1)
70
        accept_confirm { find('.js-branch-fix .btn-remove').click }
71 72 73 74 75 76 77 78 79

        expect(page).not_to have_content('fix')
        expect(find('.all-branches')).to have_selector('li', count: 0)
      end
    end
  end

  context 'logged in as master' do
    before do
80 81
      sign_in(user)
      project.team << [user, :master]
82 83
    end

84 85 86 87 88 89 90
    describe 'Initial branches page' do
      it 'shows description for admin' do
        visit project_branches_path(project)

        expect(page).to have_content("Protected branches can be managed in project settings")
      end
    end
91 92
  end

93 94
  context 'logged out' do
    before do
95
      visit project_branches_path(project)
96
    end
97

98 99 100 101
    it 'does not show merge request button' do
      page.within first('.all-branches li') do
        expect(page).not_to have_content 'Merge Request'
      end
102 103
    end
  end
104 105 106 107 108 109 110 111 112

  def sorted_branches(repository, count:, sort_by:)
    sorted_branches =
      repository.branches_sorted_by(sort_by).first(count).map do |branch|
        Regexp.escape(branch.name)
      end

    Regexp.new(sorted_branches.join('.*'))
  end
113
end