From 270380c423c9ca78b414c1f8dc03808504c73f7c Mon Sep 17 00:00:00 2001 From: Paul McMahon Date: Mon, 2 Apr 2012 15:40:33 +0900 Subject: [PATCH] basic testing and don't add distinct id if it doesn't exist --- .gitignore | 1 + lib/mixpanel_rails.rb | 4 +- mixpanel_rails.gemspec | 7 ++ .../app/views/layouts/application.html.erb | 9 +++ spec/app/fake.rb | 18 +++++ spec/mixpanel_rails_spec.rb | 79 +++++++++++++++++++ spec/spec_helper.rb | 5 ++ 7 files changed, 122 insertions(+), 1 deletion(-) create mode 100644 spec/app/app/views/layouts/application.html.erb create mode 100644 spec/app/fake.rb create mode 100644 spec/mixpanel_rails_spec.rb create mode 100644 spec/spec_helper.rb diff --git a/.gitignore b/.gitignore index 466c187..6cd01cf 100644 --- a/.gitignore +++ b/.gitignore @@ -3,3 +3,4 @@ Gemfile.lock pkg/* .rvmrc +spec/app/log/* diff --git a/lib/mixpanel_rails.rb b/lib/mixpanel_rails.rb index 25475a6..0a0a7c8 100644 --- a/lib/mixpanel_rails.rb +++ b/lib/mixpanel_rails.rb @@ -32,7 +32,9 @@ def process_mixpanel_queue unless response.redirect_url && request.host == URI.parse(response.redirect_url).host mixpanel = Mixpanel::Tracker.new(MixpanelRails::Railtie.config.mixpanel_rails.token, request.env, true) distinct_id = mixpanel_distinct_id.bind(self).call - params = {:distinct_id => distinct_id}.merge(register_with_mixpanel) + params = {} + params[:distinct_id] = distinct_id if distinct_id + params.merge!(register_with_mixpanel) if request.env["Rack-Middleware-PDFKit"] || response.redirect_url mixpanel_queue.each {|s| mixpanel.track_event(s, params) } else diff --git a/mixpanel_rails.gemspec b/mixpanel_rails.gemspec index 2bc29fa..e5d768c 100644 --- a/mixpanel_rails.gemspec +++ b/mixpanel_rails.gemspec @@ -19,4 +19,11 @@ Gem::Specification.new do |s| s.require_paths = ["lib"] s.add_dependency 'mixpanel', '~> 1.0.0' + s.add_dependency 'rails', '~> 3.0' + + s.add_development_dependency 'rspec', '~> 2.5' + s.add_development_dependency 'capybara', '~> 1.1.1' + s.add_development_dependency 'steak' + s.add_development_dependency 'steak' + s.add_development_dependency 'rspec-rails' end diff --git a/spec/app/app/views/layouts/application.html.erb b/spec/app/app/views/layouts/application.html.erb new file mode 100644 index 0000000..19e2183 --- /dev/null +++ b/spec/app/app/views/layouts/application.html.erb @@ -0,0 +1,9 @@ + + + + + + + <%= yield %> + + diff --git a/spec/app/fake.rb b/spec/app/fake.rb new file mode 100644 index 0000000..0b7938a --- /dev/null +++ b/spec/app/fake.rb @@ -0,0 +1,18 @@ +require 'bundler/setup' +require 'rails' +Bundler.require(:default, Rails.env) +require 'action_controller/railtie' +require 'action_view/railtie' + +app = Class.new(Rails::Application) +app.config.root = File.dirname(__FILE__) +app.config.secret_token = "3b7cd727ee24e8444053437c36cc66c4" +app.config.active_support.deprecation = :log +app.config.mixpanel_rails.token = "test_token" +app.initialize! + +app.routes.draw do + match ':controller(/:action(/:id))' +end + +class ApplicationController < ActionController::Base; end diff --git a/spec/mixpanel_rails_spec.rb b/spec/mixpanel_rails_spec.rb new file mode 100644 index 0000000..779af23 --- /dev/null +++ b/spec/mixpanel_rails_spec.rb @@ -0,0 +1,79 @@ +require File.expand_path(File.dirname(__FILE__) + '/spec_helper') + +class ExampleController < ApplicationController + uses_mixpanel + + def no_tracking + render inline: "OK", layout: true + end + + def with_tracking + track_with_mixpanel "Register for site" + render inline: "OK", layout: true + end +end + +class RedirectsController < ApplicationController + uses_mixpanel + + def index + track_with_mixpanel "Register for site" + redirect_to action: :redirected + end + + def redirected + render inline: "OK", layout: true + end +end + +class DistinctIdController < ApplicationController + uses_mixpanel distinct_id: lambda { 1 } + + def index + render inline: "OK", layout: true + end +end + +shared_examples_for "mixpanel init" do + it do + page.find("script:first").text.should include('mpq.push(["init", "test_token"]);') + end +end + +shared_examples_for "without distinct id" do + it { page.find("script:last").text.should_not include('distinct_id') } +end + +shared_examples_for "with distinct id" do + it { page.find("script:last").text.should include('mpq.push(["register", {"distinct_id":1}]);') } +end + +shared_examples_for "with event" do + it { page.find("script:last").text.should include('mpq.push(["track", "Register for site"])') } +end + +feature 'mixpanel integration' do + context 'visit page without tracking' do + background { visit '/example/no_tracking' } + it_should_behave_like "mixpanel init" + it_should_behave_like "without distinct id" + end + + context 'visit page with tracking' do + background { visit '/example/with_tracking' } + it_should_behave_like "mixpanel init" + it_should_behave_like "without distinct id" + it_should_behave_like "with event" + end + + context 'visit page with distinct id' do + background { visit '/distinct_id' } + it_should_behave_like "mixpanel init" + it_should_behave_like "with distinct id" + end + + context 'follow redirect' do + background { visit '/redirects' } + it_should_behave_like "with event" + end +end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb new file mode 100644 index 0000000..5d97b58 --- /dev/null +++ b/spec/spec_helper.rb @@ -0,0 +1,5 @@ +$:.unshift File.expand_path('../../lib', __FILE__) +require File.expand_path(File.dirname(__FILE__) + "/app/fake") +require 'rspec/rails' +require "steak" +require 'capybara/rspec'