I'm developing an app with ChaplinJS, I have to make some unit tests for some services in the application, so far I was able to create most of the tests, but I have issues with mediators, I've been searching how to test them.
Here's a snippet
it('SHOULD execute page:goToURL once when called', () => {
var spy = sinon.spy();
Chaplin.mediator.subscribe('page:goToURL', spy);
Chaplin.mediator.publish('page:goToURL', '/someUrl');
assert(spy.called);
});
My idea here, is to subscribe to that event (page:goToURL) a spy, and if I trigger it, the spy will know and that way and can make assertions, and find out if the event is triggering properly.
I guess the problem is with this line Chaplin.mediator.publish('page:goToURL', '/someUrl');
This line is not the same context as the previous line (the subscribe) that's why it's invoking directly the callback from the service file instead of the mock that I've defined. (I made some console log's in the tested file and it's executing that directly)
The following shows an actual snippet of the code that I'm testing.
initialize: function() {
Chaplin.mediator.subscribe('page:goToURL', this.callback, this);
}
If a trigger page:goToURL, the callback will be executed.
Any ideas on how to solve this or some guidance on how to continue? I have sinon chai + mocha as testing libraries. thanks!
Aucun commentaire:
Enregistrer un commentaire