require 'rails_helper'

RSpec.describe 'Course: Administration: Administration', type: :feature do
  subject { page }

  let(:instance) { create(:instance) }

  with_tenant(:instance) do
    let!(:user) { create(:administrator) }
    before { login_as(user, scope: :user) }

    describe 'index' do
      let!(:course) { create(:course) }
      before { visit course_admin_path(course) }

      context 'with valid information' do
        let(:new_title) { 'New Title' }
        let(:new_description) { 'New Description' }

        before do
          fill_in 'course_title',          with: new_title
          fill_in 'course_description',    with: new_description
          click_button I18n.t('helpers.submit.course.update')
        end

        it 'changes the attributes' do
          expect(course.reload.title).to eq(new_title)
          expect(course.reload.description).to eq(new_description)
        end
      end

      context 'with empty title' do
        before do
          fill_in 'course_title', with: ''
          click_button I18n.t('helpers.submit.course.update')
        end

        it 'does not change title' do
          expect(course.reload.title).not_to eq('')
        end

        it 'shows the error' do
          expect(page).to have_css('div.has-error')
        end
      end
    end
  end
end

require 'rails_helper'

RSpec.feature 'Course: Administration: Administration' do
  subject { page }

  let(:instance) { create(:instance) }

  with_tenant(:instance) do
    let!(:user) { create(:administrator) }
    before { login_as(user, scope: :user) }

    context 'As an administrator' do
      let(:course) { create(:course) }
      scenario 'I can change the course attributes' do
        visit course_admin_path(course)

        fill_in 'course_title', with: ''
        click_button I18n.t('helpers.submit.course.update')
        expect(course.reload.title).not_to eq('')
        expect(page).to have_selector('div.has-error')

        new_title = 'New Title'
        new_description = 'New Description'
        fill_in 'course_title',          with: new_title
        fill_in 'course_description',    with: new_description
        click_button I18n.t('helpers.submit.course.update')

        expect(page).to have_selector('div.alert.alert-success')
        expect(course.reload.title).to eq(new_title)
        expect(course.reload.description).to eq(new_description)
      end
    end
  end
end

Rewrite the course settings specification to use the new user story style.
Rewrite the course settings specification to use the new user story style.
