ReferalHandlerTests.js 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. const SandboxedModule = require('sandboxed-module')
  2. const { expect } = require('chai')
  3. const sinon = require('sinon')
  4. const modulePath = require('path').join(
  5. __dirname,
  6. '../../../../app/src/Features/Referal/ReferalHandler.js'
  7. )
  8. describe('Referal handler', function () {
  9. beforeEach(function () {
  10. this.User = {
  11. findById: sinon.stub().returns({
  12. exec: sinon.stub(),
  13. }),
  14. }
  15. this.handler = SandboxedModule.require(modulePath, {
  16. requires: {
  17. '../../models/User': {
  18. User: this.User,
  19. },
  20. },
  21. })
  22. this.user_id = '12313'
  23. })
  24. describe('getting refered user_ids', function () {
  25. it('should get the user from mongo and return the refered users array', async function () {
  26. const user = {
  27. refered_users: ['1234', '312312', '3213129'],
  28. refered_user_count: 3,
  29. }
  30. this.User.findById.returns({
  31. exec: sinon.stub().resolves(user),
  32. })
  33. const {
  34. referedUsers: passedReferedUserIds,
  35. referedUserCount: passedReferedUserCount,
  36. } = await this.handler.promises.getReferedUsers(this.user_id)
  37. passedReferedUserIds.should.deep.equal(user.refered_users)
  38. passedReferedUserCount.should.equal(3)
  39. })
  40. it('should return an empty array if it is not set', async function () {
  41. const user = {}
  42. this.User.findById.returns({
  43. exec: sinon.stub().resolves(user),
  44. })
  45. const { referedUsers: passedReferedUserIds } =
  46. await this.handler.promises.getReferedUsers(this.user_id)
  47. passedReferedUserIds.length.should.equal(0)
  48. })
  49. it('should return a zero count if neither it or the array are set', async function () {
  50. const user = {}
  51. this.User.findById.returns({
  52. exec: sinon.stub().resolves(user),
  53. })
  54. const { referedUserCount: passedReferedUserCount } =
  55. await this.handler.promises.getReferedUsers(this.user_id)
  56. passedReferedUserCount.should.equal(0)
  57. })
  58. it('should return the array length if count is not set', async function () {
  59. const user = { refered_users: ['1234', '312312', '3213129'] }
  60. this.User.findById.returns({
  61. exec: sinon.stub().resolves(user),
  62. })
  63. const { referedUserCount: passedReferedUserCount } =
  64. await this.handler.promises.getReferedUsers(this.user_id)
  65. passedReferedUserCount.should.equal(3)
  66. })
  67. it('should error if finding the user fails', async function () {
  68. this.User.findById.returns({
  69. exec: sinon.stub().rejects(new Error('user not found')),
  70. })
  71. expect(
  72. this.handler.promises.getReferedUsers(this.user_id)
  73. ).to.be.rejectedWith('user not found')
  74. })
  75. })
  76. })