| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286 |
- var request = require("request");
- var qs = require("querystring");
- var uuid = require("uuid");
- var should = require("should");
- var sinon = require("sinon");
- var url = require("url");
- var ua = require("../lib/index.js");
- var utils = require("../lib/utils.js")
- var config = require("../lib/config.js")
- describe("ua", function () {
- describe("#pageview", function () {
- var _enqueue;
- beforeEach(function () {
- _enqueue = sinon.stub(ua.Visitor.prototype, "_enqueue", function () {
- if (arguments.length === 3 && typeof arguments[2] === 'function') {
- arguments[2]();
- }
- return this;
- });
- });
- afterEach(function () {
- _enqueue.restore()
- });
- it("should be available via the #pv shortcut", function () {
- var visitor = ua()
- visitor.pv.should.equal(visitor.pageview)
- });
- it("should accept arguments (path)", function () {
- var path = "/" + Math.random()
- var visitor = ua("UA-XXXXX-XX")
- var result = visitor.pageview(path)
- visitor._context = result._context;
- result.should.eql(visitor, "should return a visitor that is identical except for the context");
- result.should.be.instanceof(ua.Visitor);
- result._context.should.eql(_enqueue.args[0][1], "the pageview params should be persisted as the context of the visitor clone")
- _enqueue.calledOnce.should.equal(true, "#_enqueue should have been called once");
- _enqueue.args[0][0].should.equal("pageview");
- _enqueue.args[0][1].should.have.keys("dp")
- _enqueue.args[0][1].dp.should.equal(path);
- });
- it("should accept arguments (path, fn)", function () {
- var path = "/" + Math.random();
- var fn = sinon.spy();
- var visitor = ua("UA-XXXXX-XX")
- var result = visitor.pageview(path, fn)
- visitor._context = result._context;
- result.should.eql(visitor, "should return a visitor that is identical except for the context");
- result.should.be.instanceof(ua.Visitor);
- result._context.should.eql(_enqueue.args[0][1], "the pageview params should be persisted as the context of the visitor clone")
- _enqueue.calledOnce.should.equal(true, "#_enqueue should have been called once");
- _enqueue.args[0][0].should.equal("pageview");
- _enqueue.args[0][1].should.have.keys("dp")
- _enqueue.args[0][1].dp.should.equal(path);
- fn.calledOnce.should.equal(true, "callback should have been called once");
- });
- it("should accept arguments (params)", function () {
- var params = {
- dp: "/" + Math.random()
- };
- var visitor = ua("UA-XXXXX-XX")
- var result = visitor.pageview(params)
- visitor._context = result._context;
- result.should.eql(visitor, "should return a visitor that is identical except for the context");
- result.should.be.instanceof(ua.Visitor);
- result._context.should.eql(_enqueue.args[0][1], "the pageview params should be persisted as the context of the visitor clone")
- _enqueue.calledOnce.should.equal(true, "#_enqueue should have been called once");
- _enqueue.args[0][0].should.equal("pageview");
- _enqueue.args[0][1].should.have.keys("dp")
- _enqueue.args[0][1].dp.should.equal(params.dp);
- });
- it("should accept arguments (params, fn)", function () {
- var params = {
- dp: "/" + Math.random(),
- empty: null // Should be removed
- };
- var json = JSON.stringify(params)
- var fn = sinon.spy()
- ua("UA-XXXXX-XX").pageview(params, fn)
- _enqueue.calledOnce.should.equal(true, "#_enqueue should have been called once");
- _enqueue.args[0][0].should.equal("pageview");
- _enqueue.args[0][1].should.have.keys("dp")
- _enqueue.args[0][1].dp.should.equal(params.dp);
- fn.calledOnce.should.equal(true, "callback should have been called once");
- JSON.stringify(params).should.equal(json, "params should not have been modified")
- });
- it("should accept arguments (path, hostname)", function () {
- var path = Math.random().toString();
- var hostname = Math.random().toString();
- ua("UA-XXXXX-XX").pageview(path, hostname);
- _enqueue.calledOnce.should.equal(true, "#_enqueue should have been called once");
- _enqueue.args[0][0].should.equal("pageview");
- _enqueue.args[0][1].should.have.keys("dp", "dh")
- _enqueue.args[0][1].dp.should.equal(path);
- _enqueue.args[0][1].dh.should.equal(hostname);
- });
- it("should accept arguments (path, hostname, fn)", function () {
- var path = Math.random().toString();
- var hostname = Math.random().toString();
- var fn = sinon.spy()
- ua("UA-XXXXX-XX").pageview(path, hostname, fn);
- _enqueue.calledOnce.should.equal(true, "#_enqueue should have been called once");
- _enqueue.args[0][0].should.equal("pageview");
- _enqueue.args[0][1].should.have.keys("dp", "dh")
- _enqueue.args[0][1].dp.should.equal(path);
- _enqueue.args[0][1].dh.should.equal(hostname);
- fn.calledOnce.should.equal(true, "callback should have been called once");
- });
- it("should accept arguments (path, hostname, title)", function () {
- var path = Math.random().toString();
- var hostname = Math.random().toString();
- var title = Math.random().toString();
- ua("UA-XXXXX-XX").pageview(path, hostname, title);
- _enqueue.calledOnce.should.equal(true, "#_enqueue should have been called once");
- _enqueue.args[0][0].should.equal("pageview");
- _enqueue.args[0][1].should.have.keys("dp", "dh", "dt")
- _enqueue.args[0][1].dp.should.equal(path);
- _enqueue.args[0][1].dh.should.equal(hostname);
- _enqueue.args[0][1].dt.should.equal(title);
- });
- it("should accept arguments (path, hostname, title, fn)", function () {
- var path = Math.random().toString();
- var hostname = Math.random().toString();
- var title = Math.random().toString();
- var fn = sinon.spy()
- ua("UA-XXXXX-XX").pageview(path, hostname, title, fn);
- _enqueue.calledOnce.should.equal(true, "#_enqueue should have been called once");
- _enqueue.args[0][0].should.equal("pageview");
- _enqueue.args[0][1].should.have.keys("dp", "dh", "dt")
- _enqueue.args[0][1].dp.should.equal(path);
- _enqueue.args[0][1].dh.should.equal(hostname);
- _enqueue.args[0][1].dt.should.equal(title);
- fn.calledOnce.should.equal(true, "callback should have been called once");
- });
- it("should allow daisy-chaining and re-using parameters", function () {
- var path = "/" + Math.random()
- var title = Math.random().toString();
- ua("UA-XXXXX-XX").pageview(path, null, title).pageview()
- _enqueue.calledTwice.should.equal(true, "#_enqueue should have been called twice, once for each pageview");
- _enqueue.args[0][0].should.equal(_enqueue.args[1][0]);
- _enqueue.args[0][1].should.eql(_enqueue.args[1][1]);
- })
- it("should extend and overwrite params when daisy-chaining", function () {
- var path = "/" + Math.random()
- var path2 = "/" + Math.random()
- var title = Math.random().toString();
- var title2 = Math.random().toString();
- var foo = Math.random()
- ua("UA-XXXXX-XX")
- .pageview(path, null, title)
- .pageview({
- dt: title2,
- dp: path2,
- foo: foo
- }).pageview(path)
- _enqueue.calledThrice.should.equal(true, "#_enqueue should have been called three times, once for each pageview");
- _enqueue.args[0][1].should.have.keys("dp", "dt")
- _enqueue.args[0][1].dp.should.equal(path);
- _enqueue.args[0][1].dt.should.equal(title);
- _enqueue.args[1][1].should.have.keys("dp", "dt", "foo")
- _enqueue.args[1][1].dp.should.equal(path2);
- _enqueue.args[1][1].dt.should.equal(title2);
- _enqueue.args[1][1].foo.should.equal(foo);
- _enqueue.args[2][1].should.have.keys("dp", "dt")
- _enqueue.args[2][1].dp.should.equal(path);
- _enqueue.args[2][1].dt.should.equal(title2);
- })
- it("should accept document location instead of page path", function () {
- var params = {
- dl: "http://www.google.com/" + Math.random()
- };
- var json = JSON.stringify(params)
- var fn = sinon.spy()
- ua("UA-XXXXX-XX").pageview(params, fn)
- _enqueue.calledOnce.should.equal(true, "#_enqueue should have been called once");
- _enqueue.args[0][0].should.equal("pageview");
- _enqueue.args[0][1].should.have.keys("dl")
- _enqueue.args[0][1].dl.should.equal(params.dl);
- fn.calledOnce.should.equal(true, "callback should have been called once");
- JSON.stringify(params).should.equal(json, "params should not have been modified")
- });
- it("should fail without page path or document location", function () {
- var fn = sinon.spy()
- var visitor = ua()
- var result = visitor.pageview(null, fn);
- visitor._context = result._context;
- result.should.eql(visitor, "should return a visitor that is identical except for the context");
- result.should.be.instanceof(ua.Visitor);
- result._context.should.eql({}, "the transaction params should not be persisted")
- _enqueue.called.should.equal(false, "#_enqueue should have not been called once");
- fn.calledOnce.should.equal(true, "callback should have been called once");
- fn.args[0][0].should.be.instanceof(Error);
- fn.thisValues[0].should.equal(visitor);
- });
- });
- });
|