aboutsummaryrefslogtreecommitdiffstats
path: root/spec
diff options
context:
space:
mode:
Diffstat (limited to 'spec')
-rw-r--r--spec/helpers/table_builder_helper_spec.rb81
-rw-r--r--spec/support/integration_spec_helper.rb47
-rw-r--r--spec/views/offer_workbenches/show.html.erb_spec.rb55
-rw-r--r--spec/views/stop_areas/index.html.slim_spec.rb27
4 files changed, 180 insertions, 30 deletions
diff --git a/spec/helpers/table_builder_helper_spec.rb b/spec/helpers/table_builder_helper_spec.rb
index 3b3504c60..83b746d4b 100644
--- a/spec/helpers/table_builder_helper_spec.rb
+++ b/spec/helpers/table_builder_helper_spec.rb
@@ -381,5 +381,86 @@ describe TableBuilderHelper, type: :helper do
expect(beautified_html).to eq(expected.chomp)
end
+
+ context "on a single row" do
+ let(:referential){ build_stubbed :referential }
+ let(:other_referential){ build_stubbed :referential }
+ let(:user_context){
+ UserContext.new(
+ build_stubbed(
+ :user,
+ organisation: referential.organisation,
+ permissions: [
+ 'referentials.create',
+ 'referentials.update',
+ 'referentials.destroy',
+ ]
+ ),
+ referential: referential
+ )
+ }
+ let(:columns){
+ [
+ TableBuilderHelper::Column.new(
+ key: :name,
+ attribute: 'name'
+ ),
+ ]
+ }
+ let(:item){ referential.decorate }
+ let(:other_item){ other_referential.decorate }
+ let(:selectable){ false }
+ let(:links){ [:show] }
+ let(:overhead){ [] }
+ let(:model_name){ "referential" }
+ let(:other_tr){ helper.send(:tr, other_item, columns, selectable, links, overhead, model_name) }
+ let(:items){ [item, other_item] }
+
+ before(:each){
+ allow(helper).to receive(:current_user).and_return(user_context)
+ }
+
+ context "with all rows non-selectable" 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)
+ klass = "#{TableBuilderHelper.item_row_class_name([referential])}-#{i.id}"
+ selector = "tr.#{klass} [type=checkbox]"
+ expect(tr).to_not have_selector selector
+ end
+ end
+ end
+
+ context "with all rows selectable" 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)
+ klass = "#{TableBuilderHelper.item_row_class_name([referential])}-#{i.id}"
+ selector = "tr.#{klass} [type=checkbox]"
+ expect(tr).to have_selector selector
+ end
+ end
+ end
+
+ context "with THIS row non selectable" 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)
+ 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)
+ klass = "#{TableBuilderHelper.item_row_class_name([referential])}-#{item.id}"
+ selector = "tr.#{klass} [type=checkbox][disabled]"
+ expect(tr).to have_selector selector
+ end
+ end
+ end
end
end
diff --git a/spec/support/integration_spec_helper.rb b/spec/support/integration_spec_helper.rb
index 182cadf24..78efb9027 100644
--- a/spec/support/integration_spec_helper.rb
+++ b/spec/support/integration_spec_helper.rb
@@ -1,12 +1,49 @@
module IntegrationSpecHelper
- def with_permission permission, &block
- context "with permission #{permission}" do
- let(:permissions){ [permission] }
- context('', &block) if block_given?
+
+ 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
+
+ module Methods
+ def with_permission permission, &block
+ context "with permission #{permission}" do
+ let(:permissions){ [permission] }
+ context('', &block) if block_given?
+ end
end
end
+
+ def self.included into
+ into.extend Methods
+ end
end
RSpec.configure do |config|
- config.extend IntegrationSpecHelper, type: :view
+ config.include 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..138a1560d 100644
--- a/spec/views/offer_workbenches/show.html.erb_spec.rb
+++ b/spec/views/offer_workbenches/show.html.erb_spec.rb
@@ -1,5 +1,56 @@
-require 'rails_helper'
+require 'spec_helper'
-RSpec.describe "workbenches/show.html.erb", :type => :view do
+RSpec::Matchers.define :have_box_for_item do |item, disabled|
+ match do |actual|
+ klass = "#{TableBuilderHelper.item_row_class_name([item])}-#{item.id}"
+ if disabled
+ selector = "tr.#{klass} [type=checkbox][disabled][value='#{item.id}']"
+ else
+ selector = "tr.#{klass} [type=checkbox][value='#{item.id}']:not([disabled])"
+ end
+ expect(actual).to have_selector(selector, count: 1)
+ end
+ description { "have a #{disabled ? "disabled ": ""}box for the item ##{item.id}" }
+end
+
+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) }) }
+
+ context "without permission" do
+ it "should disable all the checkboxes" do
+ expect(rendered).to have_box_for_item same_organisation_referential, false
+ expect(rendered).to have_box_for_item different_organisation_referential, true
+ end
+ end
+ with_permission "referentials.destroy" do
+ it "should enable the checkbox for the referential which belongs to the same organisation and disable the other one" do
+ expect(rendered).to have_box_for_item same_organisation_referential, false
+ expect(rendered).to have_box_for_item different_organisation_referential, true
+ 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 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