<commit_before>require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper')

describe "Analytical::Modules::Google" do
  before(:each) do
    @parent = mock('api', :options=>{:google=>{:key=>'abc'}})
  end
  describe 'on initialize' do
    it 'should set the command_location' do
      a = Analytical::Modules::Google.new :parent=>@parent, :key=>'abc'
      a.tracking_command_location.should == :head_append
    end
    it 'should set the options' do
      a = Analytical::Modules::Google.new :parent=>@parent, :key=>'abc'
      a.options.should == {:key=>'abc', :parent=>@parent}
    end
  end
  describe '#track' do
    it 'should return the tracking javascript' do
      @api = Analytical::Modules::Google.new :parent=>@parent, :key=>'abcdef'
      @api.track.should == "_gaq.push(['_trackPageview']);"
      @api.track('pagename', {:some=>'data'}).should ==  "_gaq.push(['_trackPageview', \"pagename\"]);"
    end
  end
  describe '#event' do
    it 'should return the event javascript' do
      @api = Analytical::Modules::Google.new :parent=>@parent, :key=>'abcdef'
      @api.event('pagename').should ==  '_gaq.push(["_trackEvent","Event","pagename"]);'
    end
    
    it 'should default the category to "Event" if none is specified' do
      @api = Analytical::Modules::Google.new :parent=>@parent, :key=>'abcdef'
      @api.event('pagename').should ==  '_gaq.push(["_trackEvent","Event","pagename"]);'
    end
    it 'should include data value, in the "opt_value" argument' do
      @api = Analytical::Modules::Google.new :parent=>@parent, :key=>'abcdef'
      @api.event('pagename', {:value=>555, :more=>'info'}).should ==  '_gaq.push(["_trackEvent","Event","pagename",null,555]);'
    end
    it 'should include category, label, value, and noninteraction, if specified' do
      @api = Analytical::Modules::Google.new :parent=>@parent, :key=>'abcdef'
      @api.event('pagename', {:category=>'Thing', :label=>'description', :value=>555, :noninteraction=>true}).should == '_gaq.push(["_trackEvent","Thing","pagename","description",555,true]);'
    end
    it 'should not include data if there is no value' do
      @api = Analytical::Modules::Google.new :parent=>@parent, :key=>'abcdef'
      @api.event('pagename', {:more=>'info'}).should ==  '_gaq.push(["_trackEvent","Event","pagename"]);'
    end
    it 'should not include data if it is not a hash' do
      @api = Analytical::Modules::Google.new :parent=>@parent, :key=>'abcdef'
      @api.event('pagename', 555).should ==  '_gaq.push(["_trackEvent","Event","pagename"]);'
    end

  end
  describe '#custom_event' do
    it 'should return the event javascript' do
      @api = Analytical::Modules::Google.new :parent=>@parent, :key=>'abcdef'
      @api.custom_event('Tag', 'view').should ==  '_gaq.push(["_trackEvent","Tag","view"]);'
    end

    it 'should set the optional label and event value' do
      @api = Analytical::Modules::Google.new :parent=>@parent, :key=>'abcdef'
      @api.custom_event('Tag', 'view', 'rails', 27).should ==  '_gaq.push(["_trackEvent","Tag","view","rails",27]);'
    end
  end
  
  describe '#set' do
    it 'should return the set javascript' do
      @api = Analytical::Modules::Google.new :parent=>@parent, :key=>'abcdef'
      @api.set(:index => 1, :name => 'gender', :value => 'male').should ==  "_gaq.push(['_setCustomVar', 1, 'gender', 'male']);"<commit_msg>Add tests for Google module's event_javascript and set_javascript methods<commit_after>require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper')

