Skip to content

Commit

Permalink
Tests for all responders added
Browse files Browse the repository at this point in the history
  • Loading branch information
miks committed Feb 15, 2015
1 parent 780cf10 commit 97bf914
Show file tree
Hide file tree
Showing 4 changed files with 250 additions and 0 deletions.
92 changes: 92 additions & 0 deletions releaf-core/spec/lib/releaf/responders_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
require "spec_helper"

describe Releaf::Responders, type: :controller do
subject{ Releaf::BaseController.new }

describe "#respond_with" do
before do
allow(subject).to receive(:active_responder).and_return(Releaf::Responders::AfterSaveResponder)
allow(subject).to receive(:request).and_return(request)
allow(subject).to receive(:content_type).and_return(:html)
end

context "when no responder defined within options" do
it "adds active responder to `responder` options" do
expect(Releaf::Responders::AfterSaveResponder).to receive(:call)
subject.respond_with(nil)
end
end

context "when responder is defined within options" do
it "adds active responder to `responder` options" do
expect(Releaf::Responders::AfterSaveResponder).to_not receive(:call)
expect(Releaf::Responders::PageNotFoundResponder).to receive(:call)
subject.respond_with(nil, responder: Releaf::Responders::PageNotFoundResponder)
end
end
end

describe "#action_responders" do
it "returns hash with action to responders matching" do
hash = {
create: Releaf::Responders::AfterSaveResponder,
update: Releaf::Responders::AfterSaveResponder,
confirm_destroy: Releaf::Responders::ConfirmDestroyResponder,
destroy: Releaf::Responders::DestroyResponder,
access_denied: Releaf::Responders::AccessDeniedResponder,
feature_disabled: Releaf::Responders::FeatureDisabledResponder,
page_not_found: Releaf::Responders::PageNotFoundResponder,
}
expect(subject.action_responders).to eq(hash)
end
end

describe "#action_responder" do
it "returns matching responder for given action" do
allow(subject).to receive(:action_responders).and_return(a: "x")
expect(subject.action_responder(:a)).to eq("x")
allow(subject).to receive(:action_responders).and_return(b: "x")
expect(subject.action_responder(:a)).to be nil
end
end

describe "#active_responder" do
it "returns currect action matching responder" do
allow(subject).to receive(:action_name).and_return(:save)
allow(subject).to receive(:action_responder).with(:save).and_return("x")
expect(subject.active_responder).to eq("x")
end
end
end

#module Releaf::Responders

#def respond_with(resource = nil, options = {}, &block)
#options[:responder] = active_responder unless options.has_key? :responder
#super
#end

#def action_responders
#{
#create: Releaf::Responders::AfterSaveResponder,
#update: Releaf::Responders::AfterSaveResponder,
#confirm_destroy: Releaf::Responders::ConfirmDestroyResponder,
#destroy: Releaf::Responders::DestroyResponder,
#access_denied: Releaf::Responders::AccessDeniedResponder,
#feature_disabled: Releaf::Responders::FeatureDisabledResponder,
#page_not_found: Releaf::Responders::PageNotFoundResponder,
#}
#end

## Returns generic view name for given action
## @return String
#def action_responder(_action_name)
#action_responders[_action_name.to_sym]
#end

## Returns generic view name for current action
## @return String
#def active_responder
#action_responder(action_name)
#end
#end
102 changes: 102 additions & 0 deletions releaf-core/spec/responders/after_save_responder_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
require "spec_helper"

describe Releaf::Responders::AfterSaveResponder, type: :controller do
let(:controller){ Releaf::BaseController.new }
let(:resource){ Book.new}
subject{ described_class.new(controller, [resource]) }

describe "#json_resource_errors" do
it "returns resource errors formatted with `Releaf::ErrorFormatter`" do
allow(Releaf::ErrorFormatter).to receive(:format_errors).with(resource).and_return(a: "b")
expect(subject.json_resource_errors).to eq(errors: {a: "b"})
end
end

