Skip to content

Instantly share code, notes, and snippets.

@anilcse
Forked from wilsonwc/stateMock.js
Last active September 10, 2016 05:18
Show Gist options
  • Save anilcse/aa52aae775a32cfddf8981337f8f723c to your computer and use it in GitHub Desktop.
Save anilcse/aa52aae775a32cfddf8981337f8f723c to your computer and use it in GitHub Desktop.
Angular Mock for properly resolving ui-router $state in Karma unit tests
angular.module('stateMock',[]);
angular.module('stateMock').service('$state', function ($q) {
this.expectedTransitions = [];
this.transitionTo = function(stateName) {
if(this.expectedTransitions.length) {
var expectedState = this.expectedTransitions.shift();
if (expectedState !== stateName) {
throw Error('Expected transition to state: ' + expectedState + ' but transitioned to ' + stateName);
}
} else {
throw Error('No more transitions were expected! Tried to transition to ' + stateName);
}
console.log("Mock transition to: " + stateName);
var deferred = $q.defer();
var promise = deferred.promise;
deferred.resolve();
return promise;
};
this.go = this.transitionTo;
this.expectTransitionTo = function (stateName) {
this.expectedTransitions.push(stateName);
};
this.ensureAllTransitionsHappened = function () {
if (this.expectedTransitions.length) {
throw Error('Not all transitions happened!');
}
};
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment