AnalyticsManagerTests.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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.Queues = {
  15. analytics: {
  16. events: {
  17. add: sinon.stub().resolves()
  18. },
  19. editingSessions: {
  20. add: sinon.stub().resolves()
  21. }
  22. }
  23. }
  24. this.backgroundRequest = sinon.stub().yields()
  25. this.request = sinon.stub().yields()
  26. this.AnalyticsManager = SandboxedModule.require(MODULE_PATH, {
  27. globals: {
  28. console: console
  29. },
  30. requires: {
  31. 'settings-sharelatex': this.Settings,
  32. 'logger-sharelatex': {
  33. warn() {}
  34. },
  35. '../../infrastructure/Queues': this.Queues
  36. }
  37. })
  38. })
  39. describe('ignores when', function() {
  40. it('user is smoke test user', function() {
  41. this.Settings.smokeTest = { userId: this.fakeUserId }
  42. this.AnalyticsManager.identifyUser(this.fakeUserId, '')
  43. sinon.assert.notCalled(this.Queues.analytics.events.add)
  44. })
  45. it('analytics service is disabled', function() {
  46. this.Settings.analytics.enabled = false
  47. this.AnalyticsManager.identifyUser(this.fakeUserId, '')
  48. sinon.assert.notCalled(this.Queues.analytics.events.add)
  49. })
  50. })
  51. describe('queues the appropriate message for', function() {
  52. it('identifyUser', function() {
  53. const oldUserId = '456def'
  54. this.AnalyticsManager.identifyUser(this.fakeUserId, oldUserId)
  55. sinon.assert.calledWithMatch(
  56. this.Queues.analytics.events.add,
  57. 'identify',
  58. {
  59. userId: this.fakeUserId,
  60. oldUserId
  61. }
  62. )
  63. })
  64. it('recordEvent', function() {
  65. const event = 'fake-event'
  66. this.AnalyticsManager.recordEvent(this.fakeUserId, event, null)
  67. sinon.assert.calledWithMatch(this.Queues.analytics.events.add, 'event', {
  68. event,
  69. userId: this.fakeUserId,
  70. segmentation: null
  71. })
  72. })
  73. it('updateEditingSession', function() {
  74. const projectId = '789ghi'
  75. const countryCode = 'fr'
  76. this.AnalyticsManager.updateEditingSession(
  77. this.fakeUserId,
  78. projectId,
  79. countryCode
  80. )
  81. sinon.assert.calledWithMatch(this.Queues.analytics.editingSessions.add, {
  82. userId: this.fakeUserId,
  83. projectId,
  84. countryCode
  85. })
  86. })
  87. })
  88. })