AnalyticsManagerTests.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. const SandboxedModule = require('sandboxed-module')
  2. const path = require('path')
  3. const sinon = require('sinon')
  4. const MODULE_PATH = path.join(
  5. __dirname,
  6. '../../../../app/src/Features/Analytics/AnalyticsManager'
  7. )
  8. describe('AnalyticsManager', function() {
  9. beforeEach(function() {
  10. this.fakeUserId = '123abc'
  11. this.Settings = {
  12. analytics: { enabled: true }
  13. }
  14. this.analyticsEventsQueue = {
  15. add: sinon.stub().resolves(),
  16. process: sinon.stub().resolves()
  17. }
  18. this.analyticsEditingSessionQueue = {
  19. add: sinon.stub().resolves(),
  20. process: sinon.stub().resolves()
  21. }
  22. this.onboardingEmailsQueue = {
  23. add: sinon.stub().resolves(),
  24. process: sinon.stub().resolves()
  25. }
  26. const self = this
  27. this.Queues = {
  28. getAnalyticsEventsQueue: () => {
  29. return self.analyticsEventsQueue
  30. },
  31. getAnalyticsEditingSessionsQueue: () => {
  32. return self.analyticsEditingSessionQueue
  33. },
  34. getOnboardingEmailsQueue: () => {
  35. return self.onboardingEmailsQueue
  36. }
  37. }
  38. this.backgroundRequest = sinon.stub().yields()
  39. this.request = sinon.stub().yields()
  40. this.AnalyticsManager = SandboxedModule.require(MODULE_PATH, {
  41. requires: {
  42. 'settings-sharelatex': this.Settings,
  43. '../../infrastructure/Queues': this.Queues
  44. }
  45. })
  46. })
  47. describe('ignores when', function() {
  48. it('user is smoke test user', function() {
  49. this.Settings.smokeTest = { userId: this.fakeUserId }
  50. this.AnalyticsManager.identifyUser(this.fakeUserId, '')
  51. sinon.assert.notCalled(this.analyticsEventsQueue.add)
  52. })
  53. it('analytics service is disabled', function() {
  54. this.Settings.analytics.enabled = false
  55. this.AnalyticsManager.identifyUser(this.fakeUserId, '')
  56. sinon.assert.notCalled(this.analyticsEventsQueue.add)
  57. })
  58. })
  59. describe('queues the appropriate message for', function() {
  60. it('identifyUser', function() {
  61. const oldUserId = '456def'
  62. this.AnalyticsManager.identifyUser(this.fakeUserId, oldUserId)
  63. sinon.assert.calledWithMatch(this.analyticsEventsQueue.add, 'identify', {
  64. userId: this.fakeUserId,
  65. oldUserId
  66. })
  67. })
  68. it('recordEvent', function() {
  69. const event = 'fake-event'
  70. this.AnalyticsManager.recordEvent(this.fakeUserId, event, null)
  71. sinon.assert.calledWithMatch(this.analyticsEventsQueue.add, 'event', {
  72. event,
  73. userId: this.fakeUserId,
  74. segmentation: null
  75. })
  76. })
  77. it('updateEditingSession', function() {
  78. const projectId = '789ghi'
  79. const countryCode = 'fr'
  80. this.AnalyticsManager.updateEditingSession(
  81. this.fakeUserId,
  82. projectId,
  83. countryCode
  84. )
  85. sinon.assert.calledWithMatch(this.analyticsEditingSessionQueue.add, {
  86. userId: this.fakeUserId,
  87. projectId,
  88. countryCode
  89. })
  90. })
  91. })
  92. })