Permalink
Browse files

write tests for verifying JS loading of stats

  • Loading branch information...
1 parent b44d1f7 commit aeadf081e7ca2dc08420dcdc82b1b51cf20df169 @lukebaker lukebaker committed Nov 2, 2012
@@ -95,7 +95,9 @@ AOI.admin = (function($) {
getNext(items);
});
ajax.error(function() {
- el.html('<span class="label label-important">Error</span>');
+ el.find('[data-stats-key]').each(function(i, col) {
+ $(col).html('<span class="label label-important">Error</span>');
+ });
getNext(items);
});
}
@@ -6,6 +6,29 @@ def mock_question(stubs={})
@mock_question ||= mock_model(Question, stubs)
end
+ describe "GET admin_stats" do
+ context "when not logged in" do
+ it "should redirect to the login page" do
+ get :admin_stats, :id => mock_question.id, :format => :json
+ response.should redirect_to(new_session_url)
+ end
+ end
+ context "when logged in as a non-admin" do
+ it "should redirect to the login page" do
+ sign_in_as Factory.create :user
+ get :admin_stats, :id => mock_question.id, :format => :json
+ response.should redirect_to(new_session_url)
+ end
+ end
+ context "when logged in as an admin" do
+ it "should return the json response of the stats" do
+ sign_in_as Factory.create :admin_confirmed_user
+ Question.stub!(:find).with(mock_question.id).and_return(mock_question)
+ response.should be_success
+ end
+ end
+ end
+
describe "GET index" do
context "when logged in as an admin" do
before do
@@ -89,7 +112,6 @@ def mock_question(stubs={})
end
end
-
end
end
@@ -0,0 +1,34 @@
+require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
+require 'active_resource/http_mock'
+
+describe 'An super-admin on the admin page' do
+ before do
+ ActiveResource::HttpMock.respond_to do |mock|
+ question = Factory.build(:question, :id => 1, :picked_prompt_id => 1).attributes
+ questions = [question].to_xml(:root => 'questions')
+ mock.get "/questions.xml?active_user_ideas=true&all=false&user_ideas=true&votes_since=2012-11-02", {}, questions, 200
+ mock.get "/questions/1.xml", {}, question.to_xml(:root => 'question'), 200
+ mock.get "/questions/1/vote_rate.xml", {}, {:voterate => 0.5}.to_xml, 200
+ mock.get "/questions/1/upload_to_participation_rate.xml", {}, {:uploadparticipationrate => 0.2}.to_xml, 200
+ mock.get "/questions/1/median_responses_per_session.xml", {}, {:median => 3}.to_xml, 200
+ mock.get "/questions/1/votes_per_uploaded_choice.xml", {}, {:value => 3.33333}.to_xml, 200
+ end
+ end
+
+ it "should see the stats get filled in via ajax" do
+ Capybara.current_driver = Capybara.javascript_driver
+ admin = Factory.create(:admin_confirmed_user)
+ capybara_sign_in_as(admin)
+
+ u = Factory.build(:email_confirmed_user)
+ e = Factory.create(:earl, :user => u)
+
+ visit admin_path
+ page.should have_content("Data Visualizations")
+ page.should have_no_selector('[data-stats-key] img')
+ page.find('[data-stats-key="vote_rate"]').text.should == "0.5"
+ page.find('[data-stats-key="upload_to_participation_rate"]').text.should == "0.2"
+ page.find('[data-stats-key="median_responses_per_session"]').text.should == "3"
+ page.find('[data-stats-key="votes_per_uploaded_choice"]').text.should == "3.333"
+ end
+end
@@ -5,4 +5,15 @@ def sign_in_as(user)
def sign_out
@controller.current_user = nil
end
+
+ def capybara_sign_in_as(user)
+ visit new_session_path
+ within('.body form') do
+ fill_in "Email", :with => user.email
+ fill_in "Password", :with => user.password
+ click_button "Log In"
+ end
+ page.should have_content("Signed in.")
+ page.should have_content(user.email)
+ end
end
@@ -22,6 +22,9 @@
q.local_identifier "335"
q.active false
q.site_id 9
+ q.recent_votes 1
+ q.user_ideas 20
+ q.active_user_ideas 10
end
Factory.sequence :question_id do |n|

0 comments on commit aeadf08

Please sign in to comment.