require 'rails_helper'

RSpec.describe Public::ContractTemplatesController, type: :controller do
  render_views
  
  let(:account) { create(:account, name: "Dev Account") }
  let(:project) { create(:project, account: account) }
  let(:contract_templates) { create_list(:contract_template, 20, project: project) }

  describe '#index' do
    it 'responds successfully' do
      get :index, params: { project_id: project, account_id: project.account, contract_template_ids: contract_templates.map(&:id) }

      expect(response).to be_successful
    end

    it 'renders content if there are contract templates' do
      contract_template = create(:contract_template,
                                 name: 'My Contract Template', fee: 50, release_type: 'appearance',
                                 project: project)
      sign_path = new_account_project_contract_template_appearance_release_path(project.account, project, contract_template)
      sign_url = new_account_project_contract_template_appearance_release_url(project.account, project, contract_template)

      get :index, params: { project_id: project, account_id: project.account, contract_template_ids: [contract_template.id] }

      expect(response.body).to have_content('My Contract Template')
      expect(response.body).to have_link('Sign', href: sign_path)
    end

    context 'when there are many records' do
      it 'paginates the table' do
        get :index, params: { project_id: project, account_id: project.account, contract_template_ids: contract_templates.map(&:id) }

        expect(response.body).to have_link('2', href: account_project_contract_templates_path(project.account, project, contract_template_ids: contract_templates.map(&:id), page: 2))
      end
    end
  end
end

