BigW Consortium Gitlab

commit_range_parser_spec.rb 3.47 KB
Newer Older
1 2 3 4 5 6 7 8 9 10
require 'spec_helper'

describe Banzai::ReferenceParser::CommitRangeParser, lib: true do
  include ReferenceParserHelpers

  let(:project) { create(:empty_project, :public) }
  let(:user) { create(:user) }
  subject { described_class.new(project, user) }
  let(:link) { empty_html_link }

11 12 13 14 15 16 17 18
  describe '#nodes_visible_to_user' do
    context 'when the link has a data-issue attribute' do
      before { link['data-commit-range'] = '123..456' }

      it_behaves_like "referenced feature visibility", "repository"
    end
  end

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
  describe '#referenced_by' do
    context 'when the link has a data-project attribute' do
      before do
        link['data-project'] = project.id.to_s
      end

      context 'when the link as a data-commit-range attribute' do
        before do
          link['data-commit-range'] = '123..456'
        end

        it 'returns an Array of commit ranges' do
          range = double(:range)

          expect(subject).to receive(:find_object).
            with(project, '123..456').
            and_return(range)

          expect(subject.referenced_by([link])).to eq([range])
        end

        it 'returns an empty Array when the commit range could not be found' do
          expect(subject).to receive(:find_object).
            with(project, '123..456').
            and_return(nil)

          expect(subject.referenced_by([link])).to eq([])
        end
      end

      context 'when the link does not have a data-commit-range attribute' do
        it 'returns an empty Array' do
          expect(subject.referenced_by([link])).to eq([])
        end
      end
    end

    context 'when the link does not have a data-project attribute' do
      it 'returns an empty Array' do
        expect(subject.referenced_by([link])).to eq([])
      end
    end
  end

  describe '#commit_range_ids_per_project' do
    before do
      link['data-project'] = project.id.to_s
    end

    it 'returns a Hash containing range IDs per project' do
      link['data-commit-range'] = '123..456'

      hash = subject.commit_range_ids_per_project([link])

      expect(hash).to be_an_instance_of(Hash)

      expect(hash[project.id].to_a).to eq(['123..456'])
    end

    it 'does not add a project when the data-commit-range attribute is empty' do
      hash = subject.commit_range_ids_per_project([link])

      expect(hash).to be_empty
    end
  end

  describe '#find_ranges' do
    it 'returns an Array of range objects' do
      range = double(:commit)

      expect(subject).to receive(:find_object).
        with(project, '123..456').
        and_return(range)

      expect(subject.find_ranges(project, ['123..456'])).to eq([range])
    end

    it 'skips ranges that could not be found' do
      expect(subject).to receive(:find_object).
        with(project, '123..456').
        and_return(nil)

      expect(subject.find_ranges(project, ['123..456'])).to eq([])
    end
  end

  describe '#find_object' do
    let(:range) { double(:range) }

    before do
      expect(CommitRange).to receive(:new).and_return(range)
    end

    context 'when the range has valid commits' do
      it 'returns the commit range' do
        expect(range).to receive(:valid_commits?).and_return(true)

        expect(subject.find_object(project, '123..456')).to eq(range)
      end
    end

    context 'when the range does not have any valid commits' do
      it 'returns nil' do
        expect(range).to receive(:valid_commits?).and_return(false)

        expect(subject.find_object(project, '123..456')).to be_nil
      end
    end
  end
end