Skip to content

Instantly share code, notes, and snippets.

@jasperkuperus
Forked from wilsonwc/stateMock.js
Last active March 31, 2017 13:11
Show Gist options
  • Save jasperkuperus/e96717f1f25bb71afff8 to your computer and use it in GitHub Desktop.
Save jasperkuperus/e96717f1f25bb71afff8 to your computer and use it in GitHub Desktop.
angular.module('stateMock',[]);
angular.module('stateMock').service("$state", function($q){
this.expectedTransitions = [];
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);
return $q.when();
}
this.go = this.transitionTo;
this.expectTransitionTo = function(stateName){
this.expectedTransitions.push(stateName);
}
this.ensureAllTransitionsHappened = function(){
if(this.expectedTransitions.length > 0){
throw Error("Not all transitions happened!");
}
}
});
@rima1712
Copy link

the same function don't work with es6
angular.module('stateMock',[])
angular.module('stateMock')
.service("$state", ($q) => {
this.expectedTransitions = []
this.transitionTo = (stateName) => {
if(this.expectedTransitions.length > 0){
let 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 )
}
let deferred = $q.defer()
let promise = deferred.promise
deferred.resolve()
return promise
}
this.go = this.transitionTo

    this.expectTransitionTo = (stateName) => {
        this.expectedTransitions.push(stateName)
    }


    this.ensureAllTransitionsHappened = () => {
        if(this.expectedTransitions.length > 0){
            throw Error("Not all transitions happened!")
        }
    }
})

Any idea please ??

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