{"size": 3400, "ext": "js", "max_stars_repo_path": "test/unit/src/Referal/ReferalHandlerTests.js", "max_stars_repo_name": "kingzevin/web-microservice", "max_stars_repo_stars_event_min_datetime": "2019-10-28T12:56:35.000Z", "max_stars_repo_stars_event_max_datetime": "2019-10-28T12:56:35.000Z", "max_issues_repo_path": "test/unit/src/Referal/ReferalHandlerTests.js", "max_issues_repo_name": "kingzevin/web-microservice", "max_issues_repo_issues_event_min_datetime": "2020-06-18T15:37:47.000Z", "max_issues_repo_issues_event_max_datetime": "2022-03-26T19:40:56.000Z", "max_forks_repo_path": "test/unit/src/Referal/ReferalHandlerTests.js", "max_forks_repo_name": "kingzevin/web-microservice", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": 1, "max_issues_count": 2, "max_forks_count": null, "avg_line_length": 29.0598290598, "max_line_length": 92, "alphanum_fraction": 0.615}
/* eslint-disable
    handle-callback-err,
    max-len,
    no-return-assign,
    no-unused-vars,
*/
// TODO: This file was created by bulk-decaffeinate.
// Fix any style issues and re-enable lint.
/*
 * decaffeinate suggestions:
 * DS102: Remove unnecessary code created because of implicit returns
 * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
 */
const SandboxedModule = require('sandboxed-module')
const assert = require('assert')
require('chai').should()
const sinon = require('sinon')
const modulePath = require('path').join(
  __dirname,
  '../../../../app/src/Features/Referal/ReferalHandler.js'
)

describe('Referal handler', function() {
  beforeEach(function() {
    this.User = { findById: sinon.stub() }
    this.handler = SandboxedModule.require(modulePath, {
      globals: {
        console: console
      },
      requires: {
        'logger-sharelatex': {
          log() {},
          err() {}
        },
        '../../models/User': {
          User: this.User
        }
      }
    })
    return (this.user_id = '12313')
  })

  describe('getting refered user_ids', function() {
    it('should get the user from mongo and return the refered users array', function(done) {
      const user = {
        refered_users: ['1234', '312312', '3213129'],
        refered_user_count: 3
      }
      this.User.findById.callsArgWith(1, null, user)

      return this.handler.getReferedUsers(
        this.user_id,
        (err, passedReferedUserIds, passedReferedUserCount) => {
          passedReferedUserIds.should.deep.equal(user.refered_users)
          passedReferedUserCount.should.equal(3)
          return done()
        }
      )
    })

    it('should return an empty array if it is not set', function(done) {
      const user = {}
      this.User.findById.callsArgWith(1, null, user)

      return this.handler.getReferedUsers(
        this.user_id,
        (err, passedReferedUserIds, passedReferedUserCount) => {
          passedReferedUserIds.length.should.equal(0)
          return done()
        }
      )
    })

    it('should return a zero count if netither it or the array are set', function(done) {
      const user = {}
      this.User.findById.callsArgWith(1, null, user)

      return this.handler.getReferedUsers(
        this.user_id,
        (err, passedReferedUserIds, passedReferedUserCount) => {
          passedReferedUserCount.should.equal(0)
          return done()
        }
      )
    })

    it('should return the array length if count is not set', function(done) {
      const user = { refered_users: ['1234', '312312', '3213129'] }
      this.User.findById.callsArgWith(1, null, user)

      return this.handler.getReferedUsers(
        this.user_id,
        (err, passedReferedUserIds, passedReferedUserCount) => {
          passedReferedUserCount.should.equal(3)
          return done()
        }
      )
    })

    it('should return the count if it differs from the array length', function(done) {
      const user = {
        refered_users: ['1234', '312312', '3213129'],
        refered_user_count: 5
      }
      this.User.findById.callsArgWith(1, null, user)

      return this.handler.getReferedUsers(
        this.user_id,
        (err, passedReferedUserIds, passedReferedUserCount) => {
          passedReferedUserCount.should.equal(5)
          return done()
        }
      )
    })
  })
})
