ReferalHandlerTests.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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 SandboxedModule = require('sandboxed-module')
  15. const assert = require('assert')
  16. require('chai').should()
  17. const sinon = require('sinon')
  18. const modulePath = require('path').join(
  19. __dirname,
  20. '../../../../app/src/Features/Referal/ReferalHandler.js'
  21. )
  22. describe('Referal handler', function() {
  23. beforeEach(function() {
  24. this.User = { findById: sinon.stub() }
  25. this.handler = SandboxedModule.require(modulePath, {
  26. globals: {
  27. console: console
  28. },
  29. requires: {
  30. 'logger-sharelatex': {
  31. log() {},
  32. err() {}
  33. },
  34. '../../models/User': {
  35. User: this.User
  36. }
  37. }
  38. })
  39. return (this.user_id = '12313')
  40. })
  41. describe('getting refered user_ids', function() {
  42. it('should get the user from mongo and return the refered users array', function(done) {
  43. const user = {
  44. refered_users: ['1234', '312312', '3213129'],
  45. refered_user_count: 3
  46. }
  47. this.User.findById.callsArgWith(1, null, user)
  48. return this.handler.getReferedUsers(this.user_id, function(
  49. err,
  50. passedReferedUserIds,
  51. passedReferedUserCount
  52. ) {
  53. passedReferedUserIds.should.deep.equal(user.refered_users)
  54. passedReferedUserCount.should.equal(3)
  55. return done()
  56. })
  57. })
  58. it('should return an empty array if it is not set', function(done) {
  59. const user = {}
  60. this.User.findById.callsArgWith(1, null, user)
  61. return this.handler.getReferedUsers(this.user_id, function(
  62. err,
  63. passedReferedUserIds,
  64. passedReferedUserCount
  65. ) {
  66. passedReferedUserIds.length.should.equal(0)
  67. return done()
  68. })
  69. })
  70. it('should return a zero count if netither it or the array are set', function(done) {
  71. const user = {}
  72. this.User.findById.callsArgWith(1, null, user)
  73. return this.handler.getReferedUsers(this.user_id, function(
  74. err,
  75. passedReferedUserIds,
  76. passedReferedUserCount
  77. ) {
  78. passedReferedUserCount.should.equal(0)
  79. return done()
  80. })
  81. })
  82. it('should return the array length if count is not set', function(done) {
  83. const user = { refered_users: ['1234', '312312', '3213129'] }
  84. this.User.findById.callsArgWith(1, null, user)
  85. return this.handler.getReferedUsers(this.user_id, function(
  86. err,
  87. passedReferedUserIds,
  88. passedReferedUserCount
  89. ) {
  90. passedReferedUserCount.should.equal(3)
  91. return done()
  92. })
  93. })
  94. it('should return the count if it differs from the array length', function(done) {
  95. const user = {
  96. refered_users: ['1234', '312312', '3213129'],
  97. refered_user_count: 5
  98. }
  99. this.User.findById.callsArgWith(1, null, user)
  100. return this.handler.getReferedUsers(this.user_id, function(
  101. err,
  102. passedReferedUserIds,
  103. passedReferedUserCount
  104. ) {
  105. passedReferedUserCount.should.equal(5)
  106. return done()
  107. })
  108. })
  109. })
  110. })