describe "#render_notification?" do
before do
allow(subject).to receive(:has_errors?).and_return(true)
allow(subject).to receive(:format).and_return(:json)
end

context "when request format is other json" do
it "returns true" do
allow(subject).to receive(:format).and_return(:html)
expect(subject.render_notification?).to be true
end
end

context "when object has no errors" do
it "returns true" do
allow(subject).to receive(:has_errors?).and_return(false)
expect(subject.render_notification?).to be true
end
end

context "when request format is json and object has errors" do
it "returns false" do
expect(subject.render_notification?).to be false
end
end
end

describe "#respond" do
before do
allow(subject).to receive(:to_html)
end

context "when render notifications return `true`" do
it "renders notification with success value true/false whether resource has errors" do
allow(subject).to receive(:render_notification?).and_return(true)
allow(subject).to receive(:has_errors?).and_return(true)
expect(subject.controller).to receive(:render_notification).with(false)
subject.respond

allow(subject).to receive(:has_errors?).and_return(false)
expect(subject.controller).to receive(:render_notification).with(true)
subject.respond
end
end

context "when render notifications return `false`" do
it "does not render notification" do
allow(subject).to receive(:render_notification?).and_return(false)
expect(subject.controller).to_not receive(:render_notification)
subject.respond
end
end
end

describe "#to_json" do
context "when resource has errors" do
it "calls `display_errors`" do
allow(subject).to receive(:has_errors?).and_return(true)
expect(subject).to receive(:display_errors)
subject.to_json
end
end

context "when resource has no errors" do
before do
allow(subject).to receive(:resource_location).and_return("some_url")
allow(subject).to receive(:has_errors?).and_return(false)
end

context "when options has :redirect key" do
it "calls `display_errors`" do
allow(subject).to receive(:options).and_return(redirect: true)
expect(subject).to receive(:render).with(json: {url: "some_url"}, status: 303)
subject.to_json
end
end

context "when options has key :destroyable with `false` value" do
it "renders `refused_destroy` template" do
allow(subject).to receive(:options).and_return({})
expect(subject).to receive(:redirect_to).with("some_url", status: 303)
subject.to_json
end
end
end
end
end

26 changes: 26 additions & 0 deletions releaf-core/spec/responders/confirm_destroy_responder_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
require "spec_helper"

describe Releaf::Responders::ConfirmDestroyResponder, type: :controller do
let(:controller){ Releaf::BaseController.new }
let(:resource){ Book.new}
subject{ described_class.new(controller, [resource]) }

describe "#to_html" do
context "when options has key :destroyable with `true` value" do
it "renders default view" do
allow(subject).to receive(:options).and_return(destroyable: true)
expect(subject).to receive(:default_render)
subject.to_html
end
end

context "when options has key :destroyable with `false` value" do
it "renders `refused_destroy` template" do
allow(subject).to receive(:options).and_return(destroyable: false)
expect(subject).to receive(:render).with("refused_destroy")
subject.to_html
end
end
end
end

30 changes: 30 additions & 0 deletions releaf-core/spec/responders/destroy_responder_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
require "spec_helper"

describe Releaf::Responders::DestroyResponder, type: :controller do
let(:controller){ Releaf::BaseController.new }
let(:resource){ Book.new}
subject{ described_class.new(controller, [resource]) }

describe "#to_html" do
before do
allow(controller).to receive(:request).and_return(request)
allow(controller).to receive(:formats).and_return([:html])
allow(subject).to receive(:default_render)
end

context "when resource has been successfully destroyed" do
it "renders success notification" do
resource.destroy
expect(subject.controller).to receive(:render_notification).with(true, failure_message_key: "cant destroy, because relations exists")
subject.to_html
end
end

context "when resource has not been destroyed" do
it "renders failure notification" do
expect(subject.controller).to receive(:render_notification).with(false, failure_message_key: "cant destroy, because relations exists")
subject.to_html
end
end
end
end

0 comments on commit 97bf914

Please sign in to comment.