I have these 2 following methods, which I created a test using jest to the one called removerFromDb
initializeData = async (collectionName) => {
getSomeDataFromDatabase()
this.#data = data;
}
removerFromDb = async (collectionName, _id) => {
await database(async (db) => {
const collection = db.collection(collectionName);
await collection.deleteOne({ _id: ObjectId(_id) });
});
this.initializeData(collectionName);
}
my test looks like that:
const data = require('../data/testingData.json');
it('Should remove item from list', async () => {
let dataCopy= data;
database
.mockImplementationOnce(() => {
dataCopy= dataCopy.filter((item) => item._id != _id);
})
.mockImplementationOnce(() => dataCopy);
await DataManager.removerFromDb("", 1); // the second param serves as _id
expect(DataManager.getData().length).toEqual(5);
});
Now, if you take a look at removerFromDb, you can see that it accept a second parameter named _id. But in my test when I call the method with 1 the mockImplementationOnce function I created doesn't get the value, instead it gets undefined. I found a way to make it work by doing this:
database
.mockImplementationOnce(() => {
const _id = 1; // notice I added this line
replicatedData = replicatedData.filter((item) => item._id != _id);
})
But it's less ideal and I don't understand why I'm not getting the value I sent through the parent function removerFromDb.
Is there any way to do it? Why isn't it working the first way?
Aucun commentaire:
Enregistrer un commentaire