describe "Analytical::Modules::Google" do
  before(:each) do
    @parent = mock('api', :options=>{:google=>{:key=>'abc'}})
  end
  describe 'on initialize' do
    it 'should set the command_location' do
      a = Analytical::Modules::Google.new :parent=>@parent, :key=>'abc'
      a.tracking_command_location.should == :head_append
    end
    it 'should set the options' do
      a = Analytical::Modules::Google.new :parent=>@parent, :key=>'abc'
      a.options.should == {:key=>'abc', :parent=>@parent}
    end
  end
  describe '#track' do
    it 'should return the tracking javascript' do
      @api = Analytical::Modules::Google.new :parent=>@parent, :key=>'abcdef'
      @api.track.should == "_gaq.push(['_trackPageview']);"
      @api.track('pagename', {:some=>'data'}).should ==  "_gaq.push(['_trackPageview', \"pagename\"]);"
    end
  end
  describe '#event' do
    it 'should return the event javascript' do
      @api = Analytical::Modules::Google.new :parent=>@parent, :key=>'abcdef'
      @api.event('pagename').should ==  '_gaq.push(["_trackEvent","Event","pagename"]);'
    end
    
    it 'should default the category to "Event" if none is specified' do
      @api = Analytical::Modules::Google.new :parent=>@parent, :key=>'abcdef'
      @api.event('pagename').should ==  '_gaq.push(["_trackEvent","Event","pagename"]);'
    end
    it 'should include data value, in the "opt_value" argument' do
      @api = Analytical::Modules::Google.new :parent=>@parent, :key=>'abcdef'
      @api.event('pagename', {:value=>555, :more=>'info'}).should ==  '_gaq.push(["_trackEvent","Event","pagename",null,555]);'
    end
    it 'should include category, label, value, and noninteraction, if specified' do
      @api = Analytical::Modules::Google.new :parent=>@parent, :key=>'abcdef'
      @api.event('pagename', {:category=>'Thing', :label=>'description', :value=>555, :noninteraction=>true}).should == '_gaq.push(["_trackEvent","Thing","pagename","description",555,true]);'
    end
    it 'should not include data if there is no value' do
      @api = Analytical::Modules::Google.new :parent=>@parent, :key=>'abcdef'
      @api.event('pagename', {:more=>'info'}).should ==  '_gaq.push(["_trackEvent","Event","pagename"]);'
    end
    it 'should not include data if it is not a hash' do
      @api = Analytical::Modules::Google.new :parent=>@parent, :key=>'abcdef'
      @api.event('pagename', 555).should ==  '_gaq.push(["_trackEvent","Event","pagename"]);'
    end
  end
  describe '#custom_event' do
    it 'should return the event javascript' do
      @api = Analytical::Modules::Google.new :parent=>@parent, :key=>'abcdef'
      @api.custom_event('Tag', 'view').should ==  '_gaq.push(["_trackEvent","Tag","view"]);'
    end

    it 'should set the optional label and event value' do
      @api = Analytical::Modules::Google.new :parent=>@parent, :key=>'abcdef'
      @api.custom_event('Tag', 'view', 'rails', 27).should ==  '_gaq.push(["_trackEvent","Tag","view","rails",27]);'
    end
  end
  
  describe '#event_javascript' do
    it 'should return a custom javascript function' do
      @api = Analytical::Modules::Google.new :parent=>@parent, :key=>'abcdef'
      @api.event_javascript.should =~ /_gaq.push\(\['_trackEvent', data.category, name, data.label, data.value, data.noninteraction\]\);/
    end
  end
  describe '#set_javascript' do
    it 'should return a custom javascript function' do
      @api = Analytical::Modules::Google.new :parent=>@parent, :key=>'abcdef'
      @api.set_javascript.should =~ /_gaq.push\(\['_setCustomVar', data.index, data.name, data.value, data.scope\]\);/
    end
  end

  describe '#set' do
    it 'should return the set javascript' do
      @api = Analytical::Modules::Google.new :parent=>@parent, :key=>'abcdef'
      @api.set(:index => 1, :name => 'gender', :value => 'male').should ==  "_gaq.push(['_setCustomVar', 1, 'gender', 'male']);"
{'fraction_non_alphanumeric': 0.1936785474108944, 'fraction_numerical': 0.0056489576328177535, 'mean_word_length': 4.380607814761215, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 87, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3504633', 'n_tokens_mistral': 2510, 'n_tokens_neox': 2412, 'n_words': 546}