# frozen_string_literal: true

require 'spec_helper'

module RailsBestPractices
  module Reviews
    describe RemoveUnusedMethodsInHelpersReview do
      let(:runner) do
        Core::Runner.new(
          prepares: [Prepares::ControllerPrepare.new, Prepares::HelperPrepare.new],
          reviews: described_class.new(except_methods: [])
        )
      end

      it 'removes unused methods' do
        content = <<-EOF
        module PostsHelper
          def unused; end
        end
        EOF
        runner.prepare('app/helpers/posts_helper.rb', content)
        runner.review('app/helpers/posts_helper.rb', content)
        runner.after_review
        expect(runner.errors.size).to eq(1)
        expect(runner.errors[0].to_s).to eq(
          'app/helpers/posts_helper.rb:2 - remove unused methods (PostsHelper#unused)'
        )
      end

      it 'does not remove unused methods if called on views' do
        content = <<-EOF
        module PostsHelper
          def used?(post); end
        end
        EOF
        runner.prepare('app/helpers/posts_helper.rb', content)
        runner.review('app/helpers/posts_helper.rb', content)
        content = <<-EOF
        <% if used?(@post) %>
        <% end %>
        EOF
        runner.review('app/views/posts/show.html.erb', content)
        runner.after_review
        expect(runner.errors.size).to eq(0)
      end

      it 'does not remove unused methods if called on helpers' do
        content = <<-EOF
        module PostsHelper
          def used?(post)
            test?(post)
          end

          def test?(post); end
        end
        EOF
        runner.prepare('app/helpers/posts_helper.rb', content)
        runner.review('app/helpers/posts_helper.rb', content)
        content = <<-EOF
        <% if used?(@post) %>
        <% end %>
        EOF
        runner.review('app/views/posts/show.html.erb', content)
        runner.after_review
        expect(runner.errors.size).to eq(0)
      end

      it 'does not remove unused methods if called on admins' do
        helper_content = <<-EOF
        module PostsHelper
          def used?(post); end
        end
        EOF
        admin_content = <<-EOF
        ActiveAdmin.register Posts do
          column :used do
            used?(@post)
          end
        end
        EOF
        runner.prepare('app/helpers/posts_helper.rb', helper_content)
        runner.prepare('app/admin/posts.rb', admin_content)
        runner.after_prepare
        runner.review('app/helpers/posts_helper.rb', helper_content)
        runner.review('app/admin/posts.rb', admin_content)
        runner.after_review
        expect(runner.errors.size).to eq(0)
      end

      it 'does not remove unused methods if called on notifiers' do
        helper_content = <<-EOF
        module PostsHelper
          def used?(post); end
        end
        EOF
        notifier_content = <<-EOF
        class UserMailer ActionMailer::Base
          def post_notification(post)
            return unless used?(post)
          end
        end
        EOF
        runner.prepare('app/helpers/posts_helper.rb', helper_content)
        runner.prepare('app/notifers/post_mailer.rb', notifier_content)
        runner.after_prepare
        runner.review('app/helpers/posts_helper.rb', helper_content)
        runner.review('app/notifiers/post_mailer.rb', notifier_content)
        runner.after_review
        expect(runner.errors.size).to eq(0)
      end

      it 'does not remove unused methods if called on controllers' do
        helper_content = <<-EOF
        module PostsHelper
          def used?(post); end
        end
        EOF
        controller_content = <<-EOF
        class PostsController < InheritedResources::Base
          include PostsHelper

          def show
            @post = Post.find(params[:id])
            used?(@post)
          end
        end
        EOF
        runner.prepare('app/helpers/posts_helper.rb', helper_content)
        runner.prepare('app/controllers/posts_controller.rb', controller_content)
        runner.after_prepare
        runner.review('app/helpers/posts_helper.rb', helper_content)
        runner.review('app/controllers/posts_controller.rb', controller_content)
        runner.after_review
        expect(runner.errors.size).to eq(0)
      end

      it 'does not remove unused methods if called in descendant controllers' do
        application_helper_content = <<-EOF
        module ApplicationHelper
          def admin?; end
        end
        EOF
        application_controller_content = <<-EOF
        class ApplicationController
          include ApplicationHelper
        end
        EOF
        controller_content = <<-EOF
        class PostsController < ApplicationController

          def show
            head(:forbidden) unless admin?
          end
        end
        EOF
        runner.prepare('app/helpers/application_helper.rb', application_helper_content)
        runner.prepare('app/controllers/application_controller.rb', application_controller_content)
        runner.prepare('app/controllers/posts_controller.rb', controller_content)
        runner.after_prepare
        runner.review('app/helpers/application_helper.rb', application_helper_content)
        runner.review('app/controllers/application_controller.rb', application_controller_content)
        runner.review('app/controllers/posts_controller.rb', controller_content)
        runner.after_review
        expect(runner.errors.size).to eq(0)
      end

      it 'does not check ignored files' do
        runner =
          Core::Runner.new(
            prepares: [Prepares::ControllerPrepare.new, Prepares::HelperPrepare.new],
            reviews: described_class.new(ignored_files: /posts_helper/, except_methods: [])
          )

        content = <<-EOF
        module PostsHelper
          def unused; end
        end
        EOF
        runner.prepare('app/helpers/posts_helper.rb', content)
        runner.review('app/helpers/posts_helper.rb', content)
        runner.after_review
        expect(runner.errors.size).to eq(0)
      end
    end
  end
end
