diff options
| author | Zog | 2017-12-20 12:58:20 +0100 |
|---|---|---|
| committer | Alban Peignier | 2017-12-21 21:19:27 +0100 |
| commit | 9a34cb48f49df574ae1e7c599713ed246e2938cf (patch) | |
| tree | 49c91cfdaa645f39363c3e8c492b86a0dde08c89 /spec | |
| parent | 8cd9b9ee5fc127b2f39a9c89c71327ab8d5e9cec (diff) | |
| download | chouette-core-9a34cb48f49df574ae1e7c599713ed246e2938cf.tar.bz2 | |
Refs #5430 @2h; Deactivate lines instead of destroying them
- Add `activate` and `deactivate` actions in `LinesController`, as well
as corresponding routes
- Add `activate!` and `deactivate!` methods in `Chouette::Line`, as well
as `activated?`
- Add `activate?` and `deactivate?` permissions in `LinePolicy`
- Add corresponding `action_links`in the Decorator
- Create helper for these actions
- Add an optional `'extra_class` to the Links
- Update styles for ".delete-action" to handle the case where there are
several
- Add I18n keys accordingly
Diffstat (limited to 'spec')
| -rw-r--r-- | spec/controllers/lines_controller_spec.rb | 38 | ||||
| -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 |
4 files changed, 133 insertions, 37 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/support/integration_spec_helper.rb b/spec/support/integration_spec_helper.rb index 1bf211fe1..36306559d 100644 --- a/spec/support/integration_spec_helper.rb +++ b/spec/support/integration_spec_helper.rb @@ -1,19 +1,20 @@ module IntegrationSpecHelper - def paginate_collection klass, decorator, page=1 - coll = klass.page(page) + def paginate_collection klass, decorator, page=1, context={} + collection = klass.page(page) if decorator - coll = ModelDecorator.decorate( coll, with: decorator ) + collection = ModelDecorator.decorate(collection, with: decorator, context: context) end - coll + 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 @@ -34,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 |
