PageRenderTime 8ms CodeModel.GetById 1ms app.highlight 5ms RepoModel.GetById 1ms app.codeStats 0ms

/spec/rails_best_practices/reviews/simplify_render_in_views_review_spec.rb

http://github.com/flyerhzm/rails_best_practices
Ruby | 105 lines | 91 code | 13 blank | 1 comment | 0 complexity | 772e3f593cb810b14103237e5da8edc0 MD5 | raw file
  1# frozen_string_literal: true
  2
  3require 'spec_helper'
  4
  5module RailsBestPractices
  6  module Reviews
  7    describe SimplifyRenderInViewsReview do
  8      let(:runner) { Core::Runner.new(reviews: described_class.new) }
  9
 10      it 'simplifies render simple partial' do
 11        content = <<-EOF
 12        <%= render partial: 'sidebar' %>
 13        EOF
 14        runner.review('app/views/posts/index.html.erb', content)
 15        expect(runner.errors.size).to eq(1)
 16        expect(runner.errors[0].to_s).to eq('app/views/posts/index.html.erb:1 - simplify render in views')
 17      end
 18
 19      it 'simplifies render partial with object' do
 20        content = <<-EOF
 21        <%= render partial: 'post', object: @post %>
 22        EOF
 23        runner.review('app/views/posts/index.html.erb', content)
 24        expect(runner.errors.size).to eq(1)
 25        expect(runner.errors[0].to_s).to eq('app/views/posts/index.html.erb:1 - simplify render in views')
 26      end
 27
 28      it 'simplifies render partial with collection' do
 29        content = <<-EOF
 30        <%= render partial: 'posts', collection: @posts %>
 31        EOF
 32        runner.review('app/views/posts/index.html.erb', content)
 33        expect(runner.errors.size).to eq(1)
 34        expect(runner.errors[0].to_s).to eq('app/views/posts/index.html.erb:1 - simplify render in views')
 35      end
 36
 37      it 'simplifies render partial with local variables' do
 38        content = <<-EOF
 39        <%= render partial: 'comment', locals: { parent: post } %>
 40        EOF
 41        runner.review('app/views/posts/index.html.erb', content)
 42        expect(runner.errors.size).to eq(1)
 43        expect(runner.errors[0].to_s).to eq('app/views/posts/index.html.erb:1 - simplify render in views')
 44      end
 45
 46      it 'does not simplify render simple partial' do
 47        content = <<-EOF
 48        <%= render 'sidebar' %>
 49        <%= render 'shared/sidebar' %>
 50        EOF
 51        runner.review('app/views/posts/index.html.erb', content)
 52        expect(runner.errors.size).to eq(0)
 53      end
 54
 55      it 'does not simplify render partial with object' do
 56        content = <<-EOF
 57        <%= render @post %>
 58        EOF
 59        runner.review('app/views/posts/index.html.erb', content)
 60        expect(runner.errors.size).to eq(0)
 61      end
 62
 63      it 'does not simplify render partial with collection' do
 64        content = <<-EOF
 65        <%= render @posts %>
 66        EOF
 67        runner.review('app/views/posts/index.html.erb', content)
 68        expect(runner.errors.size).to eq(0)
 69      end
 70
 71      it 'does not simplify render partial with local variables' do
 72        content = <<-EOF
 73        <%= render 'comment', parent: post %>
 74        EOF
 75        runner.review('app/views/posts/index.html.erb', content)
 76        expect(runner.errors.size).to eq(0)
 77      end
 78
 79      it 'does not simplify render partial with complex partial' do
 80        content = <<-EOF
 81        <%= render partial: 'shared/post', object: @post %>
 82        EOF
 83        runner.review('app/views/posts/index.html.erb', content)
 84        expect(runner.errors.size).to eq(0)
 85      end
 86
 87      it 'does not simplify render partial with layout option' do
 88        content = <<-EOF
 89        <%= render partial: 'post', layout: 'post' %>
 90        EOF
 91        runner.review('app/views/posts/index.html.erb', content)
 92        expect(runner.errors.size).to eq(0)
 93      end
 94
 95      it 'does not check ignored files' do
 96        runner = Core::Runner.new(reviews: described_class.new(ignored_files: %r{views/posts/index}))
 97        content = <<-EOF
 98        <%= render partial: 'sidebar' %>
 99        EOF
100        runner.review('app/views/posts/index.html.erb', content)
101        expect(runner.errors.size).to eq(0)
102      end
103    end
104  end
105end