ReferalHandlerTests.mjs 2.6 KB

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