ReferalFeaturesTests.js 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. const SandboxedModule = require('sandboxed-module')
  2. const sinon = require('sinon')
  3. const { expect } = require('chai')
  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. requires: {
  12. '../../models/User': {
  13. User: (this.User = {}),
  14. },
  15. '@overleaf/settings': (this.Settings = {}),
  16. },
  17. })
  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(async 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().returns({
  38. exec: sinon.stub().resolves(stubbedUser),
  39. })
  40. this.features = await this.ReferalFeatures.promises.getBonusFeatures(
  41. this.user_id
  42. )
  43. })
  44. it('should get the users number of refered user', function () {
  45. this.User.findOne.calledWith({ _id: this.user_id }).should.equal(true)
  46. })
  47. it('should return the features', function () {
  48. expect(this.features).to.equal(this.Settings.bonus_features[3])
  49. })
  50. })
  51. describe('when the user is not at a bonus level', function () {
  52. beforeEach(async function () {
  53. this.refered_user_count = 0
  54. this.Settings.bonus_features = {
  55. 1: {
  56. collaborators: 3,
  57. dropbox: false,
  58. versioning: false,
  59. },
  60. }
  61. this.User.findOne = sinon.stub().returns({
  62. exec: sinon
  63. .stub()
  64. .resolves({ refered_user_count: this.refered_user_count }),
  65. })
  66. this.features = await this.ReferalFeatures.promises.getBonusFeatures(
  67. this.user_id
  68. )
  69. })
  70. it('should get the users number of refered user', function () {
  71. this.User.findOne.calledWith({ _id: this.user_id }).should.equal(true)
  72. })
  73. it('should return an empty feature set', function () {
  74. expect(this.features).to.be.empty
  75. })
  76. })
  77. })