timing.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  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("#timing", 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 (category)", function () {
  25. var category = Math.random().toString();
  26. var visitor = ua()
  27. var result = visitor.timing(category);
  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("timing");
  34. _enqueue.args[0][1].should.have.keys("utc")
  35. _enqueue.args[0][1].utc.should.equal(category);
  36. });
  37. it("should accept arguments (category, fn)", function () {
  38. var category = Math.random().toString();
  39. var fn = sinon.spy();
  40. var visitor = ua()
  41. var result = visitor.timing(category, fn);
  42. visitor._context = result._context;
  43. result.should.eql(visitor, "should return a visitor that is identical except for the context");
  44. result.should.be.instanceof(ua.Visitor);
  45. result._context.should.eql(_enqueue.args[0][1], "the pageview params should be persisted as the context of the visitor clone")
  46. _enqueue.calledOnce.should.equal(true, "#_enqueue should have been called once");
  47. _enqueue.args[0][0].should.equal("timing");
  48. _enqueue.args[0][1].should.have.keys("utc")
  49. _enqueue.args[0][1].utc.should.equal(category);
  50. fn.calledOnce.should.equal(true, "callback should have been called once")
  51. });
  52. it("should accept arguments (category, variable)", function () {
  53. var category = Math.random().toString();
  54. var variable = Math.random().toString();
  55. var visitor = ua()
  56. var result = visitor.timing(category, variable);
  57. visitor._context = result._context;
  58. result.should.eql(visitor, "should return a visitor that is identical except for the context");
  59. result.should.be.instanceof(ua.Visitor);
  60. result._context.should.eql(_enqueue.args[0][1], "the pageview params should be persisted as the context of the visitor clone")
  61. _enqueue.calledOnce.should.equal(true, "#_enqueue should have been called once");
  62. _enqueue.args[0][0].should.equal("timing");
  63. _enqueue.args[0][1].should.have.keys("utc", "utv")
  64. _enqueue.args[0][1].utc.should.equal(category);
  65. _enqueue.args[0][1].utv.should.equal(variable);
  66. });
  67. it("should accept arguments (category, variable, fn)", function () {
  68. var category = Math.random().toString();
  69. var variable = Math.random().toString();
  70. var fn = sinon.spy();
  71. var visitor = ua()
  72. var result = visitor.timing(category, variable, fn);
  73. visitor._context = result._context;
  74. result.should.eql(visitor, "should return a visitor that is identical except for the context");
  75. result.should.be.instanceof(ua.Visitor);
  76. result._context.should.eql(_enqueue.args[0][1], "the pageview params should be persisted as the context of the visitor clone")
  77. _enqueue.calledOnce.should.equal(true, "#_enqueue should have been called once");
  78. _enqueue.args[0][0].should.equal("timing");
  79. _enqueue.args[0][1].should.have.keys("utc", "utv")
  80. _enqueue.args[0][1].utc.should.equal(category);
  81. _enqueue.args[0][1].utv.should.equal(variable);
  82. fn.calledOnce.should.equal(true, "callback should have been called once")
  83. });
  84. it("should accept arguments (category, variable, time)", function () {
  85. var category = Math.random().toString();
  86. var variable = Math.random().toString();
  87. var time = Math.random();
  88. var visitor = ua()
  89. var result = visitor.timing(category, variable, time);
  90. visitor._context = result._context;
  91. result.should.eql(visitor, "should return a visitor that is identical except for the context");
  92. result.should.be.instanceof(ua.Visitor);
  93. result._context.should.eql(_enqueue.args[0][1], "the pageview params should be persisted as the context of the visitor clone")
  94. _enqueue.calledOnce.should.equal(true, "#_enqueue should have been called once");
  95. _enqueue.args[0][0].should.equal("timing");
  96. _enqueue.args[0][1].should.have.keys("utc", "utv", "utt")
  97. _enqueue.args[0][1].utc.should.equal(category);
  98. _enqueue.args[0][1].utv.should.equal(variable);
  99. _enqueue.args[0][1].utt.should.equal(time);
  100. });
  101. it("should accept arguments (category, variable, time)", function () {
  102. var category = Math.random().toString();
  103. var variable = Math.random().toString();
  104. var time = Math.random();
  105. var fn = sinon.spy()
  106. var visitor = ua()
  107. var result = visitor.timing(category, variable, time, fn);
  108. visitor._context = result._context;
  109. result.should.eql(visitor, "should return a visitor that is identical except for the context");
  110. result.should.be.instanceof(ua.Visitor);
  111. result._context.should.eql(_enqueue.args[0][1], "the pageview params should be persisted as the context of the visitor clone")
  112. _enqueue.calledOnce.should.equal(true, "#_enqueue should have been called once");
  113. _enqueue.args[0][0].should.equal("timing");
  114. _enqueue.args[0][1].should.have.keys("utc", "utv", "utt")
  115. _enqueue.args[0][1].utc.should.equal(category);
  116. _enqueue.args[0][1].utv.should.equal(variable);
  117. _enqueue.args[0][1].utt.should.equal(time);
  118. fn.calledOnce.should.equal(true, "callback should have been called once")
  119. });
  120. it("should accept arguments (category, variable, time, label)", function () {
  121. var category = Math.random().toString();
  122. var variable = Math.random().toString();
  123. var time = Math.random();
  124. var label = Math.random().toString();
  125. var visitor = ua()
  126. var result = visitor.timing(category, variable, time, label);
  127. visitor._context = result._context;
  128. result.should.eql(visitor, "should return a visitor that is identical except for the context");
  129. result.should.be.instanceof(ua.Visitor);
  130. result._context.should.eql(_enqueue.args[0][1], "the pageview params should be persisted as the context of the visitor clone")
  131. _enqueue.calledOnce.should.equal(true, "#_enqueue should have been called once");
  132. _enqueue.args[0][0].should.equal("timing");
  133. _enqueue.args[0][1].should.have.keys("utc", "utv", "utt", "utl")
  134. _enqueue.args[0][1].utc.should.equal(category);
  135. _enqueue.args[0][1].utv.should.equal(variable);
  136. _enqueue.args[0][1].utt.should.equal(time);
  137. _enqueue.args[0][1].utl.should.equal(label);
  138. });
  139. it("should accept arguments (category, variable, time, label, fn)", function () {
  140. var category = Math.random().toString();
  141. var variable = Math.random().toString();
  142. var time = Math.random();
  143. var label = Math.random().toString();
  144. var fn = sinon.spy()
  145. var visitor = ua()
  146. var result = visitor.timing(category, variable, time, label, fn);
  147. visitor._context = result._context;
  148. result.should.eql(visitor, "should return a visitor that is identical except for the context");
  149. result.should.be.instanceof(ua.Visitor);
  150. result._context.should.eql(_enqueue.args[0][1], "the pageview params should be persisted as the context of the visitor clone")
  151. _enqueue.calledOnce.should.equal(true, "#_enqueue should have been called once");
  152. _enqueue.args[0][0].should.equal("timing");
  153. _enqueue.args[0][1].should.have.keys("utc", "utv", "utt", "utl")
  154. _enqueue.args[0][1].utc.should.equal(category);
  155. _enqueue.args[0][1].utv.should.equal(variable);
  156. _enqueue.args[0][1].utt.should.equal(time);
  157. _enqueue.args[0][1].utl.should.equal(label);
  158. fn.calledOnce.should.equal(true, "callback should have been called once")
  159. });
  160. it("should accept arguments (category, variable, time, label, params)", function () {
  161. var category = Math.random().toString();
  162. var variable = Math.random().toString();
  163. var time = Math.random();
  164. var label = Math.random().toString();
  165. var params = {p: Math.random().toString()}
  166. var visitor = ua()
  167. var result = visitor.timing(category, variable, time, label, params);
  168. visitor._context = result._context;
  169. result.should.eql(visitor, "should return a visitor that is identical except for the context");
  170. result.should.be.instanceof(ua.Visitor);
  171. result._context.should.eql(_enqueue.args[0][1], "the pageview params should be persisted as the context of the visitor clone")
  172. _enqueue.calledOnce.should.equal(true, "#_enqueue should have been called once");
  173. _enqueue.args[0][0].should.equal("timing");
  174. _enqueue.args[0][1].should.have.keys("utc", "utv", "utt", "utl", "p")
  175. _enqueue.args[0][1].utc.should.equal(category);
  176. _enqueue.args[0][1].utv.should.equal(variable);
  177. _enqueue.args[0][1].utt.should.equal(time);
  178. _enqueue.args[0][1].utl.should.equal(label);
  179. _enqueue.args[0][1].p.should.equal(params.p);
  180. });
  181. it("should accept arguments (category, variable, time, label, params, fn)", function () {
  182. var category = Math.random().toString();
  183. var variable = Math.random().toString();
  184. var time = Math.random();
  185. var label = Math.random().toString();
  186. var params = {p: Math.random().toString()}
  187. var fn = sinon.spy()
  188. var visitor = ua()
  189. var result = visitor.timing(category, variable, time, label, params, fn);
  190. visitor._context = result._context;
  191. result.should.eql(visitor, "should return a visitor that is identical except for the context");
  192. result.should.be.instanceof(ua.Visitor);
  193. result._context.should.eql(_enqueue.args[0][1], "the pageview params should be persisted as the context of the visitor clone")
  194. _enqueue.calledOnce.should.equal(true, "#_enqueue should have been called once");
  195. _enqueue.args[0][0].should.equal("timing");
  196. _enqueue.args[0][1].should.have.keys("utc", "utv", "utt", "utl", "p")
  197. _enqueue.args[0][1].utc.should.equal(category);
  198. _enqueue.args[0][1].utv.should.equal(variable);
  199. _enqueue.args[0][1].utt.should.equal(time);
  200. _enqueue.args[0][1].utl.should.equal(label);
  201. _enqueue.args[0][1].p.should.equal(params.p);
  202. fn.calledOnce.should.equal(true, "callback should have been called once")
  203. });
  204. it("should accept arguments (category, variable, time, label, params)", function () {
  205. var params = {
  206. utc: Math.random().toString(),
  207. utv: Math.random().toString(),
  208. utt: Math.random(),
  209. utl: Math.random().toString(),
  210. p: Math.random().toString()
  211. }
  212. var visitor = ua()
  213. var result = visitor.timing(params);
  214. visitor._context = result._context;
  215. result.should.eql(visitor, "should return a visitor that is identical except for the context");
  216. result.should.be.instanceof(ua.Visitor);
  217. result._context.should.eql(_enqueue.args[0][1], "the pageview params should be persisted as the context of the visitor clone")
  218. _enqueue.calledOnce.should.equal(true, "#_enqueue should have been called once");
  219. _enqueue.args[0][0].should.equal("timing");
  220. _enqueue.args[0][1].should.have.keys("utc", "utv", "utt", "utl", "p")
  221. _enqueue.args[0][1].utc.should.equal(params.utc);
  222. _enqueue.args[0][1].utv.should.equal(params.utv);
  223. _enqueue.args[0][1].utt.should.equal(params.utt);
  224. _enqueue.args[0][1].utl.should.equal(params.utl);
  225. _enqueue.args[0][1].p.should.equal(params.p);
  226. });
  227. it("should accept arguments (category, variable, time, label, params)", function () {
  228. var params = {
  229. utc: Math.random().toString(),
  230. utv: Math.random().toString(),
  231. utt: Math.random(),
  232. utl: Math.random().toString(),
  233. p: Math.random().toString()
  234. }
  235. var fn = sinon.spy()
  236. var visitor = ua()
  237. var result = visitor.timing(params, fn);
  238. visitor._context = result._context;
  239. result.should.eql(visitor, "should return a visitor that is identical except for the context");
  240. result.should.be.instanceof(ua.Visitor);
  241. result._context.should.eql(_enqueue.args[0][1], "the pageview params should be persisted as the context of the visitor clone")
  242. _enqueue.calledOnce.should.equal(true, "#_enqueue should have been called once");
  243. _enqueue.args[0][0].should.equal("timing");
  244. _enqueue.args[0][1].should.have.keys("utc", "utv", "utt", "utl", "p")
  245. _enqueue.args[0][1].utc.should.equal(params.utc);
  246. _enqueue.args[0][1].utv.should.equal(params.utv);
  247. _enqueue.args[0][1].utt.should.equal(params.utt);
  248. _enqueue.args[0][1].utl.should.equal(params.utl);
  249. _enqueue.args[0][1].p.should.equal(params.p);
  250. fn.calledOnce.should.equal(true, "callback should have been called once")
  251. });
  252. });
  253. });