Skip to content

Commit

Permalink
start writing some features for ads
Browse files Browse the repository at this point in the history
  • Loading branch information
shime committed Aug 5, 2013
1 parent 1a5e3fd commit c3199c6
Show file tree
Hide file tree
Showing 9 changed files with 144 additions and 25 deletions.
2 changes: 1 addition & 1 deletion app/views/ads/new.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
<%= render 'ads/form', :f => f %>
<%= f.button :submit %>
<%= f.button :submit, :id => "submit-ad" %>
<%= link_to "Odustani", root_path %>
<% end %>
</section>
Expand Down
12 changes: 9 additions & 3 deletions lib/ad_filter.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
class AdFilter
FILTER_ATTRIBUTES = [:region_id, :category_id, :ad_type]

attr_accessor :region_id, :category_id, :ad_type, :query, :page, :per_page, :user
SEARCH_ATTRIBUTES = [:region_id, :category_id, :ad_type, :query, :page, :per_page, :user]

SEARCH_ATTRIBUTES.each {|attr| attr_accessor attr }

def initialize(params = {})
normalized_hash_of(params).each do |key, value|
set_attribute key, to: value
if params.symbolize_keys.keys.any? {|key| SEARCH_ATTRIBUTES.include? key}
normalized_hash_of(params).each do |key, value|
set_attribute key, to: value
end
end
end

Expand Down Expand Up @@ -62,6 +66,8 @@ def present?

def blank?
value.blank? || value.to_i == 0
rescue
false
end

attr_accessor :value, :key
Expand Down
5 changes: 4 additions & 1 deletion spec/factories/cities.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
# Read about factories at https://github.com/thoughtbot/factory_girl
#

CITIES = %w.Split Zagreb Bjelovar Osijek Samobor Sisak Kutina.

FactoryGirl.define do
factory :city do
name "MyString"
name { CITIES.sample }
region
end
end
4 changes: 2 additions & 2 deletions spec/factories/favorites.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

FactoryGirl.define do
factory :favorite, :class => 'Favorites' do
ad nil
user nil
ad
user
end
end
4 changes: 3 additions & 1 deletion spec/factories/regions.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
# Read about factories at https://github.com/thoughtbot/factory_girl
#
REGIONS = %w.Bjelovarsko-Bilogorska Krapinsko-Zagorska Primorsko-Goranska.

FactoryGirl.define do
factory :region do
name "MyString"
name { REGIONS.sample }
end
end
39 changes: 38 additions & 1 deletion spec/features/ads_spec.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,44 @@
# encoding: utf-8

include AdHelper
include SessionHelper

def debug
save_and_open_page
end

feature "Ads" do
scenario "Ad creation for guests" do
visit new_ad_path
page.should have_content("moraš se prijaviti")
expect_to_see("moraš se prijaviti")
end

scenario "Ad creation for users", :sunspot_matchers => true do
["Clothing", "Food"].each {|name| create(:category, :name => name) }
["Zagreb", "Split"].each {|name| create(:city, :name => name) }

sign_in
visit new_ad_path

fill_in_ad_details

submit_ad

expect_to_see_details_for Ad.last
end

scenario "Ad validations", :sunspot_matchers => true do
["Clothing", "Food"].each {|name| create(:category, :name => name) }
["Zagreb", "Split"].each {|name| create(:city, :name => name) }

sign_in
visit new_ad_path

should_be_required :ad_type
should_be_required :title
should_be_required :description
should_be_required :phone

should_not_be_required :email
end
end
10 changes: 6 additions & 4 deletions spec/features/dashboard_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,18 @@ def link(text)

scenario "Ad filtering for giving" do
filter(:for => :giving).click
expect_to_see(:giving_only)
expect_to_see Ad.giving
expect_not_to_see Ad.receiving

filter(:for => :all).click
expect_to_see(:all)
expect_to_see Ad.all
end

scenario "Ad filtering for receiving" do
filter(:for => :receiving).click

expect_to_see(:receiving_only)
expect_to_see Ad.receiving
expect_not_to_see Ad.giving
end

scenario "Ad should have details" do
Expand All @@ -37,7 +39,7 @@ def link(text)

scenario "Ad search" do
enter_search(:for => ad)
expect_to_see ad: ad
expect_to_see ad
end


Expand Down
68 changes: 56 additions & 12 deletions spec/features/helpers/ad_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,26 +30,31 @@ def expect_to_see_ad(ad)
page.should have_content(ad.title)
end

def expect_to_see_text(text)
page.should have_content(text)
end

def expect_to_see(what)
case what
when :ad
when String
expect_to_see_text what
when Ad
expect_to_see_ad ad
when :giving_only
page.should have_selector(".cards-board article.card", count: Ad.giving.count)
page.should have_content giving_ad.title
page.should_not have_content receiving_ad.title
when :receiving_only
page.should have_selector(".cards-board article.card", count: Ad.receiving.count)
page.should have_content receiving_ad.title
page.should_not have_content giving_ad.title
when :all
page.should have_selector(".cards-board article.card", count: Ad.count)
Ad.all.map do |ad|
when Array
ads = what
page.should have_selector(".cards-board article.card", count: ads.count)
ads.map do |ad|
page.should have_content(ad.title)
end
end
end

def expect_not_to_see(ads)
ads.map do |ad|
page.should_not have_content(ad.title)
end
end

def expect_to_see_details_for ad
[ad.phone, time_ago_in_words(ad.expires_at), ad.description,
ad.title, ad.category.name, ad.region.name].each do |element|
Expand All @@ -60,4 +65,43 @@ def expect_to_see_details_for ad
def find_ad(ad)
page.find("a", :text => ad.title)
end

def default_attributes
{:ad_type => :supply, :title => "This is the new ad", :description => "This is the new ad",
:city => "Zagreb", :category => "Clothing", :phone => "+3859222222", :email => "email@email.com"}
end

def fill_in_ad_details(attributes = default_attributes)
choose "oglas_ad_type_#{Ad.type[attributes[:ad_type]]}" unless attributes[:ad_type].blank?
fill_in "Title", with: attributes[:title] if attributes[:title]
fill_in "Description", with: attributes[:description] if attributes[:description]
select attributes[:city], from: "oglas_city_id"
select attributes[:category], from: "oglas_category_id"
fill_in "Phone", with: attributes[:phone] if attributes[:phone]
fill_in "Email", with: attributes[:email] if attributes[:email]
end

def submit_ad
click_on "submit-ad"
end

def should_be_required(field)
fill_in_ad_details(default_attributes.merge({field => ""}))

submit_ad

within(:css, "div.oglas_#{field}") do
page.should(have_css("label.error"))
end
end

def should_not_be_required(field)
fill_in_ad_details(default_attributes.merge({field => ""}))

submit_ad

current_path.should eq(ad_path(Ad.last))

expect_to_see_details_for Ad.last
end
end
25 changes: 25 additions & 0 deletions spec/features/helpers/session_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
module SessionHelper
def sign_up_with(email, password)
visit sign_up_path
fill_in 'email', with: email
fill_in 'password', with: password
click_button 'Sign up'
end

def sign_in(user = create(:user))
visit sign_in_path
fill_in 'E-mail', with: user.email
fill_in 'Lozinka', with: user.password
click_button 'Prijavi se'
end

private

def sign_in_path
new_user_session_path
end

def sign_up_path
new_user_registration_path
end
end

0 comments on commit c3199c6

Please sign in to comment.