Scenario
I have built search form with 2 states: First state triggers the second one (onclick), and sets some state parameters with $state.go() function. Second state has resolve function which gets state parameter, and resolves search results.
This approach seems much cleaner way to do it, oposing to classic approach: inject the search service in state one, get results, save them in search service, change state and load results from injected service.
Question
How to make unit test for this ?
Initial idea is that I should make a test which "should trigger a locate with searchTerm and switch to results page". It fails because the states do not resolve properly with jasmine. Sometimes it's 'locked' in first state, in other scenarios state is always ''.
When I removed resolve block from configuration it works as expected, and tests pass, so the problem is obviously in resolve block. It's weird that I do not get 'unknown provider' errors though.
Any thoughts or ideas how to fix this?
Closest question on StackOverflow was this : Angular ui router unit testing (states to urls) But there user invokes state change manually, while in my scenario it's invoked by another function.
Unit test code:
describe('SearchController tests', function(){
var ctrl, $state, $rootScope, $httpBackend;
// instantiate the app to provide all the dependencies
beforeEach(module('MyApp'));
beforeEach(inject(function($controller, _$state_, _$rootScope_, _$httpBackend_) {
ctrl = $controller('SearchController', {});
$state = _$state_;
$httpBackend = _$httpBackend_;
$rootScope = _$rootScope_;
// mocking the views
// (instead of including stateMock script from the post, just to simplify)
$httpBackend.expectGET("search.html").respond("<input ng-model='vm.searchTerm'><button ng-click='vm.search()'>Search</button>");
$httpBackend.expectGET("search-results.html").respond("<div ng-repeat='res in vm.results'> {{ res }}</div>");
$state.go('search');
$rootScope.$digest();
}));
it('should trigger a search with searchTerm "rabbit" and change state to search results page', function(){
ctrl.searchTerm = 'rabbit';
$state.go('search');
ctrl.search();
$rootScope.$digest();
expect($state.current.name).toBe('search-results');
})
});
Plunker