vendredi 31 juillet 2020

Writing Mocha Chai Test cases for NodeJs Controllers

I am new to unit testing. I am trying to write test cases for controller.js files for nodejs microservices files. I am unable to understand where I am going wrong. Always throws an error "TypeError: Cannot read property 'empId' of undefined" for 2 of these properties.

This is the controller code:

const crmgDetails = db.crmgResource_details;
const employeeProposal = db.employee_Proposal;
const Op = db.Sequelize.Op;
const raDetails = db.crmgRaSheet_entity;
let results = [];
Sequelize = require('sequelize')

exports.findOne = (req, res) => {
    console.log(req.body.empId);
    crmgDetails.findAll({where: {
        resEmployeeNumber: req.body.empId
    }
    })
       .then(data => {
           res.send(data);
        })
        .catch(err => {
            res.status(500).send({
                message:
                    err.message || "Some error occurred while retrieving tutorials."
      });
    });
};

exports.findMatchingDemandsForRmg = (req,res) => {
    let proposedDemands = [];
    
    employeeProposal.findAll({
        where: {
            emp_id: req.body.empId,
            demandSbu : req.body.sbu
        }
    }).then(proposedEmployee => {
        console.log('proposedEmployee',proposedEmployee);
        if(proposedEmployee.length === 0){
            crmgDetails.findAll({
                where: {
                    resEmployeeNumber: req.body.empId,
                    demandSbu: req.body.sbu
                }
            }).then(matchingDemands => {
                
                console.log('matchingDemands  ',matchingDemands)
                proposedDemands = matchingDemands;
                
            })
        }
        else{
            console.log("crmg Employee")
            console.log(proposedEmployee)
            
            
                for(let employee of proposedEmployee){
                
                        crmgDetails.findOne({
                            where: {
                                demandUid: employee.demandUid,
                                resEmployeeNumber: req.body.empId,
                                demandSbu: req.body.sbu
                            }
                        }).then( crmgProposed=> {
                            proposedDemands.push(crmgProposed);
                        })
                
                    
                }
            
        }
        setTimeout(() => {
            console.log(proposedDemands)
        res.send(proposedDemands);
        }, 3000);
        
        
    }).catch((err)=>{
        res.status(500).send({
            message:
              err.message || "Some error occurred while retrieving tutorials."
          });
    })

}

exports.getResourceAllocationDetails = (req,res) => {
    employeeProposal.findAll({
        include: {
            model: raDetails
        },
        where: Sequelize.and(
            {activeFlag : true},
            Sequelize.or({status:"Accepted By RMG"},
            {status:"Rejected"}
        ))
    }).then(employees => {
        res.send(employees)
    })
}

This is the test file I tried to write without my head:

const CrmgRaSheetModel = require('../controllers/crmgResource_Details.controller')
describe('Check for succcessful fetech API call', () => {
  it('property getResourceAllocationDetails should be called', async () => {
    CrmgRaSheetModel.getResourceAllocationDetails((res) => {
      expect(res).to.be.an('object')
              return res.json()
          })
  });
  it('property findMatchingDemandsForRmg should be called', async () => {
    CrmgRaSheetModel.findMatchingDemandsForRmg((res) => {
      expect(res).to.be.an('object')
              return res.json()
          })
  });
  it('property findOne should be called', async () => {
    CrmgRaSheetModel.findOne((res) => {
      expect(res).to.be.an('object')
              return res.json()
          })
  })
})

Aucun commentaire:

Enregistrer un commentaire