require 'rails_helper'

RSpec.feature 'Source files index page' do
  background { i_am_logged_in_as_admin }
  scenario "it renders with all kinds of source file without problems, also a good check wether all layout files have app prefix in navigation links" do
    user = create :user, email: 'source-user8731@example.com'
    batch = create 'execution_batch/my_batch'
    source_file_1 = create 'source_file/my_source', creator:  nil
    source_file_2 = create 'source_file/my_source', creator: user, original_file: fixture_file("source_files/my_source_1.csv")
    source_file_3 = create 'source_file/my_source', creator: batch
    visit '/dunlop/source_files'
    page.should have_content "Bronbestanden overzicht"
    page.body.should include "source-user8731@example.com"
    page.body.should_not include "source-user8731@example.com</a>" # users have no show page
    page.body.should include "Uitvoerings batch</a>" # link to batch

    find(".table-link.show[href='/dunlop/source_files/#{source_file_2.id}']").click

    current_path.should eq "/dunlop/source_files/#{source_file_2.id}"
    page.should have_content "Download"

    find(".title-back-link").click

    current_path.should eq "/dunlop/source_files"

    # download source file 2
    find(".table-link.download[href='/dunlop/source_files/#{source_file_2.id}/download']").click 

    page.body.should eq <<-CSV.strip_heredoc
      my_date,my_var,My count,my_num,my_truth
      2015-05-09,abc D,19,4.8,true
      2015-11-09,def G,19,7.8,false
    CSV
  end
end

