From 2517653d34c131cd09caea023b8e3a01ffeb79a1 Mon Sep 17 00:00:00 2001 From: Raphael Sofaer Date: Tue, 26 Apr 2011 17:28:42 -0700 Subject: [PATCH] Red feature for infinite scroll after changing aspects --- features/infinite_scroll.feature | 24 +++++++++ features/step_definitions/custom_web_steps.rb | 8 +++ features/step_definitions/user_steps.rb | 16 ++++++ features/support/env.rb | 3 ++ public/javascripts/widgets/infinite-scroll.js | 52 ++++++++++--------- public/javascripts/widgets/timeago.js | 1 + 6 files changed, 79 insertions(+), 25 deletions(-) create mode 100644 features/infinite_scroll.feature diff --git a/features/infinite_scroll.feature b/features/infinite_scroll.feature new file mode 100644 index 000000000..55e9aad2f --- /dev/null +++ b/features/infinite_scroll.feature @@ -0,0 +1,24 @@ +@javascript +Feature: infinite scroll + In order to browse without disruption + As medium-sized internet grazing animal + I want the stream to infinite scroll + + Background: + Given many posts from bob and alice + + Scenario: on the main stream + When I sign in as "bob@bob.bob" + Then I should see 15 posts + + When I scroll down + And I wait for the ajax to finish + Then I should see 30 posts + + When I follow "generic" + And I wait for the ajax to finish + Then I should see 15 posts + + When I scroll down + And I wait for the ajax to finish + Then I should see 30 posts diff --git a/features/step_definitions/custom_web_steps.rb b/features/step_definitions/custom_web_steps.rb index fa3d1f146..167ffaa0f 100644 --- a/features/step_definitions/custom_web_steps.rb +++ b/features/step_definitions/custom_web_steps.rb @@ -188,3 +188,11 @@ Then /^the "([^"]*)" field(?: within "([^"]*)")? should be filled with "([^"]*)" end end end + +Then /^I should see (\d+) posts$/ do |n_posts| + evaluate_script("$('#main_stream .stream_element').length").should == n_posts.to_i +end + +And /^I scroll down$/ do + visit('#footer_nav') +end diff --git a/features/step_definitions/user_steps.rb b/features/step_definitions/user_steps.rb index e86ae19e2..7c399594d 100644 --- a/features/step_definitions/user_steps.rb +++ b/features/step_definitions/user_steps.rb @@ -139,3 +139,19 @@ Given /^there is a user "([^\"]*)" who's tagged "([^\"]*)"$/ do |full_name, tag| user.profile.build_tags user.profile.save! end + +Given /^many posts from bob and alice$/ do + alice = Factory(:user_with_aspect, :username => 'alice', :email => 'alice@alice.alice', :password => 'password', :getting_started => false) + bob = Factory(:user_with_aspect, :username => 'bob', :email => 'bob@bob.bob', :password => 'password', :getting_started => false) + connect_users_with_aspects(alice, bob) + time_interval = 1000 + (1..20).each do |n| + [alice, bob].each do |u| + post = u.post :status_message, :text => "#{u.username} - #{n} - #seeded", :to => u.aspects.first.id + post.created_at = post.created_at - time_interval + post.updated_at = post.updated_at - time_interval + post.save + time_interval += 1000 + end + end +end diff --git a/features/support/env.rb b/features/support/env.rb index 1257f607d..09827a3f5 100644 --- a/features/support/env.rb +++ b/features/support/env.rb @@ -43,6 +43,7 @@ require File.join(File.dirname(__FILE__), "database_cleaner_patches") require File.join(File.dirname(__FILE__), "..", "..", "spec", "support", "fake_redis") require File.join(File.dirname(__FILE__), "..", "..", "spec", "helper_methods") +require File.join(File.dirname(__FILE__), "..", "..", "spec", "support","user_methods") include HelperMethods Before do @@ -52,6 +53,8 @@ end silence_warnings do SERVICES['facebook'] = {'app_id' => :fake} end + +require File.join(File.dirname(__FILE__), "..", "..", "spec", "support", "fake_resque") module Resque def enqueue(klass, *args) klass.send(:perform, *args) diff --git a/public/javascripts/widgets/infinite-scroll.js b/public/javascripts/widgets/infinite-scroll.js index ad731647e..1143a37f2 100644 --- a/public/javascripts/widgets/infinite-scroll.js +++ b/public/javascripts/widgets/infinite-scroll.js @@ -4,32 +4,34 @@ */ (function() { - var InfiniteScroll = function() { - this.options = { - navSelector : "#pagination", - nextSelector : ".paginate", - itemSelector : ".stream_element", - pathParse : function( pathStr, nextPage ){ - var newPath = pathStr.replace("?", "?only_posts=true&"); - var last_time = $('#main_stream .stream_element').last().find('.time').attr('integer'); - return newPath.replace( /max_time=\d+/, 'max_time=' + last_time); - }, - bufferPx: 500, - debug: false, - donetext: Diaspora.widgets.i18n.t("infinite_scroll.no_more"), - loadingText: "", - loadingImg: '/images/ajax-loader.gif' - }; - - this.start = function() { - Diaspora.widgets.subscribe("stream/reloaded", InfiniteScroll.initialize); - - $('#main_stream').infinitescroll(this.options, function() { - Diaspora.widgets.publish("stream/scrolled"); - }); - }; + var InfiniteScroll = function() { }; + InfiniteScroll.prototype.options = { + navSelector : "#pagination", + nextSelector : ".paginate", + itemSelector : ".stream_element", + pathParse : function( pathStr, nextPage ){ + var newPath = pathStr.replace("?", "?only_posts=true&"); + var last_time = $('#main_stream .stream_element').last().find('.time').attr('integer'); + return newPath.replace( /max_time=\d+/, 'max_time=' + last_time); + }, + bufferPx: 500, + debug: false, + donetext: Diaspora.widgets.i18n.t("infinite_scroll.no_more"), + loadingText: "", + loadingImg: '/images/ajax-loader.gif' }; - Diaspora.widgets.add("infinitescroll", InfiniteScroll) + InfiniteScroll.prototype.initialize = function(){ + $('#main_stream').infinitescroll(this.options, function() { + Diaspora.widgets.publish("stream/scrolled"); + }); + }; + + InfiniteScroll.prototype.start = function() { + Diaspora.widgets.subscribe("stream/reloaded", this.initialize); + this.initialize(); + }; + + Diaspora.widgets.add("infinitescroll", InfiniteScroll); })(); diff --git a/public/javascripts/widgets/timeago.js b/public/javascripts/widgets/timeago.js index 42568fe81..c1647b925 100644 --- a/public/javascripts/widgets/timeago.js +++ b/public/javascripts/widgets/timeago.js @@ -7,6 +7,7 @@ Diaspora.widgets.add("timeago", function() { this.selector = "abbr.timeago"; this.start = function() { Diaspora.widgets.subscribe("stream/scrolled", this.updateTimeAgo); + Diaspora.widgets.subscribe("stream/reloaded", this.updateTimeAgo); if(Diaspora.widgets.i18n.language !== "en") { $.each($.timeago.settings.strings, function(index, element) {