summaryrefslogtreecommitdiff
path: root/spec/finders/branches_finder_spec.rb
blob: 5f75ff8c6ff9ce9995b738eface15546f521bd10 (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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# frozen_string_literal: true

require 'spec_helper'

describe BranchesFinder do
  let(:user) { create(:user) }
  let(:project) { create(:project, :repository) }
  let(:repository) { project.repository }

  describe '#execute' do
    context 'sort only' do
      it 'sorts by name' do
        branches_finder = described_class.new(repository, {})

        result = branches_finder.execute

        expect(result.first.name).to eq("'test'")
      end

      it 'sorts by recently_updated' do
        branches_finder = described_class.new(repository, { sort: 'updated_desc' })

        result = branches_finder.execute

        recently_updated_branch = repository.branches.max do |a, b|
          repository.commit(a.dereferenced_target).committed_date <=> repository.commit(b.dereferenced_target).committed_date
        end

        expect(result.first.name).to eq(recently_updated_branch.name)
      end

      it 'sorts by last_updated' do
        branches_finder = described_class.new(repository, { sort: 'updated_asc' })

        result = branches_finder.execute

        expect(result.first.name).to eq('feature')
      end
    end

    context 'filter only' do
      it 'filters branches by name' do
        branches_finder = described_class.new(repository, { search: 'fix' })

        result = branches_finder.execute

        expect(result.first.name).to eq('fix')
        expect(result.count).to eq(1)
      end

      it 'filters branches by name ignoring letter case' do
        branches_finder = described_class.new(repository, { search: 'FiX' })

        result = branches_finder.execute

        expect(result.first.name).to eq('fix')
        expect(result.count).to eq(1)
      end

      it 'does not find any branch with that name' do
        branches_finder = described_class.new(repository, { search: 'random' })

        result = branches_finder.execute

        expect(result.count).to eq(0)
      end

      it 'filters branches by provided names' do
        branches_finder = described_class.new(repository, { names: %w[fix csv lfs does-not-exist] })

        result = branches_finder.execute

        expect(result.count).to eq(3)
        expect(result.map(&:name)).to eq(%w{csv fix lfs})
      end

      it 'filters branches by name that begins with' do
        params = { search: '^feature_' }
        branches_finder = described_class.new(repository, params)

        result = branches_finder.execute

        expect(result.first.name).to eq('feature_conflict')
        expect(result.count).to eq(1)
      end

      it 'filters branches by name that ends with' do
        params = { search: 'feature$' }
        branches_finder = described_class.new(repository, params)

        result = branches_finder.execute

        expect(result.first.name).to eq('feature')
        expect(result.count).to eq(1)
      end

      it 'filters branches by nonexistent name that begins with' do
        params = { search: '^nope' }
        branches_finder = described_class.new(repository, params)

        result = branches_finder.execute

        expect(result.count).to eq(0)
      end

      it 'filters branches by nonexistent name that ends with' do
        params = { search: 'nope$' }
        branches_finder = described_class.new(repository, params)

        result = branches_finder.execute

        expect(result.count).to eq(0)
      end
    end

    context 'filter and sort' do
      it 'filters branches by name and sorts by recently_updated' do
        params = { sort: 'updated_desc', search: 'feat' }
        branches_finder = described_class.new(repository, params)

        result = branches_finder.execute

        expect(result.first.name).to eq('feature_conflict')
        expect(result.count).to eq(2)
      end

      it 'filters branches by name and sorts by recently_updated, with exact matches first' do
        params = { sort: 'updated_desc', search: 'feature' }
        branches_finder = described_class.new(repository, params)

        result = branches_finder.execute

        expect(result.first.name).to eq('feature')
        expect(result.second.name).to eq('feature_conflict')
        expect(result.count).to eq(2)
      end

      it 'filters branches by name and sorts by last_updated' do
        params = { sort: 'updated_asc', search: 'feature' }
        branches_finder = described_class.new(repository, params)

        result = branches_finder.execute

        expect(result.first.name).to eq('feature')
        expect(result.count).to eq(2)
      end
    end
  end
end