require 'spec_helper'

describe 'create a group with valid params' do
  it 'should create the group' do
    customer = create :customer
    subtrade = create :subtrade
    login_as_user customer
    click_link 'Get the Scoop'
    click_link 'Discussions'
    click_link 'create_group_link'
    fill_in 'group_name', with: 'foobar'
    fill_in 'group_description', with: 'wadus'
    fill_in 'group_subtrade', with: subtrade.name
    find(:xpath, "//input[@id='group_subtrade_id']").set subtrade.id
    click_button 'create_group_button'
    page.should have_content 'Members of foobar'
    page.should_not have_content "Can't create message"
    group = Group.first
    group.messages.should be_empty
  end

  it 'should add first question if filled' do
    customer = create :customer
    subtrade = create :subtrade
    login_as_user customer
    click_link 'Get the Scoop'
    click_link 'Discussions'
    click_link 'create_group_link'
    fill_in 'group_name', with: 'foobar'
    fill_in 'group_description', with: 'wadus'
    fill_in 'group_subtrade', with: subtrade.name
    find(:xpath, "//input[@id='group_subtrade_id']").set subtrade.id
    fill_in 'your_question', with: 'My first question'
    click_button 'create_group_button'
    page.should have_content 'Members of foobar'
    group = Group.first
    group.messages.count.should eq 1
    group.messages.first.text.should eq 'My first question'
  end

end

describe 'trying to create a group with invalid params' do
  it 'should not create the group' do
    customer = create :customer
    login_as_user customer
    click_link 'Get the Scoop'
    click_link 'Discussions'
    click_link 'create_group_link'
    click_button 'create_group_button'
    page.should have_content 'Create a New Group'
  end
end
