spec/draper/factory_spec.rb in draper-1.2.0 vs spec/draper/factory_spec.rb in draper-1.2.1

- old
+ new

@@ -13,11 +13,11 @@ expect{Factory.new(foo: "bar")}.to raise_error ArgumentError, /Unknown key/ end end describe "#decorate" do - context "when source is nil" do + context "when object is nil" do it "returns nil" do factory = Factory.new expect(factory.decorate(nil)).to be_nil end @@ -29,16 +29,16 @@ Factory::Worker.should_receive(:new).and_return(worker) expect(factory.decorate(double)).to be :decorated end - it "passes the source to the worker" do + it "passes the object to the worker" do factory = Factory.new - source = double + object = double - Factory::Worker.should_receive(:new).with(anything(), source).and_return(->(*){}) - factory.decorate(source) + Factory::Worker.should_receive(:new).with(anything(), object).and_return(->(*){}) + factory.decorate(object) end context "when the :with option was given" do it "passes the decorator class to the worker" do decorator_class = double @@ -93,17 +93,17 @@ describe Factory::Worker do describe "#call" do it "calls the decorator method" do - source = double + object = double options = {foo: "bar"} - worker = Factory::Worker.new(double, source) + worker = Factory::Worker.new(double, object) decorator = ->(*){} worker.stub decorator: decorator - decorator.should_receive(:call).with(source, options).and_return(:decorated) + decorator.should_receive(:call).with(object, options).and_return(:decorated) expect(worker.call(options)).to be :decorated end context "when the :context option is callable" do it "calls it" do @@ -157,44 +157,44 @@ worker.call(foo: "bar", context_args: []) end end describe "#decorator" do - context "for a singular source" do + context "for a singular object" do context "when decorator_class is specified" do it "returns the .decorate method from the decorator" do decorator_class = Class.new(Decorator) worker = Factory::Worker.new(decorator_class, double) expect(worker.decorator).to eq decorator_class.method(:decorate) end end context "when decorator_class is unspecified" do - context "and the source is decoratable" do - it "returns the source's #decorate method" do - source = double + context "and the object is decoratable" do + it "returns the object's #decorate method" do + object = double options = {foo: "bar"} - worker = Factory::Worker.new(nil, source) + worker = Factory::Worker.new(nil, object) - source.should_receive(:decorate).with(options).and_return(:decorated) - expect(worker.decorator.call(source, options)).to be :decorated + object.should_receive(:decorate).with(options).and_return(:decorated) + expect(worker.decorator.call(object, options)).to be :decorated end end - context "and the source is not decoratable" do + context "and the object is not decoratable" do it "raises an error" do - source = double - worker = Factory::Worker.new(nil, source) + object = double + worker = Factory::Worker.new(nil, object) expect{worker.decorator}.to raise_error UninferrableDecoratorError end end end end - context "for a collection source" do + context "for a collection object" do context "when decorator_class is a CollectionDecorator" do it "returns the .decorate method from the collection decorator" do decorator_class = Class.new(CollectionDecorator) worker = Factory::Worker.new(decorator_class, []) @@ -210,21 +210,23 @@ expect(worker.decorator).to eq decorator_class.method(:decorate_collection) end end context "when decorator_class is unspecified" do - context "and the source is decoratable" do - it "returns the source's #decorate method" do - source = [] - options = {foo: "bar"} - worker = Factory::Worker.new(nil, source) + context "and the object is decoratable" do + it "returns the .decorate_collection method from the object's decorator" do + object = [] + decorator_class = Class.new(Decorator) + object.stub decorator_class: decorator_class + object.stub decorate: nil + worker = Factory::Worker.new(nil, object) - source.should_receive(:decorate).with(options).and_return(:decorated) - expect(worker.decorator.call(source, options)).to be :decorated + decorator_class.should_receive(:decorate_collection).with(object, foo: "bar", with: nil).and_return(:decorated) + expect(worker.decorator.call(object, foo: "bar")).to be :decorated end end - context "and the source is not decoratable" do + context "and the object is not decoratable" do it "returns the .decorate method from CollectionDecorator" do worker = Factory::Worker.new(nil, []) expect(worker.decorator).to eq CollectionDecorator.method(:decorate) end