spec/mixpanel/middleware_spec.rb in mixpanel-4.0.3 vs spec/mixpanel/middleware_spec.rb in mixpanel-4.0.4
- old
+ new
@@ -6,11 +6,10 @@
mixpanel.append_set(:first_name => "foo", :last_name => "bar", :username => "foobar")
mixpanel.append_increment(: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?\{.*"time":.*\}/
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\"/
@@ -19,32 +18,32 @@
txt.should =~ /mixpanel\.people\.increment\(\"sign_in_rate\"\s?,\s?1\)/
end
describe Mixpanel::Middleware do
include Rack::Test::Methods
-
+
describe "Dummy apps, no text/html" do
before do
setup_rack_application(DummyApp, :body => html_document, :headers => {})
get "/"
end
it "should pass through if the document is not text/html content type" do
last_response.body.should == html_document
end
end
-
+
describe "Dummy app, handles skip requests properly" do
before do
setup_rack_application(DummyApp, {:body => html_document, :headers => {"Content-Type" => "text/html"}})
end
-
+
it "should not append mixpanel scripts with skip request" do
get "/", {}, {"HTTP_SKIP_MIXPANEL_MIDDLEWARE" => true}
Nokogiri::HTML(last_response.body).search('script').should be_empty
end
-
+
it "should append mixpanel scripts without skip request" do
get "/"
Nokogiri::HTML(last_response.body).search('script').size.should == 1
end
@@ -64,11 +63,11 @@
Nokogiri::HTML(last_response.body).search('script').size.should == 1
end
end
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"}})
get "/", {}, {"HTTP_X_REQUESTED_WITH" => "XMLHttpRequest"}
@@ -289,9 +288,24 @@
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
+ end
+
+ describe "With turbolinks" do
+ before do
+ setup_rack_application(DummyApp, {
+ :body => ['',html_document],
+ :headers => {'Content-Type' => 'text/html'}
+ }, {:insert_js_last => true})
+ get '/', {}, {'HTTP_X_XHR_REFERER' => '/', 'mixpanel_events' => @mixpanel.queue}
+ end
+
+ it "should append mixpanel scripts to end of body element" do
+ Nokogiri::HTML(last_response.body).search('head script').should be_empty
+ Nokogiri::HTML(last_response.body).search('body script').should_not be_empty
end
end
end
describe "Tracking appended events" do