require 'spec_helper'

# these test will verify all of the user progression cases that rely on a javascript event (not bothering with instrumented models)

describe "User Progression", :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) }

  describe "downloaded client" do

    before(:each) do
      FactoryGirl.create(:artifact_update, :product => 'JamClient/Win32')
      FactoryGirl.create(:artifact_update, :product => 'JamClient/MacOSX')
    end

    # right after you signup as a musician, you are downloaded to a page that let's you download the client.
    describe "post-signup" do
      before do
        visit signup_path
        fill_in "jam_ruby_user[first_name]",    with: "Mike"
        fill_in "jam_ruby_user[last_name]", with: "Jones"
        fill_in "jam_ruby_user[email]", with: "noone@jamkazam.com"
        fill_in "jam_ruby_user[password]", with: "jam123"
        fill_in "jam_ruby_user[password_confirmation]", with: "jam123"
        check("jam_ruby_user[instruments][drums][selected]")
        check("jam_ruby_user[terms_of_service]")
        click_button "CREATE ACCOUNT"

        should have_selector('.overlay-inner', text: "You have successfully registered as a JamKazam musician.")
        first("div.client-download a").trigger(:click)
        #find("body iframe.downloading")
        sleep 1
      end

      it {User.find_by_email('noone@jamkazam.com').reload(); User.find_by_email('noone@jamkazam.com').first_downloaded_client_at.should_not be_nil }
    end

    describe "downloads-page" do

      # there is also a generic download pages
      before(:each) do
        sign_in_poltergeist user
        visit '/downloads'
        first("div.client-download a").trigger(:click)
        #find("body iframe.downloading")
        sleep 1
      end

      it {user.reload(); user.first_downloaded_client_at.should_not be_nil }

    end

  end

  describe "certified gear" do
    before(:each) do
      sign_in_poltergeist user
      visit '/#/account/audio'
      find("div.account-audio a[data-purpose='add-profile']").trigger(:click)
      find("a[layout-wizard-link='2']").trigger(:click) # NEXT
      find("a[layout-wizard-link='3']").trigger(:click) # NEXT
      find("a[layout-wizard-link='4']").trigger(:click) # START TEST
      find("a[layout-action='close']").trigger(:click) # FINISH
      sleep 1
    end

    it {user.reload(); user.first_certified_gear_at.should_not be_nil }
  end

  describe "social promoted" do

  end
end
