Skip to content

Instantly share code, notes, and snippets.

@alexanderjeurissen
Forked from morrislaptop/stateMock.js
Last active December 15, 2015 08:35
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save alexanderjeurissen/1c45ca694ad333baeddd to your computer and use it in GitHub Desktop.
Save alexanderjeurissen/1c45ca694ad333baeddd to your computer and use it in GitHub Desktop.
//GistID:1c45ca694ad333baeddd
'use strict';
angular.module('stateMock', []);
angular.module('stateMock').service("$state", function($q) {
this.expectedTransitions = [];
this.current = {};
this.transitionTo = function(stateName) {
if (this.expectedTransitions.length > 0) {
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);
this.current.name = stateName;
var deferred = $q.defer();
var promise = deferred.promise;
deferred.resolve();
return promise;
};
this.go = this.transitionTo;
this.is = function(stateName) {
if (this.current.name === stateName)
return true;
return false
};
this.expectTransitionTo = function(stateName) {
this.expectedTransitions.push(stateName);
};
this.ensureAllTransitionsHappened = function() {
if (this.expectedTransitions.length > 0) {
throw Error("Not all transitions happened!");
}
};
});
@alexanderjeurissen
Copy link
Author

includes $state.current.name implementation from https://gist.github.com/morrislaptop/8c101b59878327d932d2

@elfadotonto
Copy link

added method for setting $state.current https://gist.github.com/elfadotonto/a35e63911029023a3e0f

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment