Hello, I have unit test failed strangely I guess the mocking is not correct:
exports.updatePec = catchAsync(async (req, res, next) => {
const { id } = req.params
const {
nom_pec,
num_cin_nrc_pec,
adresse_pec,
email_pec,
type_pec,
num_telephone_pec
} = req.body
const newPecValues = {}
const {
id: currentPecId,
nom_pec: currentNomPec,
num_cin_nrc_pec: currentCinPec,
adresse_pec: currentAddressPec,
email_pec: currentEmailPec,
type_pec: currentTypePec,
num_telephone_pec: currentNumTelephonePec
} = (await partie_en_conflit.findByPk(id)) || {}
if (currentPecId) {
//adding the new columns values to update
if (nom_pec && nom_pec !== currentNomPec) {
newPecValues.nom_pec = nom_pec
}
if (num_cin_nrc_pec && num_cin_nrc_pec !== currentCinPec) {
newPecValues.num_cin_nrc_pec = num_cin_nrc_pec
}
if (adresse_pec && adresse_pec !== currentAddressPec) {
newPecValues.adresse_pec = adresse_pec
}
if (email_pec && email_pec !== currentEmailPec) {
newPecValues.email_pec = email_pec
}
if (type_pec && type_pec !== currentTypePec) {
newPecValues.type_pec = type_pec
}
if (num_telephone_pec && num_telephone_pec !== currentNumTelephonePec) {
newPecValues.num_telephone_pec = num_telephone_pec
}
newPecValues.client_id = req.user.client_id
if (Object.keys(newPecValues).length === 1) {
return res.status(204).json({
status: 'success',
data: {}
})
}
//perform the update
const nouveauPec = await partie_en_conflit.update(
{ ...newPecValues },
{
where: {
id: id
},
returning: true
}
)
console.log('nouveauPec')
if (!nouveauPec[0]) {
return next(new AppError(`the record to update not found`, 404))
}
//successfull pec update
const updatedPecToReturn = await partie_en_conflit.findOne({
attributes: {
exclude: ['updatedAt', 'createdAt']
},
where: { id: id },
distinct: true
})
return res.status(200).json({
status: 'success',
data: updatedPecToReturn
})
}
return next(new AppError(`the record to update not found`, 404))
})
this is the unit test for update pec:
const pecController = require('./pecController')
jest.mock('../models')
const db = require('../models')
describe('pecController', () => {
describe('updatePec', () => {
test('Expect to respond with 200 code and pecs data when pec exist && provided new values valid', async () => {
const currentPecvalues = {
id: 'fakePecUuId',
nom_pec: 'pec1',
num_cin_nrc_pec: 'A9999',
adresse_pec: 'fake address',
email_pec: 'fakeemail@fake.com',
type_pec: 'client',
num_telephone_pec: '06748591'
}
const newValues = { nom_pec: 'new pec name' }
db.partie_en_conflit.findByPk = jest
.fn()
.mockImplementation(() => Promise.resolve({ ...currentPecvalues }))
db.partie_en_conflit.update = jest.fn().mockImplementation(() =>
Promise.resolve([
1,
[
{
...currentPecvalues,
...newValues
}
]
])
)
db.partie_en_conflit.findOne = jest
.fn()
.mockImplementation(() =>
Promise.resolve({ ...currentPecvalues, ...newValues })
)
const req = {
query: {},
user: { client_id: 'fakeUuid' },
params: { id: 'fakePecUuid' },
body: newValues
}
const res = {
status: jest.fn(() => res),
json: jest.fn()
}
const next = jest.fn()
pecController.updatePec(req, res, next)
expect(next).not.toHaveBeenCalled()
expect(db.partie_en_conflit.findByPk).toHaveBeenCalled()
expect(db.partie_en_conflit.update).toHaveBeenCalled()
expect(db.partie_en_conflit.findOne).toHaveBeenCalled()
expect(res.status).toHaveBeenCalledTimes(1)
expect(res.status).toHaveBeenCalledWith(200)
expect(res.json).toHaveBeenCalledTimes(1)
expect(res.json).toHaveBeenCalledWith({
status: 'success',
data: { ...currentPecvalues, ...newValues }
})
})
})
})
the test fails with this msg:
I’m really confused, thanks for any help