# frozen_string_literal: true
require 'rails_helper'

Warden.test_mode!

RSpec.feature 'Projects', slow: true, elasticsearch: true, type: :feature do
  let(:site_user) { create :site_user }
  let(:site_admin) { create :site_admin }
  let(:collaborators) { create_list :site_user, 10 }

  context 'user is signed in' do
    before(:each) { sign_in site_user }
    context 'is on projects page' do
      before(:each) { visit user_projects_path(site_user.user_name) }

      scenario 'wishes to create a new project' do
        click_link('Create project')

        expect(current_path).to have_content(new_user_project_path(site_user.user_name))
      end
    end

    context 'is on new project page' do
      before(:each) { visit new_user_project_path(site_user.user_name) }

      scenario 'fills out the form with valid data and creates a project' do
        current_projects = site_user.owned_projects.size

        fill_in 'project_name', with: 'my_new_project'
        fill_in 'project_description', with: 'some pretty words about pretty things'
        click_on 'Create'

        expect(current_path).to have_content(
          user_project_path(site_user.user_name, 'my_new_project')
        )

        expect(page).to have_content(I18n.t('projects.create.success'))

        site_user.reload

        expect(site_user.owned_projects.size).to eq(current_projects + 1)
      end
    end # context is on a new project page

    context 'tries to create a project with invalid data' do
      before(:each) { visit new_user_project_path(site_user.user_name) }

      scenario ': name is not unique' do
        site_user.owned_projects << build(:project, name: 'one_name')
        fill_in 'project_name', with: 'one_name'
        click_on 'Create'

        expect(page).to have_content(I18n.t('projects.create.failure'))
        expect(current_path).to have_content(new_user_project_path(site_user.user_name))
      end # scenario name is not unique
    end # context tries to create a project with invalid data
  end # context user is signed in
end
