BigW Consortium Gitlab

factory_spec.rb 3.9 KB
Newer Older
1 2 3
require 'spec_helper'

describe Gitlab::Ci::Status::Factory do
4
  let(:user) { create(:user) }
5
  let(:fabricated_status) { factory.fabricate! }
6
  let(:factory) { described_class.new(resource, user) }
7 8

  context 'when object has a core status' do
9 10 11
    HasStatus::AVAILABLE_STATUSES.each do |simple_status|
      context "when simple core status is #{simple_status}" do
        let(:resource) { double('resource', status: simple_status) }
12

13
        let(:expected_status) do
14
          Gitlab::Ci::Status.const_get(simple_status.capitalize)
15 16 17 18 19 20 21 22 23 24 25 26
        end

        it "fabricates a core status #{simple_status}" do
          expect(fabricated_status).to be_a expected_status
        end

        it "matches a valid core status for #{simple_status}" do
          expect(factory.core_status).to be_a expected_status
        end

        it "does not match any extended statuses for #{simple_status}" do
          expect(factory.extended_statuses).to be_empty
27 28 29 30
        end
      end
    end
  end
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

  context 'when resource supports multiple extended statuses' do
    let(:resource) { double('resource', status: :success) }

    let(:first_extended_status) do
      Class.new(SimpleDelegator) do
        def first_method
          'first return value'
        end

        def second_method
          'second return value'
        end

        def self.matches?(*)
          true
        end
      end
    end

    let(:second_extended_status) do
      Class.new(SimpleDelegator) do
        def first_method
          'decorated return value'
        end

        def third_method
          'third return value'
        end

        def self.matches?(*)
          true
        end
      end
    end

    shared_examples 'compound decorator factory' do
      it 'fabricates compound decorator' do
69 70 71
        expect(fabricated_status.first_method).to eq 'decorated return value'
        expect(fabricated_status.second_method).to eq 'second return value'
        expect(fabricated_status.third_method).to eq 'third return value'
72 73 74
      end

      it 'delegates to core status' do
75
        expect(fabricated_status.text).to eq 'passed'
76 77 78
      end

      it 'latest matches status becomes a status name' do
79 80 81 82 83 84 85 86 87 88
        expect(fabricated_status.class).to eq second_extended_status
      end

      it 'matches correct core status' do
        expect(factory.core_status).to be_a Gitlab::Ci::Status::Success
      end

      it 'matches correct extended statuses' do
        expect(factory.extended_statuses)
          .to eq [first_extended_status, second_extended_status]
89 90 91 92 93 94 95 96 97
      end
    end

    context 'when exclusive statuses are matches' do
      before do
        allow(described_class).to receive(:extended_statuses)
          .and_return([[first_extended_status, second_extended_status]])
      end

98 99 100 101
      it 'does not fabricate compound decorator' do
        expect(fabricated_status.first_method).to eq 'first return value'
        expect(fabricated_status.second_method).to eq 'second return value'
        expect(fabricated_status).not_to respond_to(:third_method)
102 103 104
      end

      it 'delegates to core status' do
105 106 107 108 109 110 111 112 113
        expect(fabricated_status.text).to eq 'passed'
      end

      it 'matches correct core status' do
        expect(factory.core_status).to be_a Gitlab::Ci::Status::Success
      end

      it 'matches correct extended statuses' do
        expect(factory.extended_statuses).to eq [first_extended_status]
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
      end
    end

    context 'when exclusive statuses are not matched' do
      before do
        allow(described_class).to receive(:extended_statuses)
          .and_return([[first_extended_status], [second_extended_status]])
      end

      it_behaves_like 'compound decorator factory'
    end

    context 'when using simplified status grouping' do
      before do
        allow(described_class).to receive(:extended_statuses)
          .and_return([first_extended_status, second_extended_status])
      end

      it_behaves_like 'compound decorator factory'
    end
  end
135
end