spec/mixpanel/tracker/middleware_spec.rb in mixpanel-1.1.3 vs spec/mixpanel/tracker/middleware_spec.rb in mixpanel-2.0.0
- old
+ new
@@ -1,7 +1,25 @@
require 'spec_helper'
+def exec_default_appends_on(mixpanel)
+ mixpanel.append_event("Visit", {:article => 1})
+ mixpanel.append_event("Sign in")
+ mixpanel.append_person_event(:first_name => "foo", :last_name => "bar", :username => "foobar")
+ mixpanel.append_person_increment_event(:sign_in_rate)
+end
+
+def check_for_default_appends_on(txt)
+ txt.should =~ /mixpanel\.track\("Visit",\s?\{"article":1\}\)/
+ txt.should =~ /mixpanel\.track\("Sign in",\s?\{\}\)/
+ txt.should =~ /mixpanel\.people\.set\(.*\);\nmixpanel.people.increment\(\"sign_in_rate\",\s?1\);/
+ match = txt.match(/mixpanel\.people\.set\((.*\));/)
+ match[1].should =~ /\"\$first_name\":\"foo\"/
+ match[1].should =~ /\"\$username\":\"foobar\"/
+ match[1].should =~ /\"\$last_name\":\"bar\"/
+ txt.should =~ /mixpanel\.people\.increment\(\"sign_in_rate\"\s?,\s?1\)/
+end
+
describe Mixpanel::Tracker::Middleware do
include Rack::Test::Methods
describe "Dummy apps, no text/html" do
before do
@@ -15,11 +33,11 @@
end
describe "Appending async mixpanel scripts" do
describe "With ajax requests" do
before do
- setup_rack_application(DummyApp, {:body => html_document, :headers => {"Content-Type" => "text/html"}}, {:async => true})
+ setup_rack_application(DummyApp, {:body => html_document, :headers => {"Content-Type" => "text/html"}})
get "/", {}, {"HTTP_X_REQUESTED_WITH" => "XMLHttpRequest"}
end
it "should not append mixpanel scripts to head element" do
Nokogiri::HTML(last_response.body).search('script').should be_empty
@@ -30,11 +48,11 @@
end
end
describe "With large ajax response" do
before do
- setup_rack_application(DummyApp, {:body => large_script, :headers => {"Content-Type" => "text/html"}}, {:async => true})
+ setup_rack_application(DummyApp, {:body => large_script, :headers => {"Content-Type" => "text/html"}})
get "/", {}, {"HTTP_X_REQUESTED_WITH" => "XMLHttpRequest"}
end
it "should not append mixpanel scripts to head element" do
last_response.body.index('var mp_protocol').should be_nil
@@ -46,11 +64,11 @@
end
describe "With regular requests" do
describe "With js in head" do
before do
- setup_rack_application(DummyApp, {:body => html_document, :headers => {"Content-Type" => "text/html"}}, {:async => true, :insert_js_last => false})
+ setup_rack_application(DummyApp, {:body => html_document, :headers => {"Content-Type" => "text/html"}}, {:insert_js_last => false})
get "/"
end
it "should append mixpanel scripts to head element" do
Nokogiri::HTML(last_response.body).search('head script').should_not be_empty
@@ -60,11 +78,11 @@
it "should have 1 included script" do
Nokogiri::HTML(last_response.body).search('script').size.should == 1
end
it "should use the specified token instantiating mixpanel lib" do
- last_response.should =~ /mpq.push\(\["init", "#{MIX_PANEL_TOKEN}"\]\)/
+ last_response.body.should =~ /mixpanel\.init\("#{MIX_PANEL_TOKEN}"\)/
end
it "should define Content-Length if not exist" do
last_response.headers.has_key?("Content-Length").should == true
end
@@ -74,11 +92,11 @@
end
end
describe "With js last" do
before do
- setup_rack_application(DummyApp, {:body => html_document, :headers => {"Content-Type" => "text/html"}}, {:async => true, :insert_js_last => true})
+ setup_rack_application(DummyApp, {:body => html_document, :headers => {"Content-Type" => "text/html"}}, {:insert_js_last => true})
get "/"
end
it "should append mixpanel scripts to end of body element" do
Nokogiri::HTML(last_response.body).search('head script').should be_empty
@@ -129,16 +147,16 @@
it "should append mixpanel scripts to head element" do
Nokogiri::HTML(last_response.body).search('head script').should_not be_empty
Nokogiri::HTML(last_response.body).search('body script').should be_empty
end
- it "should have 2 included scripts" do
- Nokogiri::HTML(last_response.body).search('script').size.should == 2
+ it "should have 1 included script" do
+ Nokogiri::HTML(last_response.body).search('script').size.should == 1
end
it "should use the specified token instantiating mixpanel lib" do
- last_response.should =~ /new MixpanelLib\('#{MIX_PANEL_TOKEN}'\)/
+ last_response.body.should =~ /mixpanel\.init\("#{MIX_PANEL_TOKEN}"\)/
end
it "should define Content-Length if not exist" do
last_response.headers.has_key?("Content-Length").should == true
end
@@ -163,72 +181,67 @@
end
describe "Tracking async appended events" do
before do
@mixpanel = Mixpanel::Tracker.new(MIX_PANEL_TOKEN, {})
- @mixpanel.append_event("Visit", {:article => 1})
- @mixpanel.append_event("Sign in")
+ exec_default_appends_on @mixpanel
end
describe "With ajax requests and text/html response" do
before do
- setup_rack_application(DummyApp, {:body => "<p>response</p>", :headers => {"Content-Type" => "text/html"}}, {:async => true})
+ setup_rack_application(DummyApp, {:body => "<p>response</p>", :headers => {"Content-Type" => "text/html"}})
get "/", {}, {"mixpanel_events" => @mixpanel.queue, "HTTP_X_REQUESTED_WITH" => "XMLHttpRequest"}
end
it "should render only one script tag" do
Nokogiri::HTML(last_response.body).search('script').size.should == 1
end
it "should be tracking the correct events inside a script tag" do
script = Nokogiri::HTML(last_response.body).search('script')
- script.inner_html.should =~ /try\s?\{(.*)\}\s?catch/m
- script.inner_html.should =~ /mpq\.push\(\["track",\s?"Visit",\s?\{"article":1\}\]\)/
- script.inner_html.should =~ /mpq\.push\(\["track",\s?"Sign in",\s?\{\}\]\)/
+ check_for_default_appends_on script.inner_html
end
it "should delete events queue after use it" do
last_request.env.has_key?("mixpanel_events").should == false
end
end
describe "With ajax requests and text/javascript response" do
before do
- setup_rack_application(DummyApp, {:body => "alert('response')", :headers => {"Content-Type" => "text/javascript"}}, {:async => true})
+ setup_rack_application(DummyApp, {:body => "alert('response')", :headers => {"Content-Type" => "text/javascript"}})
get "/", {}, {"mixpanel_events" => @mixpanel.queue, "HTTP_X_REQUESTED_WITH" => "XMLHttpRequest"}
end
it "should not render a script tag" do
Nokogiri::HTML(last_response.body).search('script').size.should == 0
end
it "should be tracking the correct events inside a try/catch" do
script = last_response.body.match(/try\s?\{(.*)\}\s?catch/m)[1]
- script.should =~ /mpq\.push\(\["track",\s?"Visit",\s?\{"article":1\}\]\)/
- script.should =~ /mpq\.push\(\["track",\s?"Sign in",\s?\{\}\]\)/
+ check_for_default_appends_on script
end
it "should delete events queue after use it" do
last_request.env.has_key?("mixpanel_events").should == false
end
end
describe "With regular requests" do
before do
- setup_rack_application(DummyApp, {:body => html_document, :headers => {"Content-Type" => "text/html"}}, {:async => true})
+ setup_rack_application(DummyApp, {:body => html_document, :headers => {"Content-Type" => "text/html"}})
get "/", {}, {"mixpanel_events" => @mixpanel.queue}
end
it "should render 2 script tags" do
Nokogiri::HTML(last_response.body).search('script').size.should == 2
end
it "should be tracking the correct events" do
- last_response.body.should =~ /mpq\.push\(\["track",\s?"Visit",\s?\{"article":1\}\]\)/
- last_response.body.should =~ /mpq\.push\(\["track",\s?"Sign in",\s?\{\}\]\)/
+ check_for_default_appends_on last_response.body
end
it "should delete events queue after use it" do
last_request.env.has_key?("mixpanel_events").should == false
end
@@ -236,12 +249,11 @@
end
describe "Tracking appended events" do
before do
@mixpanel = Mixpanel::Tracker.new(MIX_PANEL_TOKEN, {})
- @mixpanel.append_event("Visit", {:article => 1})
- @mixpanel.append_event("Sign in")
+ exec_default_appends_on @mixpanel
end
describe "With ajax requests and text/html response" do
before do
setup_rack_application(DummyApp, :body => "<p>response</p>", :headers => {"Content-Type" => "text/html"})
@@ -253,13 +265,11 @@
Nokogiri::HTML(last_response.body).search('script').size.should == 1
end
it "should be tracking the correct events inside a script tag" do
script = Nokogiri::HTML(last_response.body).search('script')
- script.inner_html.should =~ /try\s?\{(.*)\}\s?catch/m
- script.inner_html.should =~ /mpmetrics\.track\("Visit",\s?\{"article":1\}\)/
- script.inner_html.should =~ /mpmetrics\.track\("Sign in",\s?\{\}\)/
+ check_for_default_appends_on script.inner_html
end
it "should delete events queue after use it" do
last_request.env.has_key?("mixpanel_events").should == false
end
@@ -275,12 +285,11 @@
Nokogiri::HTML(last_response.body).search('script').size.should == 0
end
it "should be tracking the correct events inside a try/catch" do
script = last_response.body.match(/try\s?\{(.*)\}\s?catch/m)[1]
- script.should =~ /mpmetrics\.track\("Visit",\s?\{"article":1\}\)/
- script.should =~ /mpmetrics\.track\("Sign in",\s?\{\}\)/
+ check_for_default_appends_on script
end
it "should delete events queue after use it" do
last_request.env.has_key?("mixpanel_events").should == false
end
@@ -291,16 +300,15 @@
setup_rack_application(DummyApp, :body => html_document, :headers => {"Content-Type" => "text/html"})
get "/", {}, {"mixpanel_events" => @mixpanel.queue}
end
- it "should render 3 script tags" do
- Nokogiri::HTML(last_response.body).search('script').size.should == 3
+ it "should render 2 script tags" do
+ Nokogiri::HTML(last_response.body).search('script').size.should == 2
end
it "should be tracking the correct events" do
- last_response.body.should =~ /mpmetrics\.track\("Visit",\s?\{"article":1\}\)/
- last_response.body.should =~ /mpmetrics\.track\("Sign in",\s?\{\}\)/
+ check_for_default_appends_on last_response.body
end
it "should delete events queue after use it" do
last_request.env.has_key?("mixpanel_events").should == false
end