From 2736ff5e010586266176a9a0fb0ac8dc6e227f63 Mon Sep 17 00:00:00 2001 From: Zog Date: Fri, 15 Dec 2017 17:01:49 +0100 Subject: Ref #5291@2h; Specs setup - Refactor specs helper to be more generic - Write missing specs for the Workbenches#show view We now have failing tests highlighting the bug --- app/helpers/table_builder_helper.rb | 20 +++++++---- spec/support/integration_spec_helper.rb | 30 ++++++++++++++++ spec/views/offer_workbenches/show.html.erb_spec.rb | 41 ++++++++++++++++++++-- spec/views/stop_areas/index.html.slim_spec.rb | 27 +++----------- 4 files changed, 87 insertions(+), 31 deletions(-) diff --git a/app/helpers/table_builder_helper.rb b/app/helpers/table_builder_helper.rb index 64bec6bae..59906dc87 100644 --- a/app/helpers/table_builder_helper.rb +++ b/app/helpers/table_builder_helper.rb @@ -95,6 +95,18 @@ module TableBuilderHelper class: cls end + def self.item_row_class_name collection + if collection.respond_to?(:model) + model_name = collection.model.name + elsif collection.respond_to?(:first) + model_name = collection.first.class.name + else + model_name = "item" + end + + model_name.split("::").last.parameterize + end + private def thead(collection, columns, sortable, selectable, has_links, overhead, model ) @@ -188,15 +200,11 @@ 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 + model_name = TableBuilderHelper.item_row_class_name collection content_tag :tbody do collection.map do |item| - klass = "#{model_name.parameterize}-#{item.id}" + klass = "#{model_name}-#{item.id}" content_tag :tr, class: klass do bcont = [] diff --git a/spec/support/integration_spec_helper.rb b/spec/support/integration_spec_helper.rb index 182cadf24..5bcf0bd3a 100644 --- a/spec/support/integration_spec_helper.rb +++ b/spec/support/integration_spec_helper.rb @@ -4,9 +4,39 @@ module IntegrationSpecHelper let(:permissions){ [permission] } context('', &block) if block_given? end + + def paginate_collection klass, decorator, page=1 + ModelDecorator.decorate( klass.page(page), with: decorator ) + end + + def build_paginated_collection factory, decorator, opts={} + count = opts.delete(:count) || 2 + page = opts.delete(:page) || 1 + klass = nil + count.times { klass ||= create(factory, opts).class } + paginate_collection klass, decorator, page + end end end RSpec.configure do |config| config.extend IntegrationSpecHelper, type: :view end + +RSpec::Matchers.define :have_link_for_each_item do |collection, name, href| + match do |actual| + collection.each do |item| + expect(rendered).to have_selector("tr.#{TableBuilderHelper.item_row_class_name(collection)}-#{item.id} .actions a[href='#{href.call(item)}']", count: 1) + end + end + description { "have #{name} link for each item" } +end + +RSpec::Matchers.define :have_the_right_number_of_links do |collection, count| + match do |actual| + collection.each do |item| + expect(rendered).to have_selector("tr.#{TableBuilderHelper.item_row_class_name(collection)}-#{item.id} .actions a", count: count) + end + end + description { "have #{count} links for each item" } +end diff --git a/spec/views/offer_workbenches/show.html.erb_spec.rb b/spec/views/offer_workbenches/show.html.erb_spec.rb index 40b09268a..597335166 100644 --- a/spec/views/offer_workbenches/show.html.erb_spec.rb +++ b/spec/views/offer_workbenches/show.html.erb_spec.rb @@ -1,5 +1,42 @@ -require 'rails_helper' +require 'spec_helper' -RSpec.describe "workbenches/show.html.erb", :type => :view do +describe "workbenches/show", :type => :view do + let!(:ids) { ['STIF:CODIFLIGNE:Line:C00840', 'STIF:CODIFLIGNE:Line:C00086'] } + let!(:lines) { + ids.map do |id| + create :line, objectid: id, line_referential: workbench.line_referential + end + } + let!(:workbench){ assign :workbench, create(:workbench) } + let!(:same_organisation_referential){ create :workbench_referential, workbench: workbench, metadatas: [create(:referential_metadata, lines: lines)] } + let!(:different_organisation_referential){ create :workbench_referential, metadatas: [create(:referential_metadata, lines: lines)] } + let!(:referentials){ + same_organisation_referential && different_organisation_referential + assign :wbench_refs, paginate_collection(Referential, ReferentialDecorator) + } + let!(:q) { assign :q_for_form, Ransack::Search.new(Referential) } + before :each do + + lines + controller.request.path_parameters[:id] = workbench.id + expect(workbench.referentials).to include same_organisation_referential + expect(workbench.referentials).to_not include different_organisation_referential + expect(workbench.all_referentials).to include same_organisation_referential + expect(workbench.all_referentials).to include different_organisation_referential + render + end + + it { should have_link_for_each_item(referentials, "show", -> (referential){ view.referential_path(referential) }) } + it "should enable the checkbox for the referential which belongs to the same organisation" do + klass = "#{TableBuilderHelper.item_row_class_name(referentials)}-#{same_organisation_referential.id}" + selector = "tr.#{klass} [type=checkbox][value='#{same_organisation_referential.id}']:not([disabled])" + expect(rendered).to have_selector(selector, count: 1) + end + + it "should disable the checkbox for the referential which does not belong to the same organisation" do + klass = "#{TableBuilderHelper.item_row_class_name(referentials)}-#{different_organisation_referential.id}" + selector = "tr.#{klass} [type=checkbox][disabled][value='#{different_organisation_referential.id}']" + expect(rendered).to have_selector(selector, count: 1) + 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 64c958879..294acfe1a 100644 --- a/spec/views/stop_areas/index.html.slim_spec.rb +++ b/spec/views/stop_areas/index.html.slim_spec.rb @@ -1,29 +1,10 @@ require 'spec_helper' -RSpec::Matchers.define :have_link_for_each_stop_area do |stop_areas, name, href| - match do |actual| - stop_areas.each do |stop_area| - expect(rendered).to have_selector("tr.stoparea-#{stop_area.id} .actions a[href='#{href.call(stop_area)}']", count: 1) - end - end - description { "have #{name} link for each stop area" } -end - -RSpec::Matchers.define :have_the_right_number_of_links do |stop_areas, count| - match do |actual| - stop_areas.each do |stop_area| - expect(rendered).to have_selector("tr.stoparea-#{stop_area.id} .actions a", count: count) - end - end - description { "have #{count} links for each stop area" } -end - describe "/stop_areas/index", :type => :view do let!(:stop_area_referential) { assign :stop_area_referential, create(:stop_area_referential) } 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 ) + assign :stop_areas, build_paginated_collection(:stop_area, StopAreaDecorator, stop_area_referential: stop_area_referential) end let!(:q) { assign :q, Ransack::Search.new(Chouette::StopArea) } @@ -35,7 +16,7 @@ describe "/stop_areas/index", :type => :view do render end - it { should have_link_for_each_stop_area(stop_areas, "show", -> (stop_area){ view.stop_area_referential_stop_area_path(stop_area_referential, stop_area) }) } + it { should have_link_for_each_item(stop_areas, "show", -> (stop_area){ view.stop_area_referential_stop_area_path(stop_area_referential, stop_area) }) } it { should have_the_right_number_of_links(stop_areas, 1) } with_permission "stop_areas.create" do @@ -45,8 +26,8 @@ describe "/stop_areas/index", :type => :view do end with_permission "stop_areas.update" do - it { should have_link_for_each_stop_area(stop_areas, "show", -> (stop_area){ view.stop_area_referential_stop_area_path(stop_area_referential, stop_area) }) } - it { should have_link_for_each_stop_area(stop_areas, "edit", -> (stop_area){ view.edit_stop_area_referential_stop_area_path(stop_area_referential, stop_area) }) } + it { should have_link_for_each_item(stop_areas, "show", -> (stop_area){ view.stop_area_referential_stop_area_path(stop_area_referential, stop_area) }) } + it { should have_link_for_each_item(stop_areas, "edit", -> (stop_area){ view.edit_stop_area_referential_stop_area_path(stop_area_referential, stop_area) }) } it { should have_the_right_number_of_links(stop_areas, 2) } end -- cgit v1.2.3