Say we have a function like this in flow.js which is a web component so all are functions inside a class within this file.
change(){
this.getParams();
this.loadData();
}
I want to write a test case that can test change() and confirm whether these two functions have been invoked. I tried many approaches but it dint work. One of the approaches I tried is:
it('change() test', function () {
const sandbox = sinon.createSandbox();
sandbox.spy(el);
el.change();
assert(el.change.calledOnce); // output is true
assert(el.getParams.calledOnce); // output is false
assert(el.loadData.calledOnce); // output is false
}
el is the object/webcomponent.
Could you please let me know what is wrong here and what is the right implementation for this scenario ?
You should add spy to el.getParams() and el.loadData() methods.
E.g.
flow.js:
class Flow {
change() {
this.getParams();
this.loadData();
}
getParams() {}
loadData() {}
}
module.exports = Flow;
flow.test.js:
const Flow = require('./flow');
const sinon = require('sinon');
describe('flow', () => {
it('should pass', () => {
const sandbox = sinon.createSandbox();
const flow = new Flow();
sandbox.spy(flow, 'getParams');
sandbox.spy(flow, 'loadData');
flow.change();
sinon.assert.calledOnce(flow.getParams);
sinon.assert.calledOnce(flow.loadData);
});
});