diff options
Diffstat (limited to 'spec')
| -rw-r--r-- | spec/controllers/lines_controller_spec.rb | 38 | ||||
| -rw-r--r-- | spec/controllers/stop_area_referentials_controller_spec.rb | 2 | ||||
| -rw-r--r-- | spec/controllers/stop_areas_controller_spec.rb | 38 | ||||
| -rw-r--r-- | spec/factories/chouette_routes.rb | 7 | ||||
| -rw-r--r-- | spec/factories/chouette_stop_areas.rb | 4 | ||||
| -rw-r--r-- | spec/features/purchase_windows_permission_spec.rb | 4 | ||||
| -rw-r--r-- | spec/support/integration_spec_helper.rb | 34 | ||||
| -rw-r--r-- | spec/views/lines/index.html.erb_spec.rb | 27 | ||||
| -rw-r--r-- | spec/views/lines/index.html.slim_spec.rb | 71 | ||||
| -rw-r--r-- | spec/views/stop_areas/index.html.slim_spec.rb | 54 | ||||
| -rw-r--r-- | spec/views/vehicle_journeys/index.html.slim_spec.rb | 30 |
11 files changed, 267 insertions, 42 deletions
diff --git a/spec/controllers/lines_controller_spec.rb b/spec/controllers/lines_controller_spec.rb new file mode 100644 index 000000000..ce5adbbdd --- /dev/null +++ b/spec/controllers/lines_controller_spec.rb @@ -0,0 +1,38 @@ +RSpec.describe LinesController, :type => :controller do + login_user + + let(:line_referential) { create :line_referential } + let(:line) { create :line, line_referential: line_referential } + + describe 'PUT deactivate' do + let(:request){ put :deactivate, id: line.id, line_referential_id: line_referential.id } + + it 'should redirect to 403' do + expect(request).to redirect_to "/403" + end + + with_permission "lines.change_status" do + it 'returns HTTP success' do + expect(request).to redirect_to [line_referential, line] + expect(line.reload).to be_deactivated + end + end + end + + describe 'PUT activate' do + let(:request){ put :activate, id: line.id, line_referential_id: line_referential.id } + before(:each){ + line.deactivate! + } + it 'should redirect to 403' do + expect(request).to redirect_to "/403" + end + + with_permission "lines.change_status" do + it 'returns HTTP success' do + expect(request).to redirect_to [line_referential, line] + expect(line.reload).to be_activated + end + end + end +end diff --git a/spec/controllers/stop_area_referentials_controller_spec.rb b/spec/controllers/stop_area_referentials_controller_spec.rb index c8d7e1736..384323334 100644 --- a/spec/controllers/stop_area_referentials_controller_spec.rb +++ b/spec/controllers/stop_area_referentials_controller_spec.rb @@ -6,7 +6,7 @@ RSpec.describe StopAreaReferentialsController, :type => :controller do describe 'PUT sync' do let(:request){ put :sync, id: stop_area_referential.id } - it { request.should redirect_to "/403" } + it { expect(request).to redirect_to "/403" } with_permission "stop_area_referentials.synchronize" do it 'returns HTTP success' do diff --git a/spec/controllers/stop_areas_controller_spec.rb b/spec/controllers/stop_areas_controller_spec.rb new file mode 100644 index 000000000..2b5f8c3e2 --- /dev/null +++ b/spec/controllers/stop_areas_controller_spec.rb @@ -0,0 +1,38 @@ +RSpec.describe StopAreasController, :type => :controller do + login_user + + let(:stop_area_referential) { create :stop_area_referential } + let(:stop_area) { create :stop_area, stop_area_referential: stop_area_referential } + + describe 'PUT deactivate' do + let(:request){ put :deactivate, id: stop_area.id, stop_area_referential_id: stop_area_referential.id } + + it 'should redirect to 403' do + expect(request).to redirect_to "/403" + end + + with_permission "stop_areas.change_status" do + it 'returns HTTP success' do + expect(request).to redirect_to [stop_area_referential, stop_area] + expect(stop_area.reload).to be_deactivated + end + end + end + + describe 'PUT activate' do + let(:request){ put :activate, id: stop_area.id, stop_area_referential_id: stop_area_referential.id } + before(:each){ + stop_area.deactivate! + } + it 'should redirect to 403' do + expect(request).to redirect_to "/403" + end + + with_permission "stop_areas.change_status" do + it 'returns HTTP success' do + expect(request).to redirect_to [stop_area_referential, stop_area] + expect(stop_area.reload).to be_activated + end + end + end +end diff --git a/spec/factories/chouette_routes.rb b/spec/factories/chouette_routes.rb index 4e20059fe..7443d08bc 100644 --- a/spec/factories/chouette_routes.rb +++ b/spec/factories/chouette_routes.rb @@ -31,6 +31,13 @@ FactoryGirl.define do end end + + trait :with_opposite do + after(:create) do |route| + opposite = create :route + route.opposite_route = opposite + end + end end factory :route_with_after_commit do diff --git a/spec/factories/chouette_stop_areas.rb b/spec/factories/chouette_stop_areas.rb index 7f937e361..94517f856 100644 --- a/spec/factories/chouette_stop_areas.rb +++ b/spec/factories/chouette_stop_areas.rb @@ -8,5 +8,9 @@ FactoryGirl.define do longitude {10.0 * rand} association :stop_area_referential + + trait :deactivated do + deleted_at { 1.hour.ago } + end end end diff --git a/spec/features/purchase_windows_permission_spec.rb b/spec/features/purchase_windows_permission_spec.rb index e74fb5c17..9f155a1e8 100644 --- a/spec/features/purchase_windows_permission_spec.rb +++ b/spec/features/purchase_windows_permission_spec.rb @@ -4,6 +4,10 @@ require 'spec_helper' describe "PurchaseWindows", :type => :feature do login_user + before do + @user.organisation.update features: %w{purchase_windows} + end + let(:purchase_window) { create :purchase_window, referential: first_referential} describe 'permissions' do diff --git a/spec/support/integration_spec_helper.rb b/spec/support/integration_spec_helper.rb index 78efb9027..36306559d 100644 --- a/spec/support/integration_spec_helper.rb +++ b/spec/support/integration_spec_helper.rb @@ -1,15 +1,20 @@ module IntegrationSpecHelper - def paginate_collection klass, decorator, page=1 - ModelDecorator.decorate( klass.page(page), with: decorator ) + def paginate_collection klass, decorator, page=1, context={} + collection = klass.page(page) + if decorator + collection = ModelDecorator.decorate(collection, with: decorator, context: context) + end + collection end def build_paginated_collection factory, decorator, opts={} + context = opts.delete(:context) || {} count = opts.delete(:count) || 2 page = opts.delete(:page) || 1 klass = nil - count.times { klass ||= create(factory, opts).class } - paginate_collection klass, decorator, page + count.times { klass = create(factory, opts).class } + paginate_collection klass, decorator, page, context end module Methods @@ -30,20 +35,33 @@ RSpec.configure do |config| config.include IntegrationSpecHelper, type: :view end -RSpec::Matchers.define :have_link_for_each_item do |collection, name, href| +RSpec::Matchers.define :have_link_for_each_item do |collection, name, opts| + opts = {href: opts} unless opts.is_a? Hash + href = opts[:href] + method = opts[:method] + method_selector = method.present? ? "[data-method='#{method.downcase}']": "" 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) + @selector = "tr.#{TableBuilderHelper.item_row_class_name(collection)}-#{item.id} .actions a[href='#{href.call(item)}']#{method_selector}" + expect(rendered).to have_selector(@selector, count: 1) end end description { "have #{name} link for each item" } + failure_message do + "expected view to have #{name} link for each item, failed with selector: \"#{@selector}\"" + end end RSpec::Matchers.define :have_the_right_number_of_links do |collection, count| - match do |actual| + match do collection.each do |item| - expect(rendered).to have_selector("tr.#{TableBuilderHelper.item_row_class_name(collection)}-#{item.id} .actions a", count: count) + @selector = "tr.#{TableBuilderHelper.item_row_class_name(collection)}-#{item.id} .actions a" + expect(rendered).to have_selector(@selector, count: count) end end description { "have #{count} links for each item" } + failure_message do + actual = Capybara::Node::Simple.new(rendered).all(@selector).count + "expected #{count} links for each item, got #{actual} for \"#{@selector}\"" + end end diff --git a/spec/views/lines/index.html.erb_spec.rb b/spec/views/lines/index.html.erb_spec.rb deleted file mode 100644 index dbc3cbdb7..000000000 --- a/spec/views/lines/index.html.erb_spec.rb +++ /dev/null @@ -1,27 +0,0 @@ -require 'spec_helper' - -describe "/lines/index", :type => :view do - - let!(:line_referential) { assign :line_referential, create(:line_referential) } - let!(:network) { create :network } - let!(:company) { create :company } - let!(:lines) { assign :lines, Array.new(2) { create(:line, line_referential: line_referential, network: network, company: company) }.paginate } - let!(:q) { assign :q, Ransack::Search.new(Chouette::Line) } - - before :each do - allow(view).to receive(:link_with_search).and_return("#") - end - - # it "should render a show link for each group" do - # render - # lines.each do |line| - # expect(rendered).to have_selector(".line a[href='#{view.line_referential_line_path(line_referential, line)}']", :text => line.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_line_referential_line_path(line_referential)}']") - # end - -end diff --git a/spec/views/lines/index.html.slim_spec.rb b/spec/views/lines/index.html.slim_spec.rb new file mode 100644 index 000000000..498784912 --- /dev/null +++ b/spec/views/lines/index.html.slim_spec.rb @@ -0,0 +1,71 @@ +require 'spec_helper' + +describe "/lines/index", :type => :view do + let(:deactivated_line){ nil } + let(:line_referential) { assign :line_referential, create(:line_referential) } + let(:current_organisation) { current_user.organisation } + let(:context) { + { + current_organisation: current_organisation, + line_referential: line_referential + } + } + let(:lines) do + assign :lines, build_paginated_collection(:line, LineDecorator, line_referential: line_referential, context: context) + end + let!(:q) { assign :q, Ransack::Search.new(Chouette::Line) } + + before :each do + deactivated_line + allow(view).to receive(:collection).and_return(lines) + allow(view).to receive(:current_referential).and_return(line_referential) + controller.request.path_parameters[:line_referential_id] = line_referential.id + render + end + + common_items = ->{ + it { should have_link_for_each_item(lines, "show", -> (line){ view.line_referential_line_path(line_referential, line) }) } + it { should have_link_for_each_item(lines, "network", -> (line){ view.line_referential_network_path(line_referential, line.network) }) } + it { should have_link_for_each_item(lines, "company", -> (line){ view.line_referential_company_path(line_referential, line.company) }) } + } + + common_items.call() + it { should have_the_right_number_of_links(lines, 3) } + + with_permission "lines.change_status" do + common_items.call() + it { should have_link_for_each_item(lines, "deactivate", -> (line){ view.deactivate_line_referential_line_path(line_referential, line) }) } + it { should have_the_right_number_of_links(lines, 4) } + end + + with_permission "lines.destroy" do + common_items.call() + it { + should have_link_for_each_item(lines, "destroy", { + href: ->(line){ view.line_referential_line_path(line_referential, line)}, + method: :delete + }) + } + it { should have_the_right_number_of_links(lines, 4) } + end + + context "with a deactivated item" do + with_permission "lines.change_status" do + let(:deactivated_line){ create :line, deactivated: true } + + common_items.call() + it "should display an activate link for the deactivated one" do + lines.each do |line| + if line == deactivated_line + href = view.activate_line_referential_line_path(line_referential, line) + else + href = view.deactivate_line_referential_line_path(line_referential, line) + end + selector = "tr.#{TableBuilderHelper.item_row_class_name(lines)}-#{line.id} .actions a[href='#{href}']" + expect(rendered).to have_selector(selector, count: 1) + end + end + it { should have_the_right_number_of_links(lines, 4) } + 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 8daa5eb4b..520cecc1a 100644 --- a/spec/views/stop_areas/index.html.slim_spec.rb +++ b/spec/views/stop_areas/index.html.slim_spec.rb @@ -1,14 +1,15 @@ 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) do + let(:deactivated_stop_area){ nil } + let(:stop_area_referential) { assign :stop_area_referential, create(:stop_area_referential) } + let(:stop_areas) do 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) } before :each do + deactivated_stop_area 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) @@ -16,19 +17,60 @@ describe "/stop_areas/index", :type => :view do render end - it { should have_link_for_each_item(stop_areas, "show", -> (stop_area){ view.stop_area_referential_stop_area_path(stop_area_referential, stop_area) }) } + common_items = ->{ + it { should have_link_for_each_item(stop_areas, "show", -> (stop_area){ view.stop_area_referential_stop_area_path(stop_area_referential, stop_area) }) } + } + + common_items.call() it { should have_the_right_number_of_links(stop_areas, 1) } with_permission "stop_areas.create" do - it { should have_link_for_each_item(stop_areas, "show", -> (stop_area){ view.stop_area_referential_stop_area_path(stop_area_referential, stop_area) }) } + common_items.call() it { should_not have_link_for_each_item(stop_areas, "create", -> (stop_area){ view.new_stop_area_referential_stop_area_path(stop_area_referential) }) } it { should have_the_right_number_of_links(stop_areas, 1) } end with_permission "stop_areas.update" do - it { should have_link_for_each_item(stop_areas, "show", -> (stop_area){ view.stop_area_referential_stop_area_path(stop_area_referential, stop_area) }) } + common_items.call() 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 + with_permission "stop_areas.change_status" do + common_items.call() + it { should have_link_for_each_item(stop_areas, "deactivate", -> (stop_area){ view.deactivate_stop_area_referential_stop_area_path(stop_area_referential, stop_area) }) } + it { should have_the_right_number_of_links(stop_areas, 2) } + end + + with_permission "stop_areas.destroy" do + common_items.call() + it { + should have_link_for_each_item(stop_areas, "destroy", { + href: ->(stop_area){ view.stop_area_referential_stop_area_path(stop_area_referential, stop_area)}, + method: :delete + }) + } + it { should have_the_right_number_of_links(stop_areas, 2) } + end + + context "with a deactivated item" do + with_permission "stop_areas.change_status" do + let(:deactivated_stop_area){ create :stop_area, :deactivated, stop_area_referential: stop_area_referential } + + common_items.call() + it "should display an activate link for the deactivated one" do + stop_areas.each do |stop_area| + if stop_area == deactivated_stop_area + href = view.activate_stop_area_referential_stop_area_path(stop_area_referential, stop_area) + else + href = view.deactivate_stop_area_referential_stop_area_path(stop_area_referential, stop_area) + end + selector = "tr.#{TableBuilderHelper.item_row_class_name(stop_areas)}-#{stop_area.id} .actions a[href='#{href}']" + expect(rendered).to have_selector(selector, count: 1) + end + end + it { should have_the_right_number_of_links(stop_areas, 2) } + end + end + end diff --git a/spec/views/vehicle_journeys/index.html.slim_spec.rb b/spec/views/vehicle_journeys/index.html.slim_spec.rb new file mode 100644 index 000000000..7f0a9c5aa --- /dev/null +++ b/spec/views/vehicle_journeys/index.html.slim_spec.rb @@ -0,0 +1,30 @@ +require 'spec_helper' + +describe "/vehicle_journeys/index", :type => :view do + + let!(:referential) { assign :referential, create(:referential) } + let!(:line) { assign :line, create(:line) } + let!(:route) { assign :route, create(:route, line: line) } + let!(:vehicle_journeys) do + assign :vehicle_journeys, build_paginated_collection(:vehicle_journey, nil, route: route) + end + + before :each do + allow(view).to receive(:link_with_search).and_return("#") + allow(view).to receive(:collection).and_return(vehicle_journeys) + allow(view).to receive(:current_referential).and_return(referential) + controller.request.path_parameters[:referential_id] = referential.id + render + end + + context "with an opposite_route" do + let!(:route) { assign :route, create(:route, :with_opposite, line: line) } + + it "should have an 'oppposite route timetable' button" do + href = view.referential_line_route_vehicle_journeys_path(referential, line, route.opposite_route) + oppposite_button_selector = "a[href=\"#{href}\"]" + + expect(view.content_for(:page_header_content)).to have_selector oppposite_button_selector + end + end +end |
