spec/middleware/worker_spec.rb in wayfarer-0.4.0 vs spec/middleware/worker_spec.rb in wayfarer-0.4.1

- old
+ new

@@ -3,32 +3,30 @@ require "spec_helpers" describe Wayfarer::Middleware::Worker do let(:task) { build(:task) } - before { task.metadata.action = :index } - let(:job) do Class.new(Wayfarer::Base) do def index :foobar end end end + before do + task.metadata.action = :index + allow_any_instance_of(job).to receive(:arguments).and_return([task]) + end + describe "::route" do it "returns a route" do expect(job.route).to be_a(Wayfarer::Routing::Route) end end describe "#call" do - it "assigns the task" do - worker = job.new - expect { worker.call(task) }.to change { worker.task }.to(task) - end - it "calls the action method" do worker = job.new expect(worker).to receive(:index) worker.call(task) end @@ -39,11 +37,11 @@ end describe "#chain" do it "returns its middleware chain" do worker = job.new - expect(worker.chain.middlewares).to eq([*Wayfarer.core_middleware, worker]) + expect(worker.chain.middlewares).to eq([*Wayfarer.middleware, worker]) end end describe "#redis" do it "yields a Redis connection" do @@ -51,40 +49,79 @@ expect(conn).to be_a(Redis) end end end - describe "#page" do - let(:page) { Object.new } - let(:worker) { job.new } + describe "#params" do + it "delegates to task metadata" do + worker = job.new + expect(worker.task.metadata).to receive(:params) + worker.params + end + end - before do - task.metadata.page = page - worker.task = task + describe "#agent" do + it "delegates to task metadata" do + worker = job.new + expect(worker.task.metadata).to receive(:agent) + worker.agent end + end + describe "#browser" do + it "returns the context's instance" do + worker = job.new + context = spy + expect(worker.task.metadata).to receive(:agent).and_return(context) + expect(context).to receive(:instance) + worker.browser + end + end + + describe "#page" do + let(:worker) { job.new } + let(:page) { Object.new } + it "returns the page" do + task.metadata.page = page expect(worker.page).to be(page) end context "with live keyword" do - let(:adapter) { spy } - let(:body) { "foobar" } - let(:live) { Object.new } + let(:agent) { spy } + let(:result) { Wayfarer::Networking::Result::Success.new(page) } - before do - task.metadata.adapter = adapter - allow(adapter).to receive(:live).with(page).and_return(live) - end + before { task.metadata.agent = agent } - it "returns a live page" do - expect(worker.page(live: true)).to be(live) + context "when agent returns Result" do + before { allow(agent).to receive(:live).and_return(result) } + + it "returns the page" do + expect(worker.page(live: true)).to be(page) + end + + it "replaces the existing page" do + expect { + worker.page(live: true) + }.to change { task.metadata.page }.to(page) + end end - it "replaces the existing page" do - expect { - worker.page(live: true) - }.to change { task.metadata.page }.to(live) + context "when agent returns nil" do + before do + task.metadata.page = page + allow(agent).to receive(:live).and_return(nil) + end + + it "returns the existing page" do + expect(worker.page(live: true)).to be(page) + end + + it "does not replace the existing page" do + expect { + worker.page(live: true) + }.not_to(change { task.metadata.page }) + end end end end end