From 3ec09930fe671c51880ec9f769deab56bbda6a00 Mon Sep 17 00:00:00 2001 From: Sarah Mei Date: Sat, 28 Jan 2012 14:43:47 -0800 Subject: [PATCH] Refactor contacts#index specs. json requests now paginate correctly, but infinite scroll is still broken. --- app/controllers/contacts_controller.rb | 3 +- spec/controllers/contacts_controller_spec.rb | 89 ++++++++++++-------- 2 files changed, 54 insertions(+), 38 deletions(-) diff --git a/app/controllers/contacts_controller.rb b/app/controllers/contacts_controller.rb index 403b5f2e5..a4b5b01d4 100644 --- a/app/controllers/contacts_controller.rb +++ b/app/controllers/contacts_controller.rb @@ -23,7 +23,8 @@ class ContactsController < ApplicationController respond_to do |format| format.json { - @people = Person.all_from_aspects(params[:aspect_ids], current_user).for_json + aspect_ids = params[:aspect_ids] || current_user.aspects.map(&:id) + @people = Person.all_from_aspects(aspect_ids, current_user).for_json.paginate(:page => params[:page], :per_page => 25) render :json => @people.to_json } format.any{} diff --git a/spec/controllers/contacts_controller_spec.rb b/spec/controllers/contacts_controller_spec.rb index fc094dfc5..8c37b3f9b 100644 --- a/spec/controllers/contacts_controller_spec.rb +++ b/spec/controllers/contacts_controller_spec.rb @@ -28,48 +28,63 @@ describe ContactsController do end describe '#index' do - it "succeeds" do - get :index - response.should be_success + context 'format html' do + it "succeeds" do + get :index + response.should be_success + end + + it "assigns contacts" do + get :index + contacts = assigns(:contacts) + contacts.to_set.should == bob.contacts.to_set + end + + it "shows only contacts a user is sharing with" do + contact = bob.contacts.first + contact.update_attributes(:sharing => false) + + get :index, :set => "mine" + contacts = assigns(:contacts) + contacts.to_set.should == bob.contacts.receiving.to_set + end + + it "shows all contacts (sharing and receiving)" do + contact = bob.contacts.first + contact.update_attributes(:sharing => false) + + get :index, :set => "all" + contacts = assigns(:contacts) + contacts.to_set.should == bob.contacts.to_set + end end - it "assigns contacts" do - get :index - contacts = assigns(:contacts) - contacts.to_set.should == bob.contacts.to_set - end + context 'format json' do + it 'respects pagination' do + get :index, :format => 'json', :page => '2' + assigns[:people].should == [] + response.should be_success + end - it "shows only contacts a user is sharing with" do - contact = bob.contacts.first - contact.update_attributes(:sharing => false) + it 'assumes all aspects if none are specified' do + get :index, :format => 'json' + assigns[:people].map(&:id).should =~ bob.contacts.map { |c| c.person.id } + response.should be_success + end - get :index, :set => "mine" - contacts = assigns(:contacts) - contacts.to_set.should == bob.contacts.receiving.to_set - end + it 'returns the contacts for multiple aspects' do + get :index, :aspect_ids => bob.aspect_ids, :format => 'json' + assigns[:people].map(&:id).should =~ bob.contacts.map { |c| c.person.id } + response.should be_success + end - it "shows all contacts (sharing and receiving)" do - contact = bob.contacts.first - contact.update_attributes(:sharing => false) - - get :index, :set => "all" - contacts = assigns(:contacts) - contacts.to_set.should == bob.contacts.to_set - end - - it 'will return the contacts for multiple aspects' do - get :index, :aspect_ids => bob.aspect_ids, :format => 'json' - assigns[:people].map(&:id).should =~ bob.contacts.map{|c| c.person.id} - response.should be_success - end - - - it 'does not select duplicate contacts' do - aspect = bob.aspects.create(:name => 'hilarious people') - aspect.contacts << bob.contact_for(eve.person) - get :index, :format => 'json', :aspect_ids => bob.aspect_ids - assigns[:people].map{|p| p.id}.uniq.should == assigns[:people].map{|p| p.id} - assigns[:people].map(&:id).should =~ bob.contacts.map{|c| c.person.id} + it 'does not return duplicate contacts' do + aspect = bob.aspects.create(:name => 'hilarious people') + aspect.contacts << bob.contact_for(eve.person) + get :index, :format => 'json', :aspect_ids => bob.aspect_ids + assigns[:people].map { |p| p.id }.uniq.should == assigns[:people].map { |p| p.id } + assigns[:people].map(&:id).should =~ bob.contacts.map { |c| c.person.id } + end end end