# frozen_string_literal: true require "spec_helpers" describe Wayfarer::Middleware::Dispatch do let(:task) { build(:task) } let(:action) { :action } subject(:chain) { described_class.new } before do task[:controller] = spy task[:action] = action allow(task[:controller]).to receive(:run_callbacks).and_yield end describe "#call" do it "runs callbacks" do expect(task[:controller]).to receive(:run_callbacks).with(action) subject.call(task) end context "when action is a Symbol" do it "calls the method" do expect(task[:controller]).to receive(action) subject.call(task) end end context "with handler" do let(:action) { Class.new.include(Wayfarer::Handler) } it "instantiates and calls" do expect_any_instance_of(action).to receive(:call).with(task) subject.call(task) end end context "without action" do let(:action) { nil } it "instantiates and calls" do expect { subject.call(task) }.to raise_error(ArgumentError) end end context "without other action" do let(:action) { Class.new } it "instantiates and calls" do expect { subject.call(task) }.to raise_error(ArgumentError) end end it "yields" do expect { |spy| subject.call(task, &spy) }.to yield_control end end end