BetaProgramHandlerTests.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. /* eslint-disable
  2. handle-callback-err,
  3. max-len,
  4. no-return-assign,
  5. no-unused-vars,
  6. */
  7. // TODO: This file was created by bulk-decaffeinate.
  8. // Fix any style issues and re-enable lint.
  9. /*
  10. * decaffeinate suggestions:
  11. * DS102: Remove unnecessary code created because of implicit returns
  12. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  13. */
  14. const should = require('chai').should()
  15. const SandboxedModule = require('sandboxed-module')
  16. const assert = require('assert')
  17. const path = require('path')
  18. const modulePath = path.join(
  19. __dirname,
  20. '../../../../app/src/Features/BetaProgram/BetaProgramHandler'
  21. )
  22. const sinon = require('sinon')
  23. const { expect } = require('chai')
  24. describe('BetaProgramHandler', function() {
  25. beforeEach(function() {
  26. this.user_id = 'some_id'
  27. this.user = {
  28. _id: this.user_id,
  29. email: 'user@example.com',
  30. features: {},
  31. betaProgram: false,
  32. save: sinon.stub().callsArgWith(0, null)
  33. }
  34. return (this.handler = SandboxedModule.require(modulePath, {
  35. globals: {
  36. console: console
  37. },
  38. requires: {
  39. '../../models/User': {
  40. User: {
  41. findById: sinon.stub().callsArgWith(1, null, this.user)
  42. }
  43. },
  44. 'logger-sharelatex': (this.logger = {
  45. log: sinon.stub(),
  46. warn: sinon.stub(),
  47. err: sinon.stub()
  48. }),
  49. 'metrics-sharelatex': (this.logger = {
  50. inc: sinon.stub()
  51. })
  52. }
  53. }))
  54. })
  55. describe('optIn', function() {
  56. beforeEach(function() {
  57. this.user.betaProgram = false
  58. return (this.call = callback => {
  59. return this.handler.optIn(this.user_id, callback)
  60. })
  61. })
  62. it('should set betaProgram = true on user object', function(done) {
  63. return this.call(err => {
  64. this.user.betaProgram.should.equal(true)
  65. return done()
  66. })
  67. })
  68. it('should call user.save', function(done) {
  69. return this.call(err => {
  70. this.user.save.callCount.should.equal(1)
  71. return done()
  72. })
  73. })
  74. it('should not produce an error', function(done) {
  75. return this.call(err => {
  76. expect(err).to.equal(null)
  77. expect(err).to.not.be.instanceof(Error)
  78. return done()
  79. })
  80. })
  81. describe('when user.save produces an error', function() {
  82. beforeEach(function() {
  83. return this.user.save.callsArgWith(0, new Error('woops'))
  84. })
  85. it('should produce an error', function(done) {
  86. return this.call(err => {
  87. expect(err).to.not.equal(null)
  88. expect(err).to.be.instanceof(Error)
  89. return done()
  90. })
  91. })
  92. })
  93. })
  94. describe('optOut', function() {
  95. beforeEach(function() {
  96. this.user.betaProgram = true
  97. return (this.call = callback => {
  98. return this.handler.optOut(this.user_id, callback)
  99. })
  100. })
  101. it('should set betaProgram = true on user object', function(done) {
  102. return this.call(err => {
  103. this.user.betaProgram.should.equal(false)
  104. return done()
  105. })
  106. })
  107. it('should call user.save', function(done) {
  108. return this.call(err => {
  109. this.user.save.callCount.should.equal(1)
  110. return done()
  111. })
  112. })
  113. it('should not produce an error', function(done) {
  114. return this.call(err => {
  115. expect(err).to.equal(null)
  116. expect(err).to.not.be.instanceof(Error)
  117. return done()
  118. })
  119. })
  120. describe('when user.save produces an error', function() {
  121. beforeEach(function() {
  122. return this.user.save.callsArgWith(0, new Error('woops'))
  123. })
  124. it('should produce an error', function(done) {
  125. return this.call(err => {
  126. expect(err).to.not.equal(null)
  127. expect(err).to.be.instanceof(Error)
  128. return done()
  129. })
  130. })
  131. })
  132. })
  133. })