UserOnboardingControllerTests.js 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. const SandboxedModule = require('sandboxed-module')
  2. const modulePath =
  3. '../../../../app/src/Features/User/UserOnboardingController.js'
  4. const { ObjectId } = require('mongodb')
  5. const sinon = require('sinon')
  6. describe('UserOnboardingController', function() {
  7. beforeEach(function() {
  8. this.date = new Date().getTime()
  9. sinon.useFakeTimers(this.date)
  10. this.users = [
  11. {
  12. _id: ObjectId('00000001f037be01a0e3a541')
  13. },
  14. {
  15. _id: ObjectId('00000001f037be01a0e3a542')
  16. },
  17. {
  18. _id: ObjectId('00000001f037be01a0e3a543')
  19. }
  20. ]
  21. this.mongojs = {
  22. db: {
  23. users: {
  24. find: sinon
  25. .stub()
  26. .returns({ toArray: sinon.stub().yields(null, this.users) })
  27. }
  28. },
  29. ObjectId: ObjectId
  30. }
  31. this.logger = {
  32. log() {}
  33. }
  34. this.UserUpdater = {
  35. updateUser: sinon.stub().callsArgWith(2, null)
  36. }
  37. this.EmailHandler = {
  38. sendEmail: sinon.stub().callsArgWith(2)
  39. }
  40. this.UserOnboardingController = SandboxedModule.require(modulePath, {
  41. requires: {
  42. '../../infrastructure/mongojs': this.mongojs,
  43. './UserUpdater': this.UserUpdater,
  44. '../Email/EmailHandler': this.EmailHandler,
  45. 'logger-sharelatex': this.logger
  46. }
  47. })
  48. this.req = {}
  49. this.res = {
  50. setTimeout: sinon.stub()
  51. }
  52. })
  53. it('sends onboarding emails', function(done) {
  54. this.res.send = ids => {
  55. ids.length.should.equal(3)
  56. this.mongojs.db.users.find.calledOnce.should.equal(true)
  57. this.EmailHandler.sendEmail.calledThrice.should.equal(true)
  58. this.UserUpdater.updateUser.calledThrice.should.equal(true)
  59. for (var i = 0; i < 3; i++) {
  60. this.UserUpdater.updateUser
  61. .calledWith(
  62. this.users[0]._id,
  63. sinon.match({
  64. $set: { onboardingEmailSentAt: new Date(this.date) }
  65. })
  66. )
  67. .should.equal(true)
  68. }
  69. done()
  70. }
  71. this.UserOnboardingController.sendRecentSignupOnboardingEmails(
  72. this.req,
  73. this.res
  74. )
  75. })
  76. })