I'm new to jasmine and using angularjs as a framework.
How can i test that a $(window).trigger
"haveBeenCalledWith":
Code to test (it's a function in an angularjs service):
start: function (serverUrl, callbackFunc) {
// initialize communication manager and when ready start the worklist server
commManager.initAsync(serverUrl, function (isConnected) {
if (isConnected) {
var msgHeader = new protocolMessageHeader(userSessionGuid, messageCommandName.START_WL_SESSION + "_RQ");
var dataSet = { $type: 10, UserSessionGuid: userSessionGuid, WLSessionGuid: wlSessionGuid };
commManager.sendMessage(new protocolMessage(msgHeader, dataSet), function (data) {
if (callbackFunc) {
callbackFunc(data);
}
});
} else {
**$(window).trigger(events.onShowModalDialog, {
title: "Worklist Server Error",
body: "Can not establish connection to Worklist server",
type: generalStatusEnum.ERROR
});**
}
});
}
I made isConnected
to be false and i want to test that $(window).trigger
was called with the above parameters
Its always a problem when your code to test depends on other libraries/frameworks. So in your case, using $(window) as a global event bus is the root that it makes the code hard to test. There are some solution for this:
Let $()
return a mock where you can spy on the trigger
method. This will make your test code a bit more verbose but you can keep your actual code:
var trigger = jasmine.createSpy();
spyOn(window, '$').andReturn({trigger: trigger});
yourLib.start('url', callback);
expect(trigger).toHaveBeenCalledWith(events.onShowModalDialog, {
title: "Worklist Server Error",
body: "Can not establish connection to Worklist server",
type: generalStatusEnum.ERROR
});
In your example its easy cause you use $
only once, but think about having different uses of jquery in function will it make really hard or impossible to test the function.
A better way would be to pass the event bus into your function, or in the constructor of class. So for example your start function could look like this:
start: function (serverUrl, callbackFunc, eventBus) {
commManager.initAsync(serverUrl, function (isConnected) {
if (isConnected) {
var msgHeader = new protocolMessageHeader(userSessionGuid, messageCommandName.START_WL_SESSION + "_RQ");
var dataSet = { $type: 10, UserSessionGuid: userSessionGuid, WLSessionGuid: wlSessionGuid };
commManager.sendMessage(new protocolMessage(msgHeader, dataSet), function (data) {
if (callbackFunc) {
callbackFunc(data);
}
});
} else {
eventBus.trigger(events.onShowModalDialog, {
title: "Worklist Server Error",
body: "Can not establish connection to Worklist server",
type: generalStatusEnum.ERROR
});
}
});
}
And the test would look like this:
var trigger = jasmine.createSpy();
yourLib.start('url', callback, trigger);
expect(trigger).toHaveBeenCalledWith(events.onShowModalDialog, {
title: "Worklist Server Error",
body: "Can not establish connection to Worklist server",
type: generalStatusEnum.ERROR
});
Edit
Just realize that all your code is wrapped in a callback, so you have also spy on commManager.initAsync
, and call the callback:
spyOn(commManager, 'initAsync');
var callback = commManager.initAsync.mostRecentCall.args[1];
callback();