BigW Consortium Gitlab

branch_formatter_spec.rb 1.98 KB
Newer Older
1 2 3 4
require 'spec_helper'

describe Gitlab::GithubImport::BranchFormatter, lib: true do
  let(:project) { create(:project) }
5
  let(:commit) { create(:commit, project: project) }
6 7 8 9 10
  let(:repo) { double }
  let(:raw) do
    {
      ref: 'feature',
      repo: repo,
11
      sha: commit.id
12 13 14 15
    }
  end

  describe '#exists?' do
16
    it 'returns true when both branch, and commit exists' do
17 18 19 20 21 22 23 24 25 26
      branch = described_class.new(project, double(raw))

      expect(branch.exists?).to eq true
    end

    it 'returns false when branch does not exist' do
      branch = described_class.new(project, double(raw.merge(ref: 'removed-branch')))

      expect(branch.exists?).to eq false
    end
27 28 29 30 31 32

    it 'returns false when commit does not exist' do
      branch = described_class.new(project, double(raw.merge(sha: '2e5d3239642f9161dcbbc4b70a211a68e5e45e2b')))

      expect(branch.exists?).to eq false
    end
33 34 35 36 37 38 39 40 41 42
  end

  describe '#name' do
    it 'returns raw ref when branch exists' do
      branch = described_class.new(project, double(raw))

      expect(branch.name).to eq 'feature'
    end

    it 'returns formatted ref when branch does not exist' do
43
      branch = described_class.new(project, double(raw.merge(ref: 'removed-branch', sha: '2e5d3239642f9161dcbbc4b70a211a68e5e45e2b')))
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60

      expect(branch.name).to eq 'removed-branch-2e5d3239'
    end
  end

  describe '#repo' do
    it 'returns raw repo' do
      branch = described_class.new(project, double(raw))

      expect(branch.repo).to eq repo
    end
  end

  describe '#sha' do
    it 'returns raw sha' do
      branch = described_class.new(project, double(raw))

61
      expect(branch.sha).to eq commit.id
62 63
    end
  end
64 65

  describe '#valid?' do
66
    it 'returns true when raw repo is present' do
67 68 69 70 71
      branch = described_class.new(project, double(raw))

      expect(branch.valid?).to eq true
    end

72
    it 'returns false when raw repo is blank' do
73 74 75 76 77
      branch = described_class.new(project, double(raw.merge(repo: nil)))

      expect(branch.valid?).to eq false
    end
  end
78
end