command_definition_spec.rb 4.37 KB
Newer Older
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
require 'spec_helper'

describe Gitlab::SlashCommands::CommandDefinition do
  subject { described_class.new(:command) }

  describe "#all_names" do
    context "when the command has aliases" do
      before do
        subject.aliases = [:alias1, :alias2]
      end

      it "returns an array with the name and aliases" do
        expect(subject.all_names).to eq([:command, :alias1, :alias2])
      end
    end

    context "when the command doesn't have aliases" do
      it "returns an array with the name" do
        expect(subject.all_names).to eq([:command])
      end
    end
  end

  describe "#noop?" do
    context "when the command has an action block" do
      before do
27
        subject.action_block = proc { }
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
      end

      it "returns false" do
        expect(subject.noop?).to be false
      end
    end

    context "when the command doesn't have an action block" do
      it "returns true" do
        expect(subject.noop?).to be true
      end
    end
  end

  describe "#available?" do
    let(:opts) { { go: false } }

    context "when the command has a condition block" do
      before do
47
        subject.condition_block = proc { go }
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
      end

      context "when the condition block returns true" do
        before do
          opts[:go] = true
        end

        it "returns true" do
          expect(subject.available?(opts)).to be true
        end
      end

      context "when the condition block returns false" do
        it "returns false" do
          expect(subject.available?(opts)).to be false
        end
      end
    end

    context "when the command doesn't have a condition block" do
      it "returns true" do
        expect(subject.available?(opts)).to be true
      end
    end
  end

  describe "#execute" do
    let(:context) { OpenStruct.new(run: false) }

    context "when the command is a noop" do
      it "doesn't execute the command" do
        expect(context).not_to receive(:instance_exec)

81
        subject.execute(context, {}, nil)
82 83 84 85 86 87 88

        expect(context.run).to be false
      end
    end

    context "when the command is not a noop" do
      before do
89
        subject.action_block = proc { self.run = true }
90 91 92 93
      end

      context "when the command is not available" do
        before do
94
          subject.condition_block = proc { false }
95 96 97
        end

        it "doesn't execute the command" do
98
          subject.execute(context, {}, nil)
99 100 101 102 103 104

          expect(context.run).to be false
        end
      end

      context "when the command is available" do
105
        context "when the commnd has no arguments" do
106
          before do
107
            subject.action_block = proc { self.run = true }
108 109
          end

110 111 112
          context "when the command is provided an argument" do
            it "executes the command" do
              subject.execute(context, {}, true)
113

114
              expect(context.run).to be true
115 116 117
            end
          end

118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
          context "when the command is not provided an argument" do
            it "executes the command" do
              subject.execute(context, {}, nil)

              expect(context.run).to be true
            end
          end
        end

        context "when the command has 1 required argument" do
          before do
            subject.action_block = ->(arg) { self.run = arg }
          end

          context "when the command is provided an argument" do
133 134 135 136 137 138
            it "executes the command" do
              subject.execute(context, {}, true)

              expect(context.run).to be true
            end
          end
139 140 141 142 143 144 145 146

          context "when the command is not provided an argument" do
            it "doesn't execute the command" do
              subject.execute(context, {}, nil)

              expect(context.run).to be false
            end
          end
147 148
        end

149
        context "when the command has 1 optional argument" do
150
          before do
151 152 153 154 155 156 157 158 159
            subject.action_block = proc { |arg = nil| self.run = arg || true }
          end

          context "when the command is provided an argument" do
            it "executes the command" do
              subject.execute(context, {}, true)

              expect(context.run).to be true
            end
160 161
          end

162
          context "when the command is not provided an argument" do
163
            it "executes the command" do
164
              subject.execute(context, {}, nil)
165 166 167 168 169 170 171 172 173

              expect(context.run).to be true
            end
          end
        end
      end
    end
  end
end