Sha256: 360c4fa454f80d70f1d4797de99ebeaefd6905565f9e66d72ce2a5bba1f111a4

Contents?: true

Size: 1.96 KB

Versions: 3

Compression:

Stored size: 1.96 KB

Contents

# frozen_string_literal: true

require "spec_helpers"

describe Wayfarer::Middleware::Controller do
  let(:task) { build(:task) }

  let(:middleware) do
    Class.new do
      extend Wayfarer::Middleware::Base

      module API; end

      def call(task); end
    end
  end

  subject(:controller) { Class.new.include(described_class) }

  describe "::use" do
    it "adds middleware" do
      expect {
        controller.use(middleware)
      }.to change { controller.chain.middlewares.count }.by(1)
    end

    it "adds lazy middleware" do
      controller.use(middleware)
      expect(controller.chain.middlewares.last).to be_a(Wayfarer::Middleware::Lazy)
      expect(controller.chain.middlewares.last.klass).to be(middleware)
      expect_any_instance_of(middleware).to receive(:call).with(task)
      controller.new.call(task)
    end
  end

  describe "::api" do
    it "includes middleware API" do
      expect {
        controller.api(middleware)
      }.to change { controller.include?(middleware.const_get(:API)) }.to(true)
    end
  end

  describe "#call" do
    subject { controller.new }

    it "assigns the task" do
      expect {
        subject.call(task)
      }.to change { subject.task }.to(task)
    end

    context "without job assigned" do
      it "assigns itself" do
        expect {
          subject.call(task)
        }.to change { task.metadata.job }.to(subject)
      end
    end

    context "with job assigned" do
      before { task.metadata.job = Object.new }

      it "does not override the job" do
        expect {
          subject.call(task)
        }.not_to(change { task.metadata.job })
      end
    end

    it "assigns itself as controller" do
      expect {
        subject.call(task)
      }.to change { task.metadata.controller }.to(subject)
    end

    it "calls the chain" do
      expect(subject.chain).to receive(:call)
      subject.call(task)
    end

    it "yields" do
      expect { |spy| subject.call(task, &spy) }.to yield_control
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
wayfarer-0.4.6 spec/middleware/controller_spec.rb
wayfarer-0.4.5 spec/middleware/controller_spec.rb
wayfarer-0.4.4 spec/middleware/controller_spec.rb