var expect = require('chai').expect

function requireUncached(module){
    delete require.cache[require.resolve(module)]
    return require(module)
}

var cookie = '';
global.document = {
	location: {
		href: '',
		host: ''
	},
	referrer: '',
	get cookie() {
		return cookie;
	},
	set cookie(value) {
		if(value.indexOf('1970') >= 0) {
			cookie = '';
			return;
		}
		if(value.indexOf(' expires') >= 0) {
			value = value.substring(0, value.indexOf(' expires'));
		}
		cookie = value;
	}
};
global.document.cookie = '';
global.window = {};

describe("Metricon Script", function() {

	it('test 1', function() {
		document.location = {
			href: 'http://www.marketlytics.com/',
			host: 'marketlytics.com'
		};
		document.referrer = 'http://www.google.com.pk/';
		var script = requireUncached('../script');
		var cookieObj = window.getTrafficSrcCookie();
		expect(cookieObj.utm_source).to.equal('www.google.com.pk');
		expect(cookieObj.utm_medium).to.equal('organic');
		expect(cookieObj.utm_term).to.equal('(not provided)');
	});

	it('test 2', function() {
		document.location = {
			href: 'http://www.marketlytics.com/?utm_source=facebook.com',
			host: 'marketlytics.com'
		};
		document.referrer = 'http://www.google.com.pk/';
		var script = requireUncached('../script');
		var cookieObj = window.getTrafficSrcCookie();
		expect(cookieObj.utm_source).to.equal('www.google.com.pk');
		expect(cookieObj.utm_medium).to.equal('organic');
		expect(cookieObj.utm_term).to.equal('(not provided)');
	});

	it('test 3', function() {
		document.location = {
			href: 'http://www.marketlytics.com/?utm_source=facebook.com&utm_medium=cpc',
			host: 'marketlytics.com'
		};
		document.referrer = 'http://www.google.com.pk/';
		var script = requireUncached('../script');
		var cookieObj = window.getTrafficSrcCookie();
		expect(cookieObj.utm_source).to.equal('www.google.com.pk');
		expect(cookieObj.utm_medium).to.equal('organic');
		expect(cookieObj.utm_term).to.equal('(not provided)');
	});
	
	it('test 4', function() {
		document.location = {
			href: 'http://www.marketlytics.com/?utm_source=facebook.com&utm_medium=cpc&utm_campaign=fbads',
			host: 'marketlytics.com'
		};
		document.referrer = 'http://www.google.com.pk/';
		var script = requireUncached('../script');
		var cookieObj = window.getTrafficSrcCookie();
		expect(cookieObj.utm_source).to.equal('facebook.com');
		expect(cookieObj.utm_medium).to.equal('cpc');
		expect(cookieObj.utm_term).to.equal('');
	});

	it('test 5', function() {
		document.location = {
			href: 'http://www.walmart.ca/search/canadian%20spa%20company?gclid=CLGDyL2t1ccCFQVuGwodWuUHtw',
			host: 'www.walmart.ca'
		};
		document.referrer = 'http://www.bing.com/';
		var script = requireUncached('../script');
		var cookieObj = window.getTrafficSrcCookie();
		expect(cookieObj.utm_source).to.equal('google');
		expect(cookieObj.utm_medium).to.equal('cpc');
		expect(cookieObj.gclid).to.equal('CLGDyL2t1ccCFQVuGwodWuUHtw');
	});

	it('test 6 - should not overwrite cookie if referrer is from the same domain', function() {
		document.location = {
			href: 'http://www.marketlytics.com/',
			host: 'marketlytics.com'
		};
		document.referrer = 'http://www.marketlytics.com/';
		var script = requireUncached('../script');
		var cookieObj = window.getTrafficSrcCookie();
		expect(cookieObj.utm_source).to.equal('google');
		expect(cookieObj.utm_medium).to.equal('cpc');
	});

	it('test 7', function() {
		document.location = {
			href: 'http://www.marketlytics.com/',
			host: 'marketlytics.com'
		};
		document.referrer = '';
		var script = requireUncached('../script');
		var cookieObj = window.getTrafficSrcCookie();
		expect(cookieObj.utm_source).to.equal('google');
		expect(cookieObj.utm_medium).to.equal('cpc');
	});
});
@@ -857,35 +857,34 @@
 xpect(cookieObj.
-utm
+ga
 _source).to.equa
@@ -918,35 +918,34 @@
 xpect(cookieObj.
-utm
+ga
 _medium).to.equa
@@ -973,32 +973,34 @@
 t(cookieObj.
-utm_term
+ga_keyword
 ).to.equal('
@@ -1327,35 +1327,34 @@
 xpect(cookieObj.
-utm
+ga
 _source).to.equa
@@ -1388,35 +1388,34 @@
 xpect(cookieObj.
-utm
+ga
 _medium).to.equa
@@ -1443,32 +1443,34 @@
 t(cookieObj.
-utm_term
+ga_keyword
 ).to.equal('
@@ -1812,35 +1812,34 @@
 xpect(cookieObj.
-utm
+ga
 _source).to.equa
@@ -1873,35 +1873,34 @@
 xpect(cookieObj.
-utm
+ga
 _medium).to.equa
@@ -1928,32 +1928,34 @@
 t(cookieObj.
-utm_term
+ga_keyword
 ).to.equal('
@@ -2317,35 +2317,34 @@
 xpect(cookieObj.
-utm
+ga
 _source).to.equa
@@ -2373,35 +2373,34 @@
 xpect(cookieObj.
-utm
+ga
 _medium).to.equa
@@ -2432,16 +2432,18 @@
 Obj.
-utm_term
+ga_keyword
 ).to
@@ -2791,35 +2791,34 @@
 xpect(cookieObj.
-utm
+ga
 _source).to.equa
@@ -2841,35 +2841,34 @@
 xpect(cookieObj.
-utm
+ga
 _medium).to.equa
@@ -2896,16 +2896,19 @@
 okieObj.
+ga_
 gclid).t
@@ -3301,35 +3301,34 @@
 xpect(cookieObj.
-utm
+ga
 _source).to.equa
@@ -3351,35 +3351,34 @@
 xpect(cookieObj.
-utm
+ga
 _medium).to.equa
@@ -3656,19 +3656,18 @@
 okieObj.
-utm
+ga
 _source)
@@ -3706,19 +3706,18 @@
 okieObj.
-utm
+ga
 _medium)

Change Cookie Object Key Names