require "rails_helper"
feature "user uploads cities csv", :js do
scenario "user uploads valid csv file and sees uploaded rows content" do
user = create(:user)
login_as(user, scope: :user)
visit cities_url
page.attach_file("file", Rails.root.join("spec", "fixtures", "files", "valid_cities.csv"))
find("[data-test-id='cities-csv-import']").click
expect_url_with_flash(cities_url, "3 records were successfully uploaded from valid_cities.csv")
City.all.each { |city| expect_correct_city_row(city) }
end
scenario "user tries to upload non-csv file and sees correct table content" do
user = create(:user)
login_as(user, scope: :user)
visit cities_url
page.attach_file("file", Rails.root.join("spec", "fixtures", "files", "invalid_cities.csv"))
find("[data-test-id='cities-csv-import']").click
expect_url_with_flash(cities_url, "Invalid csv row")
expect(page).not_to have_selector("[data-test-id*='cities-index-table-citiy-']")
end
private
def expect_correct_city_row(city)
expect(page).to have_selector("[data-test-id='cities-index-table-city-#{city.id}-preamble']",
text: /\A#{Regexp.escape(city.preamble)}\z/)
expect(page).to have_selector("[data-test-id='cities-index-table-city-#{city.id}-description']",
text: /\A#{Regexp.escape(city.description)}\z/)
end
end