AnalyticsManagerTests.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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. globals: {
  42. console: console
  43. },
  44. requires: {
  45. 'settings-sharelatex': this.Settings,
  46. 'logger-sharelatex': {
  47. warn() {}
  48. },
  49. '../../infrastructure/Queues': this.Queues
  50. }
  51. })
  52. })
  53. describe('ignores when', function() {
  54. it('user is smoke test user', function() {
  55. this.Settings.smokeTest = { userId: this.fakeUserId }
  56. this.AnalyticsManager.identifyUser(this.fakeUserId, '')
  57. sinon.assert.notCalled(this.analyticsEventsQueue.add)
  58. })
  59. it('analytics service is disabled', function() {
  60. this.Settings.analytics.enabled = false
  61. this.AnalyticsManager.identifyUser(this.fakeUserId, '')
  62. sinon.assert.notCalled(this.analyticsEventsQueue.add)
  63. })
  64. })
  65. describe('queues the appropriate message for', function() {
  66. it('identifyUser', function() {
  67. const oldUserId = '456def'
  68. this.AnalyticsManager.identifyUser(this.fakeUserId, oldUserId)
  69. sinon.assert.calledWithMatch(this.analyticsEventsQueue.add, 'identify', {
  70. userId: this.fakeUserId,
  71. oldUserId
  72. })
  73. })
  74. it('recordEvent', function() {
  75. const event = 'fake-event'
  76. this.AnalyticsManager.recordEvent(this.fakeUserId, event, null)
  77. sinon.assert.calledWithMatch(this.analyticsEventsQueue.add, 'event', {
  78. event,
  79. userId: this.fakeUserId,
  80. segmentation: null
  81. })
  82. })
  83. it('updateEditingSession', function() {
  84. const projectId = '789ghi'
  85. const countryCode = 'fr'
  86. this.AnalyticsManager.updateEditingSession(
  87. this.fakeUserId,
  88. projectId,
  89. countryCode
  90. )
  91. sinon.assert.calledWithMatch(this.analyticsEditingSessionQueue.add, {
  92. userId: this.fakeUserId,
  93. projectId,
  94. countryCode
  95. })
  96. })
  97. })
  98. })