analytics.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. var expect = require('expect.js');
  2. var proxyquire = require('proxyquire');
  3. var object = require('mout').object;
  4. describe('analytics', function () {
  5. var mockAnalytics = function(stubs, promptResponse) {
  6. return proxyquire('../../lib/util/analytics', {
  7. insight: function () {
  8. return object.merge(stubs || {}, {
  9. askPermission: function (message, callback) {
  10. callback(undefined, promptResponse);
  11. }
  12. });
  13. }
  14. });
  15. };
  16. describe('#setup', function () {
  17. it('leaves analytics enabled if provided', function () {
  18. return mockAnalytics()
  19. .setup({ analytics: true })
  20. .then(function (enabled) {
  21. expect(enabled).to.be(true);
  22. });
  23. });
  24. it('leaves analytics disabled if provided', function () {
  25. return mockAnalytics()
  26. .setup({ analytics: false })
  27. .then(function (enabled) {
  28. expect(enabled).to.be(false);
  29. });
  30. });
  31. it('disables analytics for non-interactive mode', function () {
  32. return mockAnalytics()
  33. .setup({ interactive: false })
  34. .then(function (enabled) {
  35. expect(enabled).to.be(false);
  36. });
  37. });
  38. it('disables if insight.optOut is true and interactive', function () {
  39. return mockAnalytics({ optOut: true })
  40. .setup({ interactive: true })
  41. .then(function (enabled) {
  42. expect(enabled).to.be(false);
  43. });
  44. });
  45. it('enables if insight.optOut is false and interactive', function () {
  46. return mockAnalytics({ optOut: false })
  47. .setup({ interactive: true })
  48. .then(function (enabled) {
  49. expect(enabled).to.be(true);
  50. });
  51. });
  52. it('disables if insight.optOut is false and non-interactive', function () {
  53. return mockAnalytics({ optOut: false })
  54. .setup({ interactive: false })
  55. .then(function (enabled) {
  56. expect(enabled).to.be(false);
  57. });
  58. });
  59. it('enables if interactive insights return true from prompt', function () {
  60. return mockAnalytics({ optOut: undefined }, true)
  61. .setup({ interactive: true })
  62. .then(function (enabled) {
  63. expect(enabled).to.be(true);
  64. });
  65. });
  66. it('disables if interactive insights return false from prompt', function () {
  67. return mockAnalytics({ optOut: undefined }, false)
  68. .setup({ interactive: true })
  69. .then(function (enabled) {
  70. expect(enabled).to.be(false);
  71. });
  72. });
  73. });
  74. describe('Tracker', function (next) {
  75. it('tracks if analytics = true', function(next) {
  76. var analytics = mockAnalytics({
  77. track: function (arg) {
  78. expect(arg).to.be('foo');
  79. next();
  80. }
  81. });
  82. new analytics.Tracker({ analytics: true }).track('foo');
  83. });
  84. it('does not track if analytics = false', function () {
  85. var analytics = mockAnalytics({
  86. track: function (arg) {
  87. throw new Error();
  88. }
  89. });
  90. expect(function () {
  91. new analytics.Tracker({ analytics: false }).track('foo');
  92. }).to.not.throwError();
  93. });
  94. it('tracks if analytics = undefined and setup returns true', function(next) {
  95. var analytics = mockAnalytics({
  96. track: function (arg) {
  97. expect(arg).to.be('foo');
  98. next();
  99. }
  100. });
  101. analytics
  102. .setup({ analytics: true })
  103. .then(function () {
  104. new analytics.Tracker({}).track('foo');
  105. });
  106. });
  107. });
  108. });