I have the following service and I want to write Unit tests for this service. However, I am exporting a function that is going to return the Service.
My main problem is, to keep things simple at the point where this service will be imported and used, I am using partials to pass in the model as dependency using Partials inside the init method.
const model = require('../models');
const SomeService = (model) => {
const getSomething = (id) => {
const [data, error] = model.getSomething(id);
if (error) {
return error;
}
return data;
};
const createSomething = (smt) => {
const [data, error] = model.createSomething(smt);
if (error) {
return error;
}
return data;
};
const deleteSomething = (id) => {
const [data, error] = model.deleteSomething(id);
if (error) {
return error;
}
return data;
};
return {
getSomething,
createSomething,
deleteSomething,
};
};
const init = (model) => () => SomeService(model);
const getSomeService = init(model);
module.exports = getSomeService;
The controller that will be consuming this service may or may not pass some other params but the thing is it definitely should not be passing the model to initialize this service. Hence, I am using the partials.
While writing Unit tests for this service, how do I mock the model? Are there any changes required to make this more testable?