diff options
Diffstat (limited to 'spec/support')
| -rw-r--r-- | spec/support/integration_spec_helper.rb | 15 | ||||
| -rw-r--r-- | spec/support/pundit/pundit_view_policy.rb | 13 | ||||
| -rw-r--r-- | spec/support/referential.rb | 4 | ||||
| -rw-r--r-- | spec/support/snapshot_support.rb | 60 | 
4 files changed, 84 insertions, 8 deletions
| diff --git a/spec/support/integration_spec_helper.rb b/spec/support/integration_spec_helper.rb index 36306559d..7ba7e9f92 100644 --- a/spec/support/integration_spec_helper.rb +++ b/spec/support/integration_spec_helper.rb @@ -3,7 +3,11 @@ module IntegrationSpecHelper    def paginate_collection klass, decorator, page=1, context={}      collection = klass.page(page)      if decorator -      collection = ModelDecorator.decorate(collection, with: decorator, context: context) +      if decorator < AF83::Decorator +        collection = decorator.decorate(collection, context: context) +      else +        collection = ModelDecorator.decorate(collection, with: decorator, context: context) +      end      end      collection    end @@ -24,6 +28,13 @@ module IntegrationSpecHelper          context('', &block) if block_given?        end      end + +    def with_feature feature, &block +      context "with feature #{feature}" do +        let(:features){ [feature] } +        context('', &block) if block_given? +      end +    end    end    def self.included into @@ -48,7 +59,7 @@ RSpec::Matchers.define :have_link_for_each_item do |collection, name, opts|    end    description { "have #{name} link for each item" }    failure_message do -    "expected view to have #{name} link for each item, failed with selector: \"#{@selector}\"" +    "expected view to have one #{name} link for each item, failed with selector: \"#{@selector}\""    end  end diff --git a/spec/support/pundit/pundit_view_policy.rb b/spec/support/pundit/pundit_view_policy.rb index 91be0624c..63970de02 100644 --- a/spec/support/pundit/pundit_view_policy.rb +++ b/spec/support/pundit/pundit_view_policy.rb @@ -2,13 +2,18 @@ module Pundit    module PunditViewPolicy      def self.included into        into.let(:permissions){ nil } -      into.let(:organisation){ referential.try(:organisation) } -      into.let(:current_referential){ referential || build_stubbed(:referential) } -      into.let(:current_user){ build_stubbed :user, permissions: permissions, organisation: organisation } +      into.let(:current_referential){ referential || build_stubbed(:referential, organisation: organisation) } +      into.let(:current_user){ create :user, permissions: permissions, organisation: organisation }        into.let(:pundit_user){ UserContext.new(current_user, referential: current_referential) } +      into.let(:current_offer_workbench) { create :workbench, organisation: organisation}        into.before do          allow(view).to receive(:pundit_user) { pundit_user } - +        allow(view).to receive(:current_user) { current_user } +        allow(view).to receive(:current_organisation).and_return(organisation) +        allow(view).to receive(:current_offer_workbench).and_return(current_offer_workbench) +        allow(view).to receive(:current_workgroup).and_return(current_offer_workbench.workgroup) +        allow(view).to receive(:has_feature?){ |f| features.include?(f)} +        allow(view).to receive(:user_signed_in?).and_return true          allow(view).to receive(:policy) do |instance|            ::Pundit.policy pundit_user, instance          end diff --git a/spec/support/referential.rb b/spec/support/referential.rb index 497ff47a8..9acdce73a 100644 --- a/spec/support/referential.rb +++ b/spec/support/referential.rb @@ -11,8 +11,8 @@ module ReferentialHelper    def self.included(base)      base.class_eval do        extend ClassMethods -      alias_method :referential, :first_referential -      alias_method :organisation, :first_organisation +      base.let(:referential){ first_referential } +      base.let(:organisation){ first_organisation }      end    end diff --git a/spec/support/snapshot_support.rb b/spec/support/snapshot_support.rb new file mode 100644 index 000000000..b1ade5288 --- /dev/null +++ b/spec/support/snapshot_support.rb @@ -0,0 +1,60 @@ +module SnaphostSpecHelper + +  module Methods +    def set_invariant expr, val=nil +      val ||= expr +      chain = expr.split(".") +      method = chain.pop + +      before(:each) do +        allow(eval(chain.join('.'))).to receive(method){ val } +      end +    end +  end + +  def self.included into +    into.extend Methods +  end +end + +RSpec.configure do |config| +  config.include SnaphostSpecHelper, type: :view +end + + +RSpec::Matchers.define :match_actions_links_snapshot do |name| +  match do |actual| +    @content = Capybara::Node::Simple.new(rendered).find('.page_header').native.inner_html +    expect(@content).to match_snapshot(name) +  end + +  failure_message do |actual| +    out = ["Snapshots did not match."] +    snap_path = File.dirname(method_missing(:class).metadata[:file_path]) + "/__snapshots__/#{name}.snap" +    temp_path = Pathname.new "#{Rails.root}/tmp/__snapshots__/#{name}.failed.snap" +    FileUtils.mkdir_p temp_path.dirname +    tmp = File.new temp_path, "w" +    tmp.write @content +    tmp.close() +    expected = File.read snap_path +    out << "Expected: #{expected}" +    out << "Actual: #{@content}" +    out << "\n\n --- DIFF ---" +    out << differ.diff_as_string(@content, expected) +    out << "\n\n --- Previews : ---" +    out << "Expected: \n" + snapshot_url(snap: snap_path, layout: :actions_links) +    out << " \nActual:  \n" + snapshot_url(snap: tmp.path, layout: :actions_links) +    out.join("\n") +  end + +  def snapshot_url snap:, layout: +    "http://localhost:3000/snap/?snap=#{URI.encode(snap.to_s)}&layout=#{URI.encode(layout.to_s)}" +  end + +  def differ +    RSpec::Support::Differ.new( +        :object_preparer => lambda { |object| RSpec::Matchers::Composable.surface_descriptions_in(object) }, +        :color => RSpec::Matchers.configuration.color? +    ) +  end +end | 
