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']}
  let(:platforms) {{ 'Win32' => 'Windows', 'MacOSX' => 'Mac' }}

  describe "downloaded client" do

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

    shared_examples :dowload_client do
      # there is also a generic download pages
      before(:each) do
        #sign_in_poltergeist user
        visit path
        # 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.keys.include?(other_download_platform).should be true
        platforms.keys.delete(other_download_platform)

        remaining_platform = platforms.keys[0]

        #other_download.trigger(:click)
        other_download.click

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

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

        find("a.current-os-download").click

        if path =~ /landing/
          should have_content("The application is downloading...") #download instruction modal
          should have_content("how to install the JamKazam app for #{platforms[remaining_platform]}")
        end
      end
    end

    describe "goto downloads-page without signin" do
      
      describe :default_downloads_page do 
        it_behaves_like :dowload_client do
          let(:path){ "/downloads" }
        end
      end
  
      describe :landing_downloads_page do
        it_behaves_like :dowload_client do
          let(:path){ "/landing/general/downloads" }
        end
      end

    end

    describe "signin and goto downloads-page" do
      before(:each) do
        sign_in_poltergeist user
      end

      describe :default_downloads_page do 
        it_behaves_like :dowload_client do
          let(:path){ "/downloads" }
        end
      end
  
      describe :landing_downloads_page do
        it_behaves_like :dowload_client do
          let(:path){ "/landing/general/downloads" }
        end
      end

    end

  end
end
