| 1 | $:.unshift(File.dirname(__FILE__)) |
|---|
| 2 | require 'spec_helper' |
|---|
| 3 | |
|---|
| 4 | describe "google_analytics plugin" do |
|---|
| 5 | def setup_google_analytics_plugin(profile_id, mode) |
|---|
| 6 | fake_plugin(:google_analytics) { |plugin| |
|---|
| 7 | plugin.mode = mode |
|---|
| 8 | plugin.conf['google_analytics.profile'] = profile_id |
|---|
| 9 | } |
|---|
| 10 | end |
|---|
| 11 | |
|---|
| 12 | describe "should render javascript" do |
|---|
| 13 | before do |
|---|
| 14 | @plugin = setup_google_analytics_plugin('53836-1', 'latest') |
|---|
| 15 | end |
|---|
| 16 | |
|---|
| 17 | it "for footer" do |
|---|
| 18 | snippet = @plugin.footer_proc |
|---|
| 19 | snippet.should == expected_html_footer_snippet |
|---|
| 20 | end |
|---|
| 21 | end |
|---|
| 22 | |
|---|
| 23 | describe "should render javascript" do |
|---|
| 24 | before do |
|---|
| 25 | @plugin = setup_google_analytics_plugin('53836-1', 'conf') |
|---|
| 26 | end |
|---|
| 27 | |
|---|
| 28 | it "for footer" do |
|---|
| 29 | snippet = @plugin.footer_proc |
|---|
| 30 | snippet.should be_empty |
|---|
| 31 | end |
|---|
| 32 | end |
|---|
| 33 | |
|---|
| 34 | describe "should not render when profile_id is empty" do |
|---|
| 35 | before do |
|---|
| 36 | @plugin = setup_google_analytics_plugin(nil, 'latest') |
|---|
| 37 | end |
|---|
| 38 | |
|---|
| 39 | it "for footer" do |
|---|
| 40 | snippet = @plugin.footer_proc |
|---|
| 41 | snippet.should be_empty |
|---|
| 42 | end |
|---|
| 43 | end |
|---|
| 44 | |
|---|
| 45 | def expected_html_footer_snippet |
|---|
| 46 | expected = <<-SCRIPT |
|---|
| 47 | <script type="text/javascript"><!-- |
|---|
| 48 | var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.") ; |
|---|
| 49 | document.write(unescape('%3Cscript src="' + gaJsHost + 'google-analytics.com/ga.js" type="text/javascript"%3E%3C/script%3E')) ; |
|---|
| 50 | // --></script> |
|---|
| 51 | <script type="text/javascript"><!-- |
|---|
| 52 | var pageTracker = _gat._getTracker("UA-53836-1"); |
|---|
| 53 | pageTracker._initData(); |
|---|
| 54 | pageTracker._trackPageview(); |
|---|
| 55 | // --></script> |
|---|
| 56 | SCRIPT |
|---|
| 57 | expected.gsub( /^\t/, '' ).chomp |
|---|
| 58 | end |
|---|
| 59 | end |
|---|