diff options
| author | Zog | 2017-12-15 09:11:04 +0100 |
|---|---|---|
| committer | Zog | 2017-12-19 14:40:38 +0100 |
| commit | 676f45e8fd85c1422345d4d27ba2385e4bd536fe (patch) | |
| tree | e09fcebd3f7970b0043bf8adbfd5e0f54a8c73e1 | |
| parent | 8e55e7036aafb485058bf9122bf47cde73a54c33 (diff) | |
| download | chouette-core-676f45e8fd85c1422345d4d27ba2385e4bd536fe.tar.bz2 | |
Refs #5287;
Add specs for the view.
Refactor to come
| -rw-r--r-- | app/helpers/table_builder_helper.rb | 10 | ||||
| -rw-r--r-- | spec/helpers/table_builder_helper_spec.rb | 6 | ||||
| -rw-r--r-- | spec/support/integration_spec_helper.rb | 17 | ||||
| -rw-r--r-- | spec/support/pundit/pundit_view_policy.rb | 16 | ||||
| -rw-r--r-- | spec/views/stop_areas/index.html.erb_spec.rb | 78 |
5 files changed, 102 insertions, 25 deletions
diff --git a/app/helpers/table_builder_helper.rb b/app/helpers/table_builder_helper.rb index 37f01ce0d..64bec6bae 100644 --- a/app/helpers/table_builder_helper.rb +++ b/app/helpers/table_builder_helper.rb @@ -188,10 +188,16 @@ module TableBuilderHelper end def tbody(collection, columns, selectable, links, overhead) + if collection.respond_to?(:model) + model_name = collection.model.name.split("::").last + else + model_name = "item" + end + content_tag :tbody do collection.map do |item| - - content_tag :tr do + klass = "#{model_name.parameterize}-#{item.id}" + content_tag :tr, class: klass do bcont = [] if selectable diff --git a/spec/helpers/table_builder_helper_spec.rb b/spec/helpers/table_builder_helper_spec.rb index 3b0a18379..3b3504c60 100644 --- a/spec/helpers/table_builder_helper_spec.rb +++ b/spec/helpers/table_builder_helper_spec.rb @@ -59,7 +59,7 @@ describe TableBuilderHelper, type: :helper do </tr> </thead> <tbody> - <tr> + <tr class="referential-#{referential.id}"> <td> <div class="checkbox"><input type="checkbox" name="#{referential.id}" id="#{referential.id}" value="#{referential.id}" /><label for="#{referential.id}"></label></div> </td> @@ -213,7 +213,7 @@ describe TableBuilderHelper, type: :helper do </tr> </thead> <tbody> - <tr> + <tr class="company-#{company.id}"> <td>#{company.get_objectid.local_id}</td> <td title="Voir"><a href="/referentials/#{referential.id}/companies/#{company.id}">#{company.name}</a></td> <td></td> @@ -326,7 +326,7 @@ describe TableBuilderHelper, type: :helper do </tr> </thead> <tbody> - <tr> + <tr class="company-#{company.id}"> <td>#{company.get_objectid.local_id}</td> <td title="Voir"><a href="/referentials/#{referential.id}/companies/#{company.id}">#{company.name}</a></td> <td></td> diff --git a/spec/support/integration_spec_helper.rb b/spec/support/integration_spec_helper.rb new file mode 100644 index 000000000..958aab9d5 --- /dev/null +++ b/spec/support/integration_spec_helper.rb @@ -0,0 +1,17 @@ +module IntegrationSpecHelper + extend ActiveSupport::Concern + + included do + def self.with_permission permission, &block + context "with permission #{permission}" do + let(:permissions){ [permission] } + context('', &block) if block_given? + end + end + end +end + + +RSpec.configure do |config| + config.include IntegrationSpecHelper, type: :view +end diff --git a/spec/support/pundit/pundit_view_policy.rb b/spec/support/pundit/pundit_view_policy.rb index b8434cac0..02a78a4e0 100644 --- a/spec/support/pundit/pundit_view_policy.rb +++ b/spec/support/pundit/pundit_view_policy.rb @@ -3,14 +3,16 @@ module Pundit extend ActiveSupport::Concern included do + + let(:permissions){ nil } + let(:current_referential){ build_stubbed :referential } + let(:current_user){ build_stubbed :user, permissions: permissions } + let(:pundit_user){ UserContext.new(current_user, referential: current_referential) } before do - controller.singleton_class.class_eval do - def policy(instance) - Class.new do - def method_missing(*args, &block); true; end - end.new - end - helper_method :policy + allow(view).to receive(:pundit_user) { pundit_user } + + allow(view).to receive(:policy) do |instance| + ::Pundit.policy pundit_user, instance end end end diff --git a/spec/views/stop_areas/index.html.erb_spec.rb b/spec/views/stop_areas/index.html.erb_spec.rb index 2dfae1bfd..a354586d2 100644 --- a/spec/views/stop_areas/index.html.erb_spec.rb +++ b/spec/views/stop_areas/index.html.erb_spec.rb @@ -1,25 +1,77 @@ require 'spec_helper' describe "/stop_areas/index", :type => :view do - let!(:stop_area_referential) { assign :stop_area_referential, create(:stop_area_referential) } - let!(:stop_areas) { assign :stop_areas, Array.new(2) { create(:stop_area, stop_area_referential: stop_area_referential) }.paginate } + let!(:stop_areas) do + 2.times { create(:stop_area, stop_area_referential: stop_area_referential) } + assign :stop_areas, ModelDecorator.decorate( Chouette::StopArea.page(1), with: StopAreaDecorator ) + end let!(:q) { assign :q, Ransack::Search.new(Chouette::StopArea) } before :each 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) + controller.request.path_parameters[:stop_area_referential_id] = stop_area_referential.id + render + end + + it "should render a row for each group" do + stop_areas.each do |stop_area| + expect(rendered).to have_selector("tr.stoparea-#{stop_area.id}", count: 1) + end + end + + it "should render a show link for each group" do + stop_areas.each do |stop_area| + expect(rendered).to have_selector("tr.stoparea-#{stop_area.id} .actions a[href='#{view.stop_area_referential_stop_area_path(stop_area_referential, stop_area)}']", count: 1) + end + end + + it "should render no other link for each group" do + stop_areas.each do |stop_area| + expect(rendered).to have_selector("tr.stoparea-#{stop_area.id} .actions a", count: 1) + end end - # it "should render a show link for each group" do - # render - # stop_areas.each do |stop_area| - # expect(rendered).to have_selector(".stop_area a[href='#{view.stop_area_referential_stop_area_path(stop_area_referential, stop_area)}']", :text => stop_area.name) - # end - # end - # - # it "should render a link to create a new group" do - # render - # expect(view.content_for(:sidebar)).to have_selector(".actions a[href='#{new_stop_area_referential_stop_area_path(stop_area_referential)}']") - # end + with_permission "stop_areas.create" do + it "should render a show link for each group" do + stop_areas.each do |stop_area| + expect(rendered).to have_selector("tr.stoparea-#{stop_area.id} .actions a[href='#{view.stop_area_referential_stop_area_path(stop_area_referential, stop_area)}']", count: 1) + end + end + + it "should render a create link for each group" do + stop_areas.each do |stop_area| + expect(rendered).to have_selector("tr.stoparea-#{stop_area.id} .actions a[href='#{view.new_stop_area_referential_stop_area_path(stop_area_referential)}']", count: 1) + end + end + + it "should render no other link for each group" do + stop_areas.each do |stop_area| + expect(rendered).to have_selector("tr.stoparea-#{stop_area.id} .actions a", count: 2) + end + end + end + + with_permission "stop_areas.update" do + it "should render a show link for each group" do + stop_areas.each do |stop_area| + expect(rendered).to have_selector("tr.stoparea-#{stop_area.id} .actions a[href='#{view.stop_area_referential_stop_area_path(stop_area_referential, stop_area)}']", count: 1) + end + end + + it "should render a edit link for each group" do + stop_areas.each do |stop_area| + expect(rendered).to have_selector("tr.stoparea-#{stop_area.id} .actions a[href='#{view.edit_stop_area_referential_stop_area_path(stop_area_referential, stop_area.id)}']", count: 1) + end + end + + it "should render no other link for each group" do + stop_areas.each do |stop_area| + expect(rendered).to have_selector("tr.stoparea-#{stop_area.id} .actions a", count: 2) + end + end + end end |
