ReferalFeaturesTests.js 2.3 KB

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