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 "Download", :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_max_wait_time = 10
  end

  let(:user) { FactoryGirl.create(:user) }
  let(:platforms) {[ 'Win32', 'MacOSX']}

  describe "downloaded client" do

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


    describe "downloads-page" do

      # there is also a generic download pages
      before(:each) do
        sign_in_poltergeist user
        visit '/downloads'

        # put the user on Windows to start
        detected_os = find("a.current-os-download")['data-platform']
      end

      it "toggle active download" do
        other_download = find(".download-others a")
        other_download_platform = other_download['data-platform']
        platforms.include?(other_download_platform).should be true
        platforms.delete(other_download_platform)

        remaining_platform = platforms[0]

        other_download.trigger(:click)

        find("a.current-os-download")['data-platform'].should == other_download_platform

        find(".download-others a[data-platform='#{remaining_platform}']").trigger(:click)
        find("a.current-os-download")['data-platform'].should == remaining_platform
      end

    end
  end
end
