diff --git a/Gemfile b/Gemfile index bb3a958..cb26cd8 100644 --- a/Gemfile +++ b/Gemfile @@ -54,6 +54,7 @@ group :test do gem 'factory_girl_rails' gem 'shoulda-matchers' gem 'poltergeist' + gem 'database_cleaner' gem 'fivemat' gem 'capybara-screenshot' end diff --git a/Gemfile.lock b/Gemfile.lock index 79f4903..4654c1b 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -76,6 +76,7 @@ GEM execjs coffee-script-source (1.7.0) daemons (1.1.9) + database_cleaner (1.2.0) debug_inspector (0.0.2) devise (3.2.3) bcrypt-ruby (~> 3.0) @@ -255,6 +256,7 @@ DEPENDENCIES bourbon capybara-screenshot coffee-rails (~> 4.0.0) + database_cleaner devise dragonfly dragonfly-s3_data_store diff --git a/app/assets/javascripts/photos.js.coffee b/app/assets/javascripts/photos.js.coffee index a634de5..9a7f52e 100644 --- a/app/assets/javascripts/photos.js.coffee +++ b/app/assets/javascripts/photos.js.coffee @@ -1,4 +1,4 @@ $(document).ready -> $(".fancy").fancybox afterLoad: () -> - $.post "/photos/#{this.element.attr('id')}/log_view" + $.post "/photos/#{this.element.data('id')}/log_view" diff --git a/app/helpers/photos_helper.rb b/app/helpers/photos_helper.rb index f4cdbee..7fce6d2 100644 --- a/app/helpers/photos_helper.rb +++ b/app/helpers/photos_helper.rb @@ -1,5 +1,5 @@ module PhotosHelper def link_to_photo(photo) - link_to '', photo.image.url, rel: 'photos', class: 'fancy', id: photo.id + link_to '', photo.image.url, rel: 'photos', class: 'fancy', data: { id: photo.id } end end diff --git a/spec/features/visitor_navigates_site_spec.rb b/spec/features/visitor_navigates_site_spec.rb new file mode 100644 index 0000000..f665d26 --- /dev/null +++ b/spec/features/visitor_navigates_site_spec.rb @@ -0,0 +1,36 @@ +require 'spec_helper' + +describe 'visitor navigates site' do + + let!(:category) { create(:category) } + let!(:photo) { create(:photo, featured: true, categories: [category]) } + + it 'shows the featured image on the home page' do + visit root_path + expect(page).to have_selector("a[data-id='#{photo.id}']") + end + + it 'increments the view counter when an image is displayed', js: true do + visit root_path + + selector = "a[data-id='#{photo.id}']" + expect(page).to have_selector(selector) + + page.find(selector).click + + expect(page).to have_selector('img.fancybox-image') + + wait_for_ajax + photo.reload + + expect(photo.views).to eq(1) + end + + it 'shows the categories' do + visit root_path + + click_link 'portfolio' + + expect(page).to have_link(category.name) + end +end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 72911e9..b539385 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -3,6 +3,11 @@ ENV["RAILS_ENV"] ||= 'test' require File.expand_path("../../config/environment", __FILE__) require 'rspec/rails' require 'rspec/autorun' +require 'capybara/rspec' +require 'capybara/poltergeist' + +# Use Poltergeist +Capybara.javascript_driver = :poltergeist # Requires supporting ruby files with custom matchers and macros, etc, # in spec/support/ and its subdirectories. @@ -19,6 +24,7 @@ RSpec.configure do |config| # Include FactoryGirl methods config.include FactoryGirl::Syntax::Methods + config.include Ajax, type: :feature # Remove this line if you're not using ActiveRecord or ActiveRecord fixtures config.fixture_path = "#{::Rails.root}/spec/fixtures" @@ -26,7 +32,7 @@ RSpec.configure do |config| # If you're not using ActiveRecord, or you'd prefer not to run each of your # examples within a transaction, remove the following line or assign false # instead of true. - config.use_transactional_fixtures = true + config.use_transactional_fixtures = false # If true, the base class of anonymous controllers will be inferred # automatically. This will be the default behavior in future versions of @@ -38,4 +44,26 @@ RSpec.configure do |config| # the seed, which is printed after each run. # --seed 1234 config.order = "random" + + config.before(:suite) do + DatabaseCleaner.clean_with(:truncation) + end + + config.before(:each) do + Capybara.default_wait_time = 2 + DatabaseCleaner.strategy = :transaction + end + + config.before(:each, js: true) do + Capybara.default_wait_time = 10 + DatabaseCleaner.strategy = :deletion + end + + config.before(:each) do + DatabaseCleaner.start + end + + config.after(:each) do + DatabaseCleaner.clean + end end diff --git a/spec/support/ajax.rb b/spec/support/ajax.rb new file mode 100644 index 0000000..6789773 --- /dev/null +++ b/spec/support/ajax.rb @@ -0,0 +1,10 @@ +module Ajax + def wait_for_ajax + Timeout.timeout(Capybara.default_wait_time) do + loop do + active = page.evaluate_script('jQuery.active') + break if active == 0 + end + end + end +end