require 'rails_helper'

feature 'Rule management' do
  scenario 'admin moves a rule down' do
    @user = create :admin
    login_as @user
    @rule = create :rule, category: 'combat', position: 1
    @other_rule = create :rule, category: 'combat', position: 2

    visit rules_path

    within "#rule_#{@rule.id}" do
      click_button 'move_down'
    end

    expect(page).to have_text('Rule was successfully moved down.')
  end

  scenario 'admin moves a rule up' do
    @user = create :admin
    login_as @user
    @rule = create :rule, category: 'combat', position: 1
    @other_rule = create :rule, category: 'combat', position: 2

    visit rules_path

    within "#rule_#{@other_rule.id}" do
      click_button 'move_up'
    end

    expect(page).to have_text('Rule was successfully moved up.')
  end

  scenario 'rule team member creates a new rule' do
    @user = create :rule_team_member
    login_as @user

    visit new_rule_path

    within '#new_rule' do
      fill_in 'rule_name', with: 'New rule'
      fill_in 'rule_body', with: 'this is the description for the new rule'
      select 'Combat', from: 'rule_category'
      click_button 'create'
    end

    expect(page).to have_text('Règle créée.')
  end

  scenario 'rule team member updates a rule' do
    @user = create :rule_team_member
    login_as @user
    @rule = create :rule

    visit edit_rule_path(@rule)

    within "#edit_rule_#{@rule.id}" do
      fill_in 'rule_name', with: 'New rule name'
      fill_in 'rule_body', with: 'new description for rule'
      click_button 'update'
    end

    expect(page).to have_text('Règle mise à jour')
  end

  scenario 'rule team member destroys a rule' do
    @user = create :rule_team_member
    login_as @user
    @rule = create :rule

    visit rule_path(@rule)

    within "#rule_#{@rule.id}" do
      click_button 'destroy'
    end

    expect(page).to have_text('Règle détruite.')
  end
end
