exception.js 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. var request = require("request");
  2. var qs = require("querystring");
  3. var uuid = require("uuid");
  4. var should = require("should");
  5. var sinon = require("sinon");
  6. var url = require("url");
  7. var ua = require("../lib/index.js");
  8. var utils = require("../lib/utils.js")
  9. var config = require("../lib/config.js")
  10. describe("ua", function () {
  11. describe("#exception", function () {
  12. var _enqueue;
  13. beforeEach(function () {
  14. _enqueue = sinon.stub(ua.Visitor.prototype, "_enqueue", function () {
  15. if (arguments.length === 3 && typeof arguments[2] === 'function') {
  16. arguments[2]();
  17. }
  18. return this;
  19. });
  20. });
  21. afterEach(function () {
  22. _enqueue.restore()
  23. });
  24. it("should accept arguments (description)", function () {
  25. var description = Math.random().toString();
  26. var visitor = ua()
  27. var result = visitor.exception(description);
  28. visitor._context = result._context;
  29. result.should.eql(visitor, "should return a visitor that is identical except for the context");
  30. result.should.be.instanceof(ua.Visitor);
  31. result._context.should.eql(_enqueue.args[0][1], "the pageview params should be persisted as the context of the visitor clone")
  32. _enqueue.calledOnce.should.equal(true, "#_enqueue should have been called once");
  33. _enqueue.args[0][0].should.equal("exception");
  34. _enqueue.args[0][1].should.have.keys("exd")
  35. _enqueue.args[0][1].exd.should.equal(description);
  36. });
  37. it("should accept arguments (description, fn)", function () {
  38. var description = Math.random().toString();
  39. var fn = sinon.spy();
  40. ua().exception(description, fn);
  41. _enqueue.calledOnce.should.equal(true, "#_enqueue should have been called once");
  42. _enqueue.args[0][0].should.equal("exception");
  43. _enqueue.args[0][1].should.have.keys("exd")
  44. _enqueue.args[0][1].exd.should.equal(description);
  45. fn.calledOnce.should.equal(true, "callback should have been called once")
  46. });
  47. it("should accept arguments (description, fatal)", function () {
  48. var description = Math.random().toString();
  49. var fatal = +!!(Math.random().toString()*2);
  50. var visitor = ua()
  51. var result = visitor.exception(description, fatal);
  52. visitor._context = result._context;
  53. result.should.eql(visitor, "should return a visitor that is identical except for the context");
  54. result.should.be.instanceof(ua.Visitor);
  55. result._context.should.eql(_enqueue.args[0][1], "the pageview params should be persisted as the context of the visitor clone")
  56. _enqueue.calledOnce.should.equal(true, "#_enqueue should have been called once");
  57. _enqueue.args[0][0].should.equal("exception");
  58. _enqueue.args[0][1].should.have.keys("exd", "exf")
  59. _enqueue.args[0][1].exd.should.equal(description);
  60. _enqueue.args[0][1].exf.should.equal(fatal);
  61. });
  62. it("should accept arguments (description, fatal, fn)", function () {
  63. var description = Math.random().toString();
  64. var fatal = +!!(Math.random().toString()*2);
  65. var fn = sinon.spy();
  66. var visitor = ua()
  67. var result = visitor.exception(description, fatal, fn);
  68. visitor._context = result._context;
  69. result.should.eql(visitor, "should return a visitor that is identical except for the context");
  70. result.should.be.instanceof(ua.Visitor);
  71. result._context.should.eql(_enqueue.args[0][1], "the pageview params should be persisted as the context of the visitor clone")
  72. _enqueue.calledOnce.should.equal(true, "#_enqueue should have been called once");
  73. _enqueue.args[0][0].should.equal("exception");
  74. _enqueue.args[0][1].should.have.keys("exd", "exf")
  75. _enqueue.args[0][1].exd.should.equal(description);
  76. _enqueue.args[0][1].exf.should.equal(fatal);
  77. fn.calledOnce.should.equal(true, "callback should have been called once")
  78. });
  79. it("should accept arguments (description, fatal, params)", function () {
  80. var description = Math.random().toString();
  81. var fatal = +!!(Math.random().toString()*2);
  82. var params = {"p": "/" + Math.random()}
  83. var visitor = ua()
  84. var result = visitor.exception(description, fatal, params);
  85. visitor._context = result._context;
  86. result.should.eql(visitor, "should return a visitor that is identical except for the context");
  87. result.should.be.instanceof(ua.Visitor);
  88. result._context.should.eql(_enqueue.args[0][1], "the pageview params should be persisted as the context of the visitor clone")
  89. _enqueue.calledOnce.should.equal(true, "#_enqueue should have been called once");
  90. _enqueue.args[0][0].should.equal("exception");
  91. _enqueue.args[0][1].should.have.keys("exd", "exf", "p")
  92. _enqueue.args[0][1].exd.should.equal(description);
  93. _enqueue.args[0][1].exf.should.equal(fatal);
  94. _enqueue.args[0][1].p.should.equal(params.p);
  95. });
  96. it("should accept arguments (description, fatal, params, fn)", function () {
  97. var description = Math.random().toString();
  98. var fatal = +!!(Math.random().toString()*2);
  99. var params = {"p": "/" + Math.random()};
  100. var fn = sinon.spy();
  101. var visitor = ua()
  102. var result = visitor.exception(description, fatal, params, fn);
  103. visitor._context = result._context;
  104. result.should.eql(visitor, "should return a visitor that is identical except for the context");
  105. result.should.be.instanceof(ua.Visitor);
  106. result._context.should.eql(_enqueue.args[0][1], "the pageview params should be persisted as the context of the visitor clone")
  107. _enqueue.calledOnce.should.equal(true, "#_enqueue should have been called once");
  108. _enqueue.args[0][0].should.equal("exception");
  109. _enqueue.args[0][1].should.have.keys("exd", "exf", "p")
  110. _enqueue.args[0][1].exd.should.equal(description);
  111. _enqueue.args[0][1].exf.should.equal(fatal);
  112. _enqueue.args[0][1].p.should.equal(params.p);
  113. fn.calledOnce.should.equal(true, "callback should have been called once")
  114. });
  115. it("should accept arguments (params)", function () {
  116. var params = {
  117. exd: Math.random().toString(),
  118. exf: +!!(Math.random().toString()*2),
  119. p: "/" + Math.random()
  120. };
  121. var visitor = ua()
  122. var result = visitor.exception(params);
  123. visitor._context = result._context;
  124. result.should.eql(visitor, "should return a visitor that is identical except for the context");
  125. result.should.be.instanceof(ua.Visitor);
  126. result._context.should.eql(_enqueue.args[0][1], "the pageview params should be persisted as the context of the visitor clone")
  127. _enqueue.calledOnce.should.equal(true, "#_enqueue should have been called once");
  128. _enqueue.args[0][0].should.equal("exception");
  129. _enqueue.args[0][1].should.have.keys("exd", "exf", "p")
  130. _enqueue.args[0][1].exd.should.equal(params.exd);
  131. _enqueue.args[0][1].exf.should.equal(params.exf);
  132. _enqueue.args[0][1].p.should.equal(params.p);
  133. });
  134. it("should accept arguments (params, fn)", function () {
  135. var params = {
  136. exd: Math.random().toString(),
  137. exf: +!!(Math.random().toString()*2),
  138. p: "/" + Math.random()
  139. };
  140. var fn = sinon.spy();
  141. var visitor = ua()
  142. var result = visitor.exception(params, fn);
  143. visitor._context = result._context;
  144. result.should.eql(visitor, "should return a visitor that is identical except for the context");
  145. result.should.be.instanceof(ua.Visitor);
  146. result._context.should.eql(_enqueue.args[0][1], "the pageview params should be persisted as the context of the visitor clone")
  147. _enqueue.calledOnce.should.equal(true, "#_enqueue should have been called once");
  148. _enqueue.args[0][0].should.equal("exception");
  149. _enqueue.args[0][1].should.have.keys("exd", "exf", "p")
  150. _enqueue.args[0][1].exd.should.equal(params.exd);
  151. _enqueue.args[0][1].exf.should.equal(params.exf);
  152. _enqueue.args[0][1].p.should.equal(params.p);
  153. fn.calledOnce.should.equal(true, "callback should have been called once")
  154. });
  155. });
  156. });