How can I test a handler with if / then blocks in a unit test for an Angular.js service?
I'm new to testing and I need to test a handler in my Angular.js service (using Jasmine and Mocha). This service validates the URL and redirects to the state main.loadbalancer.readonly
if the device number is read-only.
I'm not sure how to check the handler, since they are 2, if / then are blocked by the function, I have split them for each beforeEach(inject(function() {})
. Since they will be reused for multiple instances I thought it would be better and then call them callHandler1(....)
etc.
Now I am getting:
readonly service paste url with device 55, first load, will not go to main state
Γ’ ReferenceError: callHandler1 is not defined
Am I doing it right?
Services:
angular.module("main.loadbalancer").run(function($rootScope, $state, DeviceVal) {
return $rootScope.$on("$stateChangeStart", function(event, toState) {
var checkUrlDirectEdit, device;
device = window.location.hash.split("/")[2];
checkUrlDirectEdit = function() {
return DeviceVal.previousDevice !== device && toState.name !== "main" && DeviceVal.lb.id === "undefined";
};
if (typeof DeviceVal.lb.id === "undefined" && toState.name !== "main" || checkUrlDirectEdit()) {
event.preventDefault();
DeviceVal.previousDevice = device;
$state.transitionTo("main", {}, {
location: false
});
}
if (DeviceVal.readonly && toState.name !== "main.loadbalancer.readonly") {
event.preventDefault();
$state.transitionTo("main.loadbalancer.readonly", {
id: DeviceVal.lb.id
}, {
location: true,
reload: true
});
}
return window.addEventListener("hashchange", function() {
return location.reload();
});
});
});
Service test:
describe("readonly service", function() {
beforeEach(function() {
return module("main");
});
beforeEach(inject(function($rootScope, _$state_, _DeviceVal_) {
$rootScope = $rootScope;
$state = _$state_;
DeviceVal = _DeviceVal_;
spyOn($state, "transitionTo");
spyOn($rootScope, "$broadcast");
event = jasmine.createSpyObj("event", ["preventDefault"]);
event.preventDefault();
}));
beforeEach(inject(function() {
checkUrlDirectEdit = function(DeviceVal, toState) {
return DeviceVal.previousDevice !== device && toState.name !== "main" && DeviceVal.lb.id === "undefined";
}
}));
beforeEach(inject(function() {
function callHandler1(DeviceVal, toState, device) {
if (typeof DeviceVal.lb.id === "undefined" && toState.name !== "main" || checkUrlDirectEdit()) {
event.preventDefault();
DeviceVal.previousDevice = device;
$state.transitionTo("main", {}, {
location: false
});
}
}
}));
beforeEach(inject(function() {
function callHandler2(DeviceVal, toState, device) {
if (DeviceVal.readonly && toState.name !== "main.loadbalancer.readonly") {
event.preventDefault();
$state.transitionTo("main.loadbalancer.readonly", {
id: DeviceVal.lb.id
}, {
location: true,
reload: true
});
}
}
}));
it("paste url with device 55, first load, will not go to main state", function() {
device = 55;
DeviceVal.lb.id = undefined;
toState = { name: "not main" },
DeviceVal.previousDevice = undefined;
DeviceVal.readonly = false;
callHandler1(DeviceVal, toState, device);
callHandler2(DeviceVal, toState, device)
expect(event.preventDefault).toBeDefined();
});
it("paste url with device 56, first load, will go to main state", function() {
device = 56;
Device.lb.id = undefined;
toState = { name: "not main" },
DeviceVal.previousDevice = undefined;
DeviceVal.readonly = true;
callHandler1(DeviceVal, toState, device);
callHandler2(DeviceVal, toState, device)
expect(event.preventDefault).toHaveBeenCalled();
expect($state.transitionTo).toHaveBeenCalledWith("main.loadbalancer.readonly",
{ id: 55 },
{ location: true, reload: true });
});
});
source to share
No one has answered this question yet
Check out similar questions: