ReferalFeaturesTests.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. const SandboxedModule = require('sandboxed-module')
  2. require('chai').should()
  3. const sinon = require('sinon')
  4. const modulePath = require('path').join(
  5. __dirname,
  6. '../../../../app/src/Features/Referal/ReferalFeatures.js'
  7. )
  8. describe('ReferalFeatures', function() {
  9. beforeEach(function() {
  10. this.ReferalFeatures = SandboxedModule.require(modulePath, {
  11. globals: {
  12. console: console
  13. },
  14. requires: {
  15. '../../models/User': {
  16. User: (this.User = {})
  17. },
  18. 'settings-sharelatex': (this.Settings = {}),
  19. 'logger-sharelatex': {
  20. log() {},
  21. err() {}
  22. }
  23. }
  24. })
  25. this.callback = sinon.stub()
  26. this.referal_id = 'referal-id-123'
  27. this.referal_medium = 'twitter'
  28. this.user_id = 'user-id-123'
  29. this.new_user_id = 'new-user-id-123'
  30. })
  31. describe('getBonusFeatures', function() {
  32. beforeEach(function() {
  33. this.refered_user_count = 3
  34. this.Settings.bonus_features = {
  35. '3': {
  36. collaborators: 3,
  37. dropbox: false,
  38. versioning: false
  39. }
  40. }
  41. const stubbedUser = {
  42. refered_user_count: this.refered_user_count,
  43. features: { collaborators: 1, dropbox: false, versioning: false }
  44. }
  45. this.User.findOne = sinon.stub().callsArgWith(2, null, stubbedUser)
  46. this.ReferalFeatures.getBonusFeatures(this.user_id, this.callback)
  47. })
  48. it('should get the users number of refered user', function() {
  49. this.User.findOne.calledWith({ _id: this.user_id }).should.equal(true)
  50. })
  51. it('should call the callback with the features', function() {
  52. this.callback
  53. .calledWith(null, this.Settings.bonus_features[3])
  54. .should.equal(true)
  55. })
  56. })
  57. describe('when the user is not at a bonus level', function() {
  58. beforeEach(function() {
  59. this.refered_user_count = 0
  60. this.Settings.bonus_features = {
  61. '1': {
  62. collaborators: 3,
  63. dropbox: false,
  64. versioning: false
  65. }
  66. }
  67. this.User.findOne = sinon
  68. .stub()
  69. .callsArgWith(2, null, { refered_user_count: this.refered_user_count })
  70. this.ReferalFeatures.getBonusFeatures(this.user_id, this.callback)
  71. })
  72. it('should get the users number of refered user', function() {
  73. this.User.findOne.calledWith({ _id: this.user_id }).should.equal(true)
  74. })
  75. it('should call the callback with no features', function() {
  76. this.callback.calledWith(null, {}).should.equal(true)
  77. })
  78. })
  79. })