ReferalFeatures.test.mjs 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. import { vi, expect } from 'vitest'
  2. import sinon from 'sinon'
  3. import path from 'node:path'
  4. const modulePath = path.join(
  5. import.meta.dirname,
  6. '../../../../app/src/Features/Referal/ReferalFeatures.mjs'
  7. )
  8. describe('ReferalFeatures', function () {
  9. beforeEach(async function (ctx) {
  10. vi.doMock('../../../../app/src/models/User', () => ({
  11. User: (ctx.User = {}),
  12. }))
  13. vi.doMock('@overleaf/settings', () => ({
  14. default: (ctx.Settings = {}),
  15. }))
  16. ctx.ReferalFeatures = (await import(modulePath)).default
  17. ctx.referal_id = 'referal-id-123'
  18. ctx.referal_medium = 'twitter'
  19. ctx.user_id = 'user-id-123'
  20. ctx.new_user_id = 'new-user-id-123'
  21. })
  22. describe('getBonusFeatures', function () {
  23. beforeEach(async function (ctx) {
  24. ctx.refered_user_count = 3
  25. ctx.Settings.bonus_features = {
  26. 3: {
  27. collaborators: 3,
  28. dropbox: false,
  29. versioning: false,
  30. },
  31. }
  32. const stubbedUser = {
  33. refered_user_count: ctx.refered_user_count,
  34. features: { collaborators: 1, dropbox: false, versioning: false },
  35. }
  36. ctx.User.findOne = sinon.stub().returns({
  37. exec: sinon.stub().resolves(stubbedUser),
  38. })
  39. ctx.features = await ctx.ReferalFeatures.promises.getBonusFeatures(
  40. ctx.user_id
  41. )
  42. })
  43. it('should get the users number of refered user', function (ctx) {
  44. ctx.User.findOne.calledWith({ _id: ctx.user_id }).should.equal(true)
  45. })
  46. it('should return the features', function (ctx) {
  47. expect(ctx.features).to.equal(ctx.Settings.bonus_features[3])
  48. })
  49. })
  50. describe('when the user is not at a bonus level', function () {
  51. beforeEach(async function (ctx) {
  52. ctx.refered_user_count = 0
  53. ctx.Settings.bonus_features = {
  54. 1: {
  55. collaborators: 3,
  56. dropbox: false,
  57. versioning: false,
  58. },
  59. }
  60. ctx.User.findOne = sinon.stub().returns({
  61. exec: sinon
  62. .stub()
  63. .resolves({ refered_user_count: ctx.refered_user_count }),
  64. })
  65. ctx.features = await ctx.ReferalFeatures.promises.getBonusFeatures(
  66. ctx.user_id
  67. )
  68. })
  69. it('should get the users number of refered user', function (ctx) {
  70. ctx.User.findOne.calledWith({ _id: ctx.user_id }).should.equal(true)
  71. })
  72. it('should return an empty feature set', function (ctx) {
  73. expect(ctx.features).to.be.empty
  74. })
  75. })
  76. })