BetaProgramHandlerTests.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. const SandboxedModule = require('sandboxed-module')
  2. const path = require('path')
  3. const modulePath = path.join(
  4. __dirname,
  5. '../../../../app/src/Features/BetaProgram/BetaProgramHandler'
  6. )
  7. const sinon = require('sinon')
  8. const { expect } = require('chai')
  9. describe('BetaProgramHandler', function () {
  10. beforeEach(function () {
  11. this.user_id = 'some_id'
  12. this.user = {
  13. _id: this.user_id,
  14. email: 'user@example.com',
  15. features: {},
  16. betaProgram: false,
  17. save: sinon.stub().callsArgWith(0, null),
  18. }
  19. this.handler = SandboxedModule.require(modulePath, {
  20. requires: {
  21. '@overleaf/metrics': {
  22. inc: sinon.stub(),
  23. },
  24. '../User/UserUpdater': (this.UserUpdater = {
  25. promises: {
  26. updateUser: sinon.stub().resolves(),
  27. },
  28. }),
  29. '../Analytics/AnalyticsManager': (this.AnalyticsManager = {
  30. setUserPropertyForUser: sinon.stub().resolves(),
  31. }),
  32. },
  33. })
  34. })
  35. describe('optIn', function () {
  36. beforeEach(function () {
  37. this.user.betaProgram = false
  38. this.call = callback => {
  39. this.handler.optIn(this.user_id, callback)
  40. }
  41. })
  42. it('should call userUpdater', function (done) {
  43. this.call(err => {
  44. expect(err).to.not.exist
  45. this.UserUpdater.promises.updateUser.callCount.should.equal(1)
  46. done()
  47. })
  48. })
  49. it('should set beta-program user property to true', function (done) {
  50. this.call(err => {
  51. expect(err).to.not.exist
  52. sinon.assert.calledWith(
  53. this.AnalyticsManager.setUserPropertyForUser,
  54. this.user_id,
  55. 'beta-program',
  56. true
  57. )
  58. done()
  59. })
  60. })
  61. it('should not produce an error', function (done) {
  62. this.call(err => {
  63. expect(err).to.not.exist
  64. done()
  65. })
  66. })
  67. describe('when userUpdater produces an error', function () {
  68. beforeEach(function () {
  69. this.UserUpdater.promises.updateUser.rejects()
  70. })
  71. it('should produce an error', function (done) {
  72. this.call(err => {
  73. expect(err).to.exist
  74. expect(err).to.be.instanceof(Error)
  75. done()
  76. })
  77. })
  78. })
  79. })
  80. describe('optOut', function () {
  81. beforeEach(function () {
  82. this.user.betaProgram = true
  83. this.call = callback => {
  84. this.handler.optOut(this.user_id, callback)
  85. }
  86. })
  87. it('should call userUpdater', function (done) {
  88. this.call(err => {
  89. expect(err).to.not.exist
  90. this.UserUpdater.promises.updateUser.callCount.should.equal(1)
  91. done()
  92. })
  93. })
  94. it('should set beta-program user property to false', function (done) {
  95. this.call(err => {
  96. expect(err).to.not.exist
  97. sinon.assert.calledWith(
  98. this.AnalyticsManager.setUserPropertyForUser,
  99. this.user_id,
  100. 'beta-program',
  101. false
  102. )
  103. done()
  104. })
  105. })
  106. it('should not produce an error', function (done) {
  107. this.call(err => {
  108. expect(err).to.not.exist
  109. done()
  110. })
  111. })
  112. describe('when userUpdater produces an error', function () {
  113. beforeEach(function () {
  114. this.UserUpdater.promises.updateUser.rejects()
  115. })
  116. it('should produce an error', function (done) {
  117. this.call(err => {
  118. expect(err).to.exist
  119. expect(err).to.be.instanceof(Error)
  120. done()
  121. })
  122. })
  123. })
  124. })
  125. })