1
0
mirror of https://github.com/danbee/danbarberphoto synced 2025-03-04 08:49:07 +00:00

Add first feature spec.

This commit is contained in:
Dan Barber 2014-03-07 17:49:11 +00:00
parent cbf09ee37a
commit 9c6db30261
7 changed files with 80 additions and 3 deletions

View File

@ -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

View File

@ -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

View File

@ -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"

View File

@ -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

View File

@ -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

View File

@ -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

10
spec/support/ajax.rb Normal file
View File

@ -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