command_definition_spec.rb 6.28 KB
Newer Older
1 2
require 'spec_helper'

3
describe Gitlab::QuickActions::CommandDefinition do
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
  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
      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
43
    let(:opts) { OpenStruct.new(go: false) }
44 45 46

    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
      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
75
    let(:context) { OpenStruct.new(run: false, commands_executed_count: nil) }
76 77 78 79 80

    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
        expect(context.commands_executed_count).to be_nil
84 85 86 87 88 89
        expect(context.run).to be false
      end
    end

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

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

        it "doesn't execute the command" do
99
          subject.execute(context, nil)
100

101
          expect(context.commands_executed_count).to be_nil
102 103 104 105 106
          expect(context.run).to be false
        end
      end

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

112 113
          context "when the command is provided an argument" do
            it "executes the command" do
114
              subject.execute(context, true)
115

116
              expect(context.run).to be true
117
              expect(context.commands_executed_count).to eq(1)
118 119 120
            end
          end

121 122
          context "when the command is not provided an argument" do
            it "executes the command" do
123
              subject.execute(context, nil)
124 125

              expect(context.run).to be true
126
              expect(context.commands_executed_count).to eq(1)
127 128 129 130 131 132 133 134 135 136
            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
137
            it "executes the command" do
138
              subject.execute(context, true)
139 140

              expect(context.run).to be true
141
              expect(context.commands_executed_count).to eq(1)
142 143
            end
          end
144 145 146

          context "when the command is not provided an argument" do
            it "doesn't execute the command" do
147
              subject.execute(context, nil)
148 149 150 151

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

154
        context "when the command has 1 optional argument" do
155
          before do
156 157 158 159 160
            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
161
              subject.execute(context, true)
162 163 164

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

167
          context "when the command is not provided an argument" do
168
            it "executes the command" do
169
              subject.execute(context, nil)
170 171 172 173 174

              expect(context.run).to be true
            end
          end
        end
175 176 177 178 179 180 181 182

        context 'when the command defines parse_params block' do
          before do
            subject.parse_params_block = ->(raw) { raw.strip }
            subject.action_block = ->(parsed) { self.received_arg = parsed }
          end

          it 'executes the command passing the parsed param' do
183
            subject.execute(context, 'something   ')
184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199

            expect(context.received_arg).to eq('something')
          end
        end
      end
    end
  end

  describe '#explain' do
    context 'when the command is not available' do
      before do
        subject.condition_block = proc { false }
        subject.explanation = 'Explanation'
      end

      it 'returns nil' do
200
        result = subject.explain({}, nil)
201 202 203 204 205 206 207 208 209 210 211

        expect(result).to be_nil
      end
    end

    context 'when the explanation is a static string' do
      before do
        subject.explanation = 'Explanation'
      end

      it 'returns this static string' do
212
        result = subject.explain({}, nil)
213 214 215 216 217

        expect(result).to eq 'Explanation'
      end
    end

218 219 220 221 222 223 224 225 226 227 228 229 230
    context 'when warning is set' do
      before do
        subject.explanation = 'Explanation'
        subject.warning = 'dangerous!'
      end

      it 'returns this static string' do
        result = subject.explain({}, nil)

        expect(result).to eq 'Explanation (dangerous!)'
      end
    end

231 232 233 234 235 236
    context 'when the explanation is dynamic' do
      before do
        subject.explanation = proc { |arg| "Dynamic #{arg}" }
      end

      it 'invokes the proc' do
237
        result = subject.explain({}, 'explanation')
238 239

        expect(result).to eq 'Dynamic explanation'
240 241 242 243
      end
    end
  end
end