require 'spec_helper'

describe "Account", :js => true, :type => :feature, :capybara_feature => true do

  subject { page }

  before(:all) do
    Capybara.javascript_driver = :poltergeist
    Capybara.current_driver = Capybara.javascript_driver
    Capybara.default_wait_time = 10
  end

  let(:user) { FactoryGirl.create(:user) }

  before(:each) do
    UserMailer.deliveries.clear
    sign_in_poltergeist user
    visit "/#/account"

    find('div.account-mid.identity')
  end

  it { should have_selector('h1',    text: 'my account') }

  describe "identity" do
    before(:each) do
      find("#account-edit-identity-link").trigger(:click)
    end

    it { should have_selector('h2', text: 'identity:' ) }
    it { should have_selector('form#account-edit-email-form h4', text: 'Update your email address:') }
    it { should have_selector('form#account-edit-password-form h4', text: 'Update your password:') }

    describe "update email" do

      before(:each) do
        find('form#account-edit-email-form h4', text: 'Update your email address:')
        fill_in "account_update_email", with: "junk@jamkazam.com"
        #sleep 1
        find("#account-edit-email-submit").trigger(:click)
        fill_in "update-email-confirm-password", with: user.password
        find("#account-edit-email-confirm-password-submit").trigger(:click)

      end

      it { should have_selector('h1',    text: 'my account') }
      it { should have_selector('#notification h2', text: 'Confirmation Email Sent') }
    end

    describe "update password" do

      describe "successfully" do

        before(:each) do
          fill_in "current_password", with: user.password
          fill_in "password", with: user.password
          fill_in "password_confirmation", with: user.password
          find("#account-edit-password-submit").trigger(:click)
        end

        it { should have_selector('h1',    text: 'my account') }
      end

      describe "unsuccessfully" do

        before(:each) do
          find("#account-edit-password-submit").trigger(:click)
        end

        it { should have_selector('h2',    text: 'identity:') }
        it { should have_selector('div.field.error input[name=current_password] ~ ul li',    text: "can't be blank") }
        it { should have_selector('div.field.error input[name=password] ~ ul li',    text: "is too short (minimum is 6 characters)") }
        it { should have_selector('div.field.error input[name=password_confirmation] ~ ul li',    text: "can't be blank") }
      end
    end

    describe "profile"

    before(:each) do
      find("#account-edit-profile-link").trigger(:click)
      find('a.small', text: 'Change Avatar')
    end

    describe "successfully" do

      before(:each) do
        fill_in "first_name", with: "Bobby"
        fill_in "last_name", with: "Toes"
        find("#account-edit-profile-submit").trigger(:click)
      end

      it { should have_selector('h1',    text: 'my account') }
      it { should have_selector('#notification h2', text: 'Profile Changed') }
    end

    describe "unsuccessfully" do

      before(:each) do
        fill_in "first_name", with: ""
        fill_in "last_name", with: ""
        find("#account-edit-profile-submit").trigger(:click)
      end

      it { should have_selector('h2',    text: 'profile:') }
      it { should have_selector('div.field.error input[name=first_name] ~ ul li',    text: "can't be blank") }
      it { should have_selector('div.field.error input[name=last_name] ~ ul li',    text: "can't be blank") }
    end
  end
end
