diff options
| author | cedricnjanga | 2018-01-30 06:37:52 -0800 |
|---|---|---|
| committer | cedricnjanga | 2018-01-30 06:37:52 -0800 |
| commit | 7ec19d8dc153e1491cb1a693b11b3b0a351f316c (patch) | |
| tree | 6da6a8bbcf1ae789a460fece5cf7e961562c5222 /spec | |
| parent | f32d869cc3f34a939764cc3fa4d612a5d6544d08 (diff) | |
| parent | 617a54cf63d7f11111b168534d49cdc04a5865d4 (diff) | |
| download | chouette-core-7ec19d8dc153e1491cb1a693b11b3b0a351f316c.tar.bz2 | |
Merge branch 'master' into 5683-add_workgroup_id_to_calendars
Diffstat (limited to 'spec')
| -rw-r--r-- | spec/decorators/api_key_decorator_spec.rb | 4 | ||||
| -rw-r--r-- | spec/decorators/referential_decorator_spec.rb | 52 | ||||
| -rw-r--r-- | spec/helpers/table_builder_helper_spec.rb | 70 | ||||
| -rw-r--r-- | spec/lib/af83/decorator/decorator_link_spec.rb | 79 | ||||
| -rw-r--r-- | spec/lib/af83/decorator/decorator_spec.rb | 824 | ||||
| -rw-r--r-- | spec/spec_helper.rb | 1 | ||||
| -rw-r--r-- | spec/support/decorator_helpers.rb | 23 | ||||
| -rw-r--r-- | spec/views/lines/index.html.slim_spec.rb | 2 | ||||
| -rw-r--r-- | spec/views/referentials/show.html.erb_spec.rb | 45 | ||||
| -rw-r--r-- | spec/views/stop_areas/index.html.slim_spec.rb | 1 |
10 files changed, 1048 insertions, 53 deletions
diff --git a/spec/decorators/api_key_decorator_spec.rb b/spec/decorators/api_key_decorator_spec.rb deleted file mode 100644 index 9451a3974..000000000 --- a/spec/decorators/api_key_decorator_spec.rb +++ /dev/null @@ -1,4 +0,0 @@ -require 'spec_helper' - -describe ApiKeyDecorator do -end diff --git a/spec/decorators/referential_decorator_spec.rb b/spec/decorators/referential_decorator_spec.rb index 9e34a0109..efc438132 100644 --- a/spec/decorators/referential_decorator_spec.rb +++ b/spec/decorators/referential_decorator_spec.rb @@ -21,8 +21,8 @@ RSpec.describe ReferentialDecorator, type: [:helper, :decorator] do context 'unarchived referential' do context 'no rights' do - it 'has only a Calendar action' do - expect_action_link_hrefs.to eq([referential_time_tables_path(object)]) + it 'has only show and Calendar actions' do + expect_action_link_hrefs.to eq([[object], referential_time_tables_path(object)]) end end @@ -31,8 +31,9 @@ RSpec.describe ReferentialDecorator, type: [:helper, :decorator] do let( :user ){ build_stubbed :allmighty_user } it 'has only default actions' do - expect_action_link_elements.to be_empty + expect_action_link_elements.to eq ["Consulter", "Calendriers", "Dupliquer"] expect_action_link_hrefs.to eq([ + [object], referential_time_tables_path(object), new_referential_path(from: object) ]) @@ -41,16 +42,36 @@ RSpec.describe ReferentialDecorator, type: [:helper, :decorator] do context 'all rights and same organisation' do let( :user ){ build_stubbed :allmighty_user, organisation: referential.organisation } + let( :action){ :index } + context "on index" do + it 'has corresponding actions' do + expect_action_link_elements(action).to eq ["Consulter", "Editer", "Calendriers", "Dupliquer", "Valider", "Conserver","<span class=\"fa fa-trash mr-xs\"></span>Supprimer"] + expect_action_link_hrefs(action).to eq([ + [object], + [:edit, object], + referential_time_tables_path(object), + new_referential_path(from: object), + referential_select_compliance_control_set_path(object), + archive_referential_path(object), + referential_path(object) + ]) + end + end - it 'has all actions' do - expect_action_link_elements.to eq(%w{Purger}) - expect_action_link_hrefs.to eq([ - referential_time_tables_path(object), - new_referential_path(from: object), - referential_select_compliance_control_set_path(object), - archive_referential_path(object), - referential_path(object) - ]) + context "on show" do + let( :action){ :show } + it 'has corresponding actions' do + expect_action_link_elements(action).to eq ["Editer", "Calendriers", "Dupliquer", "Valider", "Conserver", "Purger", "<span class=\"fa fa-trash mr-xs\"></span>Supprimer"] + expect_action_link_hrefs(action).to eq([ + [:edit, object], + referential_time_tables_path(object), + new_referential_path(from: object), + referential_select_compliance_control_set_path(object), + archive_referential_path(object), + "#", + referential_path(object) + ]) + end end end end @@ -58,16 +79,17 @@ RSpec.describe ReferentialDecorator, type: [:helper, :decorator] do context 'archived referential' do before { referential.archived_at = 42.seconds.ago } context 'no rights' do - it 'has only a Calendar action' do - expect_action_link_hrefs.to eq([referential_time_tables_path(object)]) + it 'has only ahow and calendar actions' do + expect_action_link_hrefs.to eq([[object], referential_time_tables_path(object)]) end end context 'all rights and different organisation' do let( :user ){ build_stubbed :allmighty_user } it 'has only default actions' do - expect_action_link_elements.to be_empty + expect_action_link_elements.to eq ["Consulter", "Calendriers", "Dupliquer"] expect_action_link_hrefs.to eq([ + [object], referential_time_tables_path(object), new_referential_path(from: object) ]) diff --git a/spec/helpers/table_builder_helper_spec.rb b/spec/helpers/table_builder_helper_spec.rb index 8b383d88d..5bddbb16f 100644 --- a/spec/helpers/table_builder_helper_spec.rb +++ b/spec/helpers/table_builder_helper_spec.rb @@ -6,6 +6,13 @@ module TableBuilderHelper end describe TableBuilderHelper, type: :helper do + let(:features){ [] } + before do + allow_any_instance_of(AF83::Decorator::Link).to receive(:check_feature){|f| + features.include?(f) + } + end + describe "#table_builder_2" do it "builds a table" do referential = build_stubbed(:workbench_referential) @@ -36,9 +43,8 @@ describe TableBuilderHelper, type: :helper do id: referentials[0].workbench.id }) - referentials = ModelDecorator.decorate( - referentials, - with: ReferentialDecorator + referentials = ReferentialDecorator.decorate( + referentials ) expected = <<-HTML @@ -77,15 +83,21 @@ describe TableBuilderHelper, type: :helper do <td class="actions"> <div class="btn-group"> <div class="btn dropdown-toggle" data-toggle="dropdown"><span class="fa fa-cog"></span></div> - <ul class="dropdown-menu"> - <li><a href="/referentials/#{referential.id}">Consulter</a></li> - <li><a href="/referentials/#{referential.id}/edit">Editer</a></li> - <li><a href="/referentials/#{referential.id}/time_tables">Calendriers</a></li> - <li><a href="/referentials/new?from=#{referential.id}">Dupliquer</a></li> - <li><a href="/referentials/#{referential.id}/select_compliance_control_set">Valider</a></li> - <li><a rel="nofollow" data-method="put" href="/referentials/#{referential.id}/archive">Conserver</a></li> - <li class="delete-action"><a data-confirm="Etes vous sûr de vouloir supprimer ce jeu de données ?" rel="nofollow" data-method="delete" href="/referentials/#{referential.id}"><span class="fa fa-trash mr-xs"></span>Supprimer</a></li> - </ul> + <div class="dropdown-menu"> + <ul class="primary"> + <li class=""><a href="/referentials/#{referential.id}">Consulter</a></li> + <li class=""><a href="/referentials/#{referential.id}/edit">Editer</a></li> + </ul> + <ul class="other"> + <li class=""><a href="/referentials/#{referential.id}/time_tables">Calendriers</a></li> + <li class=""><a href="/referentials/new?from=#{referential.id}">Dupliquer</a></li> + <li class=""><a href="/referentials/#{referential.id}/select_compliance_control_set">Valider</a></li> + <li class=""><a rel="nofollow" data-method="put" href="/referentials/#{referential.id}/archive">Conserver</a></li> + </ul> + <ul class="footer"> + <li class=" delete-action"><a data-confirm="Etes vous sûr de vouloir supprimer ce jeu de données ?" rel="nofollow" data-method="delete" href="/referentials/#{referential.id}"><span class="fa fa-trash mr-xs"></span>Supprimer</a></li> + </ul> + </div> </div> </td> </tr> @@ -149,7 +161,7 @@ describe TableBuilderHelper, type: :helper do ) ], selectable: true, - links: [:show, :edit], + action: :index, cls: 'table has-filter has-search' ) @@ -195,9 +207,9 @@ describe TableBuilderHelper, type: :helper do referential_id: referential.id }) - companies = ModelDecorator.decorate( + companies = CompanyDecorator.decorate( companies, - with: CompanyDecorator + context: { referential: referential } ) stub_policy_scope(company) @@ -223,9 +235,11 @@ describe TableBuilderHelper, type: :helper do <td class="actions"> <div class="btn-group"> <div class="btn dropdown-toggle" data-toggle="dropdown"><span class="fa fa-cog"></span></div> - <ul class="dropdown-menu"> - <li><a href="/referentials/#{referential.id}/companies/#{company.id}">Consulter</a></li> - </ul> + <div class="dropdown-menu"> + <ul class="primary"> + <li class=""><a href="/referentials/#{referential.id}/companies/#{company.id}">Consulter</a></li> + </ul> + </div> </div> </td> </tr> @@ -307,9 +321,8 @@ describe TableBuilderHelper, type: :helper do referential_id: referential.id }) - companies = ModelDecorator.decorate( + companies = CompanyDecorator.decorate( companies, - with: CompanyDecorator, context: { referential: line_referential } ) stub_policy_scope(company) @@ -336,9 +349,11 @@ describe TableBuilderHelper, type: :helper do <td class="actions"> <div class="btn-group"> <div class="btn dropdown-toggle" data-toggle="dropdown"><span class="fa fa-cog"></span></div> - <ul class="dropdown-menu"> - <li><a href="/referentials/#{referential.id}/companies/#{company.id}">Consulter</a></li> - </ul> + <div class="dropdown-menu"> + <ul class="primary"> + <li class=""><a href="/line_referentials/#{line_referential.id}/companies/#{company.id}">Consulter</a></li> + </ul> + </div> </div> </td> </tr> @@ -374,7 +389,6 @@ describe TableBuilderHelper, type: :helper do ), ], sortable: false, - links: [:show, :edit, :delete], cls: 'table has-search' ) @@ -425,7 +439,7 @@ describe TableBuilderHelper, type: :helper do let(:selectable){ false } it "sets all rows as non selectable" do items.each do |i| - tr = helper.send(:tr, i, columns, selectable, links, overhead, model_name) + tr = helper.send(:tr, i, columns, selectable, links, overhead, model_name, :index) klass = "#{TableBuilderHelper.item_row_class_name([referential])}-#{i.id}" selector = "tr.#{klass} [type=checkbox]" expect(tr).to_not have_selector selector @@ -437,7 +451,7 @@ describe TableBuilderHelper, type: :helper do let(:selectable){ true } it "adds a checkbox in all rows" do items.each do |i| - tr = helper.send(:tr, i, columns, selectable, links, overhead, model_name) + tr = helper.send(:tr, i, columns, selectable, links, overhead, model_name, :index) klass = "#{TableBuilderHelper.item_row_class_name([referential])}-#{i.id}" selector = "tr.#{klass} [type=checkbox]" expect(tr).to have_selector selector @@ -449,14 +463,14 @@ describe TableBuilderHelper, type: :helper do let(:selectable){ ->(i){ i.id != item.id } } it "adds a checkbox in all rows" do items.each do |i| - tr = helper.send(:tr, i, columns, selectable, links, overhead, model_name) + tr = helper.send(:tr, i, columns, selectable, links, overhead, model_name, :index) klass = "#{TableBuilderHelper.item_row_class_name([referential])}-#{i.id}" selector = "tr.#{klass} [type=checkbox]" expect(tr).to have_selector selector end end it "disables this rows checkbox" do - tr = helper.send(:tr, item, columns, selectable, links, overhead, model_name) + tr = helper.send(:tr, item, columns, selectable, links, overhead, model_name, :index) klass = "#{TableBuilderHelper.item_row_class_name([referential])}-#{item.id}" selector = "tr.#{klass} [type=checkbox][disabled]" expect(tr).to have_selector selector diff --git a/spec/lib/af83/decorator/decorator_link_spec.rb b/spec/lib/af83/decorator/decorator_link_spec.rb new file mode 100644 index 000000000..0b2939421 --- /dev/null +++ b/spec/lib/af83/decorator/decorator_link_spec.rb @@ -0,0 +1,79 @@ +RSpec.describe AF83::Decorator::Link, type: :decorator do + describe "#complete?" do + context "on a imcomplete link" do + it "should be false" do + expect(AF83::Decorator::Link.new.complete?).to be_falsy + expect(AF83::Decorator::Link.new(content: "foo").complete?).to be_falsy + expect(AF83::Decorator::Link.new(href: "foo").complete?).to be_falsy + end + end + + context "on a complete link" do + it "should be true" do + expect(AF83::Decorator::Link.new(href: "foo", content: "foo").complete?).to be_truthy + end + end + end + + describe "#class" do + let(:link){ + AF83::Decorator::Link.new(href: "foo", content: "foo", class: "initial_class") + } + + it "should override exisiting class" do + expect(link.html_options[:class]).to eq "initial_class" + link.class "new_class" + expect(link.html_options[:class]).to eq "new_class" + link.class = "another_class" + expect(link.html_options[:class]).to eq "another_class" + link.class = %w(foo bar) + expect(link.html_options[:class]).to eq "foo bar" + end + end + + describe "#add_class" do + let(:link){ + AF83::Decorator::Link.new(href: "foo", content: "foo", class: "initial_class") + } + + it "should add to exisiting class" do + expect(link.html_options[:class]).to eq "initial_class" + link.add_class "new_class" + expect(link.html_options[:class]).to eq "initial_class new_class" + link.add_class "another_class" + expect(link.html_options[:class]).to eq "initial_class new_class another_class" + link.add_class %w(foo bar) + expect(link.html_options[:class]).to eq "initial_class new_class another_class foo bar" + end + end + + describe "#type" do + + let(:link){ + AF83::Decorator::Link.new(href: "foo", content: "foo") + } + + let(:context){ + Class.new do + def h + Class.new do + def link_to *args + HTMLElement.new(:a, 'foo', {}).to_html + end + end.new + end + end.new + } + + it "should allow for buttons" do + link.type = :button + expect(link.to_html).to match /\<button.*\<\/button\>/ + end + + it "should fallback to <a>" do + link.type = :spaghetti + link.bind_to_context context, :show + expect(link.to_html).to match /\<a.*\<\/a\>/ + end + end +end diff --git a/spec/lib/af83/decorator/decorator_spec.rb b/spec/lib/af83/decorator/decorator_spec.rb new file mode 100644 index 000000000..61a849b9d --- /dev/null +++ b/spec/lib/af83/decorator/decorator_spec.rb @@ -0,0 +1,824 @@ +RSpec.describe AF83::Decorator, type: :decorator do + describe(:parse_options) do + let(:options){ + {primary: true, secondary: %i(index show), policy: :blublu, weight: 12} + } + let(:link_options){ + {foo: :foo, bar: :bar} + } + let(:args){ options.dup.update(link_options.dup) } + it "should separate options from link_options" do + _options, _link_options = AF83::Decorator.instance_decorator.send :parse_options, args + expect(_options).to eq({weight: 12}) + link_options.each do |k, v| + expect(_link_options[k]).to eq v + end + expect(_link_options[:_groups][:primary]).to eq true + expect(_link_options[:_groups][:secondary]).to eq %i(index show) + expect(_link_options[:_policy]).to eq :blublu + end + end + + link_should_match_options = ->(link, options){ + options.each do |k, v| + expect(link.send(k)).to eq v + end + } + + context "as an collection decorator" do + let(:link_options) do + { + href: "/foo/bar", + content: "Blublu" + } + end + + let(:decorator) do + klass = Class.new(AF83::Decorator) + klass.action_link link_options + klass + end + + let(:decorated) do + 3.times { create :line } + decorator.decorate(Chouette::Line.all) + end + + it "should return the links" do + links = decorated.action_links + instance_exec links.first, link_options, &link_should_match_options + end + end + + context "as an instance decorator" do + describe("with the actual decorator") do + before(:each) do + Draper::HelperProxy.any_instance.stub(:policy){ + klass = Class.new do + def method_missing *args + true + end + end.new + } + end + + let(:decorated) do + line = create :line + line.decorate(context: {line_referential: line.line_referential}) + end + + it "should return the links" do + expect{ decorated.action_links }.to_not raise_error + end + end + + describe(:action_links) do + let(:decorated) do + obj = create :line + decorator.decorate(obj) + end + + context "without links" do + let(:decorator) do + Class.new(AF83::Decorator) + end + + it "should return no link" do + links = decorated.action_links + expect(links.size).to eq 0 + end + end + + context "with a single link" do + let(:link_options) do + { + href: "/foo/bar", + content: "Blublu" + } + end + + context "incompletetly defined" do + let(:decorator) do + klass = Class.new(AF83::Decorator) + klass.with_instance_decorator do |instance_decorator| + instance_decorator.action_link href: "bar" + end + klass + end + + it "should raise an error" do + expect{decorator}.to raise_error(AF83::Decorator::IncompleteLinkDefinition) + end + end + + context "defined inline" do + let(:decorator) do + klass = Class.new(AF83::Decorator) + klass.with_instance_decorator do |instance_decorator| + instance_decorator.action_link link_options + end + klass + end + + it "should return the defined link" do + links = decorated.action_links + expect(links.size).to eq 1 + instance_exec links.first, link_options, &link_should_match_options + end + end + + context "defined in a block" do + let(:decorator) do + klass = Class.new(AF83::Decorator) + klass.with_instance_decorator do |instance_decorator| + instance_decorator.action_link do |l| + l.href link_options[:href] + l.content link_options[:content] + end + end + klass + end + + it "should return the defined link" do + links = decorated.action_links + expect(links.size).to eq 1 + instance_exec links.first, link_options, &link_should_match_options + end + end + + context "with proc attributes" do + let(:decorator) do + klass = Class.new(AF83::Decorator) + klass.with_instance_decorator do |instance_decorator| + instance_decorator.action_link do |l| + l.href { context[:href] } + l.content "Blublu" + end + end + klass + end + + let(:decorated) do + obj = create :line + decorator.decorate(obj, context: {href: link_options[:href]}) + end + + it "should return the defined link" do + links = decorated.action_links + expect(links.size).to eq 1 + expect(links.first.href).to eq link_options[:href] + end + end + + context "with a method attributes" do + let(:decorator) do + klass = Class.new(AF83::Decorator) + klass.with_instance_decorator do |instance_decorator| + instance_decorator.action_link do |l| + l.href "/foo/bar" + l.content "Blublu" + l.method :put + end + end + klass + end + + let(:decorated) do + obj = create :line + decorator.decorate(obj, context: {href: link_options[:href]}) + end + + it "should return the defined method" do + links = decorated.action_links + expect(links.size).to eq 1 + expect(links.first.method).to eq :put + end + end + end + + context "with 2 links" do + let(:link_options_1) do + { + href: "/foo/bar", + content: "Blublu" + } + end + + let(:link_options_2) do + { + href: "/foo/bar/baz", + content: "Foo" + } + end + + context "without weight" do + let(:decorator) do + klass = Class.new(AF83::Decorator) + klass.with_instance_decorator do |instance_decorator| + instance_decorator.action_link link_options_1 + instance_decorator.action_link link_options_2 + end + klass + end + + it "should return links in the sequence they were defined" do + links = decorated.action_links + expect(links.size).to eq 2 + instance_exec links.first, link_options_1, &link_should_match_options + instance_exec links.last, link_options_2, &link_should_match_options + end + end + + context "with weight" do + let(:decorator) do + klass = Class.new(AF83::Decorator) + klass.with_instance_decorator do |instance_decorator| + instance_decorator.action_link link_options_1.update(weight: 10) + instance_decorator.action_link link_options_2 + end + klass + end + + it "should return links in the correct sequence" do + links = decorated.action_links + expect(links.size).to eq 2 + instance_exec links.first, link_options_2, &link_should_match_options + instance_exec links.last, link_options_1, &link_should_match_options + end + end + + context "scoped by action" do + let(:decorator) do + klass = Class.new(AF83::Decorator) + klass.with_instance_decorator do |instance_decorator| + instance_decorator.action_link link_options_1.update(action: :index) + instance_decorator.action_link link_options_2 + end + klass + end + + it "should only return links defined for the given action" do + links = decorated.action_links(:show) + expect(links.size).to eq 1 + instance_exec links.first, link_options_2, &link_should_match_options + end + end + + context "with a policy" do + let(:decorator) do + klass = Class.new(AF83::Decorator) + klass.with_instance_decorator do |instance_decorator| + instance_decorator.action_link href: "foo", content: "foo", policy: :edit + end + klass + end + + context "when the policy is not met" do + before(:each) do + Draper::HelperProxy.any_instance.stub(:policy){ + klass = Class.new do + def edit? + false + end + end.new + } + end + + it "should not return the link" do + links = decorated.action_links(:show) + expect(links.size).to eq 0 + end + end + + context "when the policy is met" do + before(:each) do + Draper::HelperProxy.any_instance.stub(:policy){ + klass = Class.new do + def edit? + true + end + end.new + } + end + + it "should not return the link" do + links = decorated.action_links(:show) + expect(links.size).to eq 1 + end + end + end + + context "with a feature" do + let(:decorator) do + klass = Class.new(AF83::Decorator) + klass.with_instance_decorator do |instance_decorator| + instance_decorator.action_link href: "foo", content: "foo", feature: :foo + end + klass + end + + context "when the feature is not present" do + before(:each) do + Draper::HelperProxy.any_instance.stub(:has_feature?){false} + end + + it "should not return the link" do + links = decorated.action_links(:show) + expect(links.size).to eq 0 + end + end + + context "when the feature is present" do + before(:each) do + Draper::HelperProxy.any_instance.stub(:has_feature?){true} + end + + it "should not return the link" do + links = decorated.action_links(:show) + expect(links.size).to eq 1 + end + end + end + + context "with a condition" do + context "set with 'with_condition'" do + context "as a value" do + context "when the condition is true" do + let(:decorator) do + klass = Class.new(AF83::Decorator) + klass.with_instance_decorator do |instance_decorator| + instance_decorator.with_condition true do + action_link href: "foo", content: "foo" + end + end + klass + end + + it "should return the link" do + links = decorated.action_links(:show) + expect(links.size).to eq 1 + end + end + + context "when the condition is false" do + let(:decorator) do + klass = Class.new(AF83::Decorator) + klass.with_instance_decorator do |instance_decorator| + instance_decorator.with_condition false do + action_link href: "foo", content: "foo" + end + end + klass + end + + it "should not return the link" do + links = decorated.action_links(:show) + expect(links.size).to eq 0 + end + end + end + + context "as a Proc" do + let(:decorator) do + klass = Class.new(AF83::Decorator) + klass.with_instance_decorator do |instance_decorator| + instance_decorator.with_condition ->{context[:show_link]} do + action_link href: "foo", content: "foo" + end + end + klass + end + + context "when the condition is true" do + let(:decorated) do + obj = create :line + decorator.decorate(obj, context: {show_link: true}) + end + + it "should return the link" do + links = decorated.action_links(:show) + expect(links.size).to eq 1 + end + end + + context "when the condition is false" do + let(:decorated) do + obj = create :line + decorator.decorate(obj, context: {show_link: false}) + end + + it "should not return the link" do + links = decorated.action_links(:show) + expect(links.size).to eq 0 + end + end + end + end + + context "set inline" do + context "as a value" do + context "when the condition is true" do + let(:decorator) do + klass = Class.new(AF83::Decorator) + klass.with_instance_decorator do |instance_decorator| + instance_decorator.action_link link_options_1.update(if: true) + end + klass + end + + it "should return the link" do + links = decorated.action_links(:show) + expect(links.size).to eq 1 + end + end + + context "when the condition is false" do + let(:decorator) do + klass = Class.new(AF83::Decorator) + klass.with_instance_decorator do |instance_decorator| + instance_decorator.action_link link_options_1.update(if: false) + end + klass + end + + it "should not return the link" do + links = decorated.action_links(:show) + expect(links.size).to eq 0 + end + end + end + + context "as a Proc" do + let(:decorator) do + klass = Class.new(AF83::Decorator) + klass.with_instance_decorator do |instance_decorator| + instance_decorator.action_link link_options_1.update(if: ->{context[:show_link]}) + end + klass + end + + context "when the condition is true" do + let(:decorated) do + obj = create :line + decorator.decorate(obj, context: {show_link: true}) + end + + it "should return the link" do + links = decorated.action_links(:show) + expect(links.size).to eq 1 + end + end + + context "when the condition is false" do + let(:decorated) do + obj = create :line + decorator.decorate(obj, context: {show_link: false}) + end + + it "should not return the link" do + links = decorated.action_links(:show) + expect(links.size).to eq 0 + end + end + end + end + end + + context "scoped by action" do + context "with a single action" do + let(:decorator) do + klass = Class.new(AF83::Decorator) + klass.with_instance_decorator do |instance_decorator| + instance_decorator.action_link link_options_1.update(action: :index) + instance_decorator.action_link link_options_2 + end + klass + end + + it "should only return links defined for the given action" do + links = decorated.action_links(:show) + expect(links.size).to eq 1 + instance_exec links.first, link_options_2, &link_should_match_options + end + end + + context "with several actions" do + let(:decorator) do + klass = Class.new(AF83::Decorator) + klass.with_instance_decorator do |instance_decorator| + instance_decorator.action_link link_options_1.update(actions: %i(index edit)) + instance_decorator.action_link link_options_2.update(actions: %i(show edit)) + end + klass + end + + it "should only return links defined for the given action" do + links = decorated.action_links(:show) + expect(links.size).to eq 1 + instance_exec links.first, link_options_2, &link_should_match_options + end + end + + context "with the keyword 'on'" do + let(:decorator) do + klass = Class.new(AF83::Decorator) + klass.with_instance_decorator do |instance_decorator| + instance_decorator.action_link link_options_1.update(on: %i(index edit)) + instance_decorator.action_link link_options_2.update(on: :show) + end + klass + end + + it "should only return links defined for the given action" do + links = decorated.action_links(:show) + expect(links.size).to eq 1 + instance_exec links.first, link_options_2, &link_should_match_options + end + end + end + end + end + + describe '#primary' do + let(:decorator) do + Class.new(AF83::Decorator) + end + + let(:decorated) do + obj = create :line + decorator.decorate(obj) + end + + it "should return a new object everytime" do + actions = decorated.action_links + primary = actions.primary + expect(actions.options[:groups]).to be_nil + expect(primary.options[:groups]).to_not be_nil + end + end + + describe(:primary_links) do + let(:decorated) do + obj = create :line + decorator.decorate(obj) + end + + context "without links" do + let(:decorator) do + Class.new(AF83::Decorator) + end + + it "should return no link" do + links = decorated.action_links + expect(links.size).to eq 0 + end + end + + context "with a single link" do + let(:link_options) do + { + href: "/foo/bar/baz", + content: "Blublu", + primary: primary + } + end + + let(:decorator) do + klass = Class.new(AF83::Decorator) + klass.with_instance_decorator do |instance_decorator| + instance_decorator.action_link link_options + end + klass + end + + context "always primary" do + let(:primary){ true } + + it "should return the link" do + links = decorated.primary_links(:show) + expect(links.size).to eq 1 + end + end + + context "primary on this action" do + let(:primary){ :show } + + it "should return the link" do + links = decorated.primary_links(:show) + expect(links.size).to eq 1 + end + end + + context "primary on this action among others" do + let(:primary){ %i(show edit) } + + it "should return the link" do + links = decorated.action_links(:show, group: :primary) + expect(links.size).to eq 1 + end + end + + context "primary on other actions" do + let(:primary){ %i(index edit) } + + it "should not return the link" do + links = decorated.action_links(:show, group: :primary) + expect(links.size).to eq 0 + end + end + + context "primary on another action" do + let(:primary){ :index } + + it "should not return the link" do + links = decorated.primary_links(:show) + expect(links.size).to eq 0 + end + end + + context "never primary" do + let(:primary){ nil } + + it "should not return the link" do + links = decorated.primary_links(:show) + expect(links.size).to eq 0 + end + end + end + end + + describe("in a group") do + let(:decorated) do + obj = create :line + decorator.decorate(obj) + end + + context "without links" do + let(:decorator) do + Class.new(AF83::Decorator) + end + + it "should return no link" do + links = decorated.action_links + expect(links.size).to eq 0 + end + end + + + context "with a single link" do + let(:link_options) do + { + href: "/foo/bar/baz", + content: "Blublu", + groups: {foo: group} + } + end + + let(:decorator) do + klass = Class.new(AF83::Decorator) + klass.with_instance_decorator do |instance_decorator| + instance_decorator.action_link link_options + end + klass + end + + context "always in" do + let(:group){ true } + + it "should return the link" do + links = decorated.action_links(:show, group: :foo) + expect(links.size).to eq 1 + end + + context "define with group" do + let(:link_options) do + { + href: "/foo/bar/baz", + content: "Blublu", + group: :foo + } + end + + let(:decorator) do + klass = Class.new(AF83::Decorator) + klass.with_instance_decorator do |instance_decorator| + instance_decorator.action_link link_options + end + klass + end + + it "should return the link" do + links = decorated.action_links(:show, group: :foo) + expect(links.size).to eq 1 + end + + it "should not return the link" do + links = decorated.action_links(:show, group: :bar) + expect(links.size).to eq 0 + end + end + end + + context "primary on this action" do + let(:group){ :show } + + it "should return the link" do + links = decorated.action_links(:show, group: :foo) + expect(links.size).to eq 1 + end + end + + context "in this action among others" do + let(:group){ %i(show edit) } + + it "should return the link" do + links = decorated.action_links(:show, group: :foo) + expect(links.size).to eq 1 + end + end + + context "in other actions" do + let(:group){ %i(index edit) } + + it "should not return the link" do + links = decorated.action_links(:show, group: :foo) + expect(links.size).to eq 0 + end + end + + context "in another action" do + let(:group){ :index } + + it "should not return the link" do + links = decorated.action_links(:show, group: :foo) + expect(links.size).to eq 0 + end + end + + context "never" do + let(:group){ nil } + + it "should not return the link" do + links = decorated.action_links(:show, group: :foo) + expect(links.size).to eq 0 + end + end + end + + describe(:grouped_by) do + let(:link_options_1) do + { + href: "/foo/bar", + content: "Blublu", + primary: true + } + end + + let(:link_options_2) do + { + href: "/foo/bar/baz", + content: "Foo", + groups: {secondary: :show} + } + end + + let(:link_options_3) do + { + href: "/foo/bar/baz/bat", + content: "Foo", + groups: {foo: :show} + } + end + + let(:link_options_4) do + { + href: "/footer", + content: "Foo", + footer: true + } + end + + let(:decorator) do + klass = Class.new(AF83::Decorator) + klass.with_instance_decorator do |instance_decorator| + instance_decorator.action_link link_options_1 + instance_decorator.action_link link_options_2 + instance_decorator.action_link link_options_3 + instance_decorator.action_link link_options_4 + end + klass + end + + it "should return links in their groups" do + links = decorated.action_links(:show).grouped_by(:primary, :secondary, :blu, :footer) + expect(links.size).to eq 5 + instance_exec links[:primary].first, link_options_1, &link_should_match_options + instance_exec links[:secondary].first, link_options_2, &link_should_match_options + expect(links[:blu].size).to eq 0 + instance_exec links[:other].first, link_options_3, &link_should_match_options + instance_exec links[:footer].first, link_options_4, &link_should_match_options + end + end + end + end +end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 2d13d3802..cde252236 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -100,7 +100,6 @@ RSpec.configure do |config| # The different available types are documented in the features, such as in # https://relishapp.com/rspec/rspec-rails/docs config.infer_spec_type_from_file_location! - end Shoulda::Matchers.configure do |config| diff --git a/spec/support/decorator_helpers.rb b/spec/support/decorator_helpers.rb index 9d450deb1..b2c41e842 100644 --- a/spec/support/decorator_helpers.rb +++ b/spec/support/decorator_helpers.rb @@ -5,18 +5,31 @@ module Support subject{ object.decorate } let( :policy ){ ::Pundit.policy(user_context, object) } let( :user_context ){ UserContext.new(user, referential: referential) } - + let( :features ){ [] } + let( :filtered_action_links){} before do allow_any_instance_of(Draper::HelperProxy).to receive(:policy).and_return policy + allow_any_instance_of(AF83::Decorator::Link).to receive(:check_feature){|f| + features.include?(f) + } end end end - def expect_action_link_hrefs - expect( subject.action_links.select(&Link.method(:===)).map(&:href) ) + def expect_action_link_hrefs(action=:index) + if subject.action_links.is_a? AF83::Decorator::ActionLinks + expect( subject.action_links(action).map(&:href) ) + else + expect( subject.action_links.select(&Link.method(:===)).map(&:href) ) + end end - def expect_action_link_elements - expect( subject.action_links.select(&HTMLElement.method(:===)).map(&:content) ) + + def expect_action_link_elements(action=:index) + if subject.action_links.is_a? AF83::Decorator::ActionLinks + expect( subject.action_links(action).map(&:content) ) + else + expect( subject.action_links.select(&HTMLElement.method(:===)).map(&:content) ) + end end end end diff --git a/spec/views/lines/index.html.slim_spec.rb b/spec/views/lines/index.html.slim_spec.rb index 498784912..fb436c545 100644 --- a/spec/views/lines/index.html.slim_spec.rb +++ b/spec/views/lines/index.html.slim_spec.rb @@ -19,7 +19,9 @@ describe "/lines/index", :type => :view do deactivated_line allow(view).to receive(:collection).and_return(lines) allow(view).to receive(:current_referential).and_return(line_referential) + allow(view).to receive(:params).and_return({action: :index}) controller.request.path_parameters[:line_referential_id] = line_referential.id + controller.request.path_parameters[:action] = "index" render end diff --git a/spec/views/referentials/show.html.erb_spec.rb b/spec/views/referentials/show.html.erb_spec.rb index f1fa7188a..4a2afe2ca 100644 --- a/spec/views/referentials/show.html.erb_spec.rb +++ b/spec/views/referentials/show.html.erb_spec.rb @@ -1,4 +1,49 @@ require 'spec_helper' describe "referentials/show", type: :view do + + let!(:referential) do + referential = create(:referential) + assign :referential, referential.decorate(context: { + current_organisation: referential.organisation + }) + end + let(:permissions){ [] } + let(:current_organisation) { organisation } + let(:current_offer_workbench) { create :workbench, organisation: current_organisation} + let(:readonly){ false } + + before :each do + assign :reflines, [] + allow(view).to receive(:current_offer_workbench).and_return(current_offer_workbench) + allow(view).to receive(:current_organisation).and_return(current_organisation) + allow(view).to receive(:current_user).and_return(current_user) + + allow(view).to receive(:resource).and_return(referential) + allow(view).to receive(:has_feature?).and_return(true) + allow(view).to receive(:user_signed_in?).and_return true + controller.request.path_parameters[:id] = referential.id + allow(view).to receive(:params).and_return({action: :show}) + + allow(referential).to receive(:referential_read_only?){ readonly } + render template: "referentials/show", layout: "layouts/application" + end + + it "should not present edit button" do + expect(rendered).to_not have_selector("a[href=\"#{view.edit_referential_path(referential)}\"]") + end + + with_permission "referentials.update" do + it "should present edit button" do + expect(rendered).to have_selector("a[href=\"#{view.edit_referential_path(referential)}\"]") + end + + context "with a readonly referential" do + let(:readonly){ true } + it "should not present edit button" do + expect(rendered).to_not have_selector("a[href=\"#{view.edit_referential_path(referential)}\"]") + end + end + end + end diff --git a/spec/views/stop_areas/index.html.slim_spec.rb b/spec/views/stop_areas/index.html.slim_spec.rb index 520cecc1a..6e66c5ab9 100644 --- a/spec/views/stop_areas/index.html.slim_spec.rb +++ b/spec/views/stop_areas/index.html.slim_spec.rb @@ -13,6 +13,7 @@ describe "/stop_areas/index", :type => :view do allow(view).to receive(:link_with_search).and_return("#") allow(view).to receive(:collection).and_return(stop_areas) allow(view).to receive(:current_referential).and_return(stop_area_referential) + allow(view).to receive(:params).and_return({action: :index}) controller.request.path_parameters[:stop_area_referential_id] = stop_area_referential.id render end |
