|
@@ -8,7 +8,11 @@ const modulePath = require('path').join(
|
|
|
|
|
|
|
|
describe('Referal handler', function () {
|
|
describe('Referal handler', function () {
|
|
|
beforeEach(function () {
|
|
beforeEach(function () {
|
|
|
- this.User = { findById: sinon.stub() }
|
|
|
|
|
|
|
+ this.User = {
|
|
|
|
|
+ findById: sinon.stub().returns({
|
|
|
|
|
+ exec: sinon.stub(),
|
|
|
|
|
+ }),
|
|
|
|
|
+ }
|
|
|
this.handler = SandboxedModule.require(modulePath, {
|
|
this.handler = SandboxedModule.require(modulePath, {
|
|
|
requires: {
|
|
requires: {
|
|
|
'../../models/User': {
|
|
'../../models/User': {
|
|
@@ -20,73 +24,68 @@ describe('Referal handler', function () {
|
|
|
})
|
|
})
|
|
|
|
|
|
|
|
describe('getting refered user_ids', function () {
|
|
describe('getting refered user_ids', function () {
|
|
|
- it('should get the user from mongo and return the refered users array', function (done) {
|
|
|
|
|
|
|
+ it('should get the user from mongo and return the refered users array', async function () {
|
|
|
const user = {
|
|
const user = {
|
|
|
refered_users: ['1234', '312312', '3213129'],
|
|
refered_users: ['1234', '312312', '3213129'],
|
|
|
refered_user_count: 3,
|
|
refered_user_count: 3,
|
|
|
}
|
|
}
|
|
|
- this.User.findById.callsArgWith(2, null, user)
|
|
|
|
|
|
|
+ this.User.findById.returns({
|
|
|
|
|
+ exec: sinon.stub().resolves(user),
|
|
|
|
|
+ })
|
|
|
|
|
+
|
|
|
|
|
+ const {
|
|
|
|
|
+ referedUsers: passedReferedUserIds,
|
|
|
|
|
+ referedUserCount: passedReferedUserCount,
|
|
|
|
|
+ } = await this.handler.promises.getReferedUsers(this.user_id)
|
|
|
|
|
|
|
|
- this.handler.getReferedUsers(
|
|
|
|
|
- this.user_id,
|
|
|
|
|
- (err, passedReferedUserIds, passedReferedUserCount) => {
|
|
|
|
|
- expect(err).not.to.exist
|
|
|
|
|
- passedReferedUserIds.should.deep.equal(user.refered_users)
|
|
|
|
|
- passedReferedUserCount.should.equal(3)
|
|
|
|
|
- done()
|
|
|
|
|
- }
|
|
|
|
|
- )
|
|
|
|
|
|
|
+ passedReferedUserIds.should.deep.equal(user.refered_users)
|
|
|
|
|
+ passedReferedUserCount.should.equal(3)
|
|
|
})
|
|
})
|
|
|
|
|
|
|
|
- it('should return an empty array if it is not set', function (done) {
|
|
|
|
|
|
|
+ it('should return an empty array if it is not set', async function () {
|
|
|
const user = {}
|
|
const user = {}
|
|
|
- this.User.findById.callsArgWith(2, null, user)
|
|
|
|
|
|
|
+ this.User.findById.returns({
|
|
|
|
|
+ exec: sinon.stub().resolves(user),
|
|
|
|
|
+ })
|
|
|
|
|
|
|
|
- this.handler.getReferedUsers(
|
|
|
|
|
- this.user_id,
|
|
|
|
|
- (err, passedReferedUserIds, passedReferedUserCount) => {
|
|
|
|
|
- expect(err).not.to.exist
|
|
|
|
|
- passedReferedUserIds.length.should.equal(0)
|
|
|
|
|
- done()
|
|
|
|
|
- }
|
|
|
|
|
- )
|
|
|
|
|
|
|
+ const { referedUsers: passedReferedUserIds } =
|
|
|
|
|
+ await this.handler.promises.getReferedUsers(this.user_id)
|
|
|
|
|
+
|
|
|
|
|
+ passedReferedUserIds.length.should.equal(0)
|
|
|
})
|
|
})
|
|
|
|
|
|
|
|
- it('should return a zero count if neither it or the array are set', function (done) {
|
|
|
|
|
|
|
+ it('should return a zero count if neither it or the array are set', async function () {
|
|
|
const user = {}
|
|
const user = {}
|
|
|
- this.User.findById.callsArgWith(2, null, user)
|
|
|
|
|
|
|
+ this.User.findById.returns({
|
|
|
|
|
+ exec: sinon.stub().resolves(user),
|
|
|
|
|
+ })
|
|
|
|
|
|
|
|
- this.handler.getReferedUsers(
|
|
|
|
|
- this.user_id,
|
|
|
|
|
- (err, passedReferedUserIds, passedReferedUserCount) => {
|
|
|
|
|
- expect(err).not.to.exist
|
|
|
|
|
- passedReferedUserCount.should.equal(0)
|
|
|
|
|
- done()
|
|
|
|
|
- }
|
|
|
|
|
- )
|
|
|
|
|
|
|
+ const { referedUserCount: passedReferedUserCount } =
|
|
|
|
|
+ await this.handler.promises.getReferedUsers(this.user_id)
|
|
|
|
|
+
|
|
|
|
|
+ passedReferedUserCount.should.equal(0)
|
|
|
})
|
|
})
|
|
|
|
|
|
|
|
- it('should return the array length if count is not set', function (done) {
|
|
|
|
|
|
|
+ it('should return the array length if count is not set', async function () {
|
|
|
const user = { refered_users: ['1234', '312312', '3213129'] }
|
|
const user = { refered_users: ['1234', '312312', '3213129'] }
|
|
|
- this.User.findById.callsArgWith(2, null, user)
|
|
|
|
|
|
|
+ this.User.findById.returns({
|
|
|
|
|
+ exec: sinon.stub().resolves(user),
|
|
|
|
|
+ })
|
|
|
|
|
|
|
|
- this.handler.getReferedUsers(
|
|
|
|
|
- this.user_id,
|
|
|
|
|
- (err, passedReferedUserIds, passedReferedUserCount) => {
|
|
|
|
|
- expect(err).not.to.exist
|
|
|
|
|
- passedReferedUserCount.should.equal(3)
|
|
|
|
|
- done()
|
|
|
|
|
- }
|
|
|
|
|
- )
|
|
|
|
|
- })
|
|
|
|
|
|
|
+ const { referedUserCount: passedReferedUserCount } =
|
|
|
|
|
+ await this.handler.promises.getReferedUsers(this.user_id)
|
|
|
|
|
|
|
|
- it('should error if finding the user fails', function (done) {
|
|
|
|
|
- this.User.findById.callsArgWith(2, new Error('user not found'))
|
|
|
|
|
|
|
+ passedReferedUserCount.should.equal(3)
|
|
|
|
|
+ })
|
|
|
|
|
|
|
|
- this.handler.getReferedUsers(this.user_id, err => {
|
|
|
|
|
- err.should.match(/user not found/)
|
|
|
|
|
- done()
|
|
|
|
|
|
|
+ it('should error if finding the user fails', async function () {
|
|
|
|
|
+ this.User.findById.returns({
|
|
|
|
|
+ exec: sinon.stub().rejects(new Error('user not found')),
|
|
|
})
|
|
})
|
|
|
|
|
+
|
|
|
|
|
+ expect(
|
|
|
|
|
+ this.handler.promises.getReferedUsers(this.user_id)
|
|
|
|
|
+ ).to.be.rejectedWith('user not found')
|
|
|
})
|
|
})
|
|
|
})
|
|
})
|
|
|
})
|
|
})
|