Skip to content

Instantly share code, notes, and snippets.

@Alex-Werner
Created December 26, 2015 10:57
Show Gist options
  • Save Alex-Werner/8d620e7835446f248adf to your computer and use it in GitHub Desktop.
Save Alex-Werner/8d620e7835446f248adf to your computer and use it in GitHub Desktop.
ChainedPromise Angular
<!--https://github.com/mohsen1/json-formatter -->
<link href="scripts/vendor/json-formatter.min.css" rel="stylesheet" type="text/css">
<div class="container">
<div class="jumbotron">
<h1>Test interface</h1>
<p>Total progression :
<div id="totalProgression" class="progress"></div>
<button type="button" class="btn btn-lg btn-danger" ng-click="runAllTest()">Run All</button>
<div class="form-group">
<label for="testLog">Test log:</label>
<textarea class="form-control" rows="5" id="testLog"></textarea>
</div>
</p>
<hr>
<div class="form-group" style="height: 350px; overflow: auto">
<label for="requestResp">Response For : {{request.name}}</label></label>
<json-formatter id="requestResp" json="request.jsonData" open="6"></json-formatter>
</div>
<hr>
<p>SYSTEM API : </p>
<button type="button" class="btn btn-lg btn-danger" ng-click="runCleanDatabase()">Clean Database</button>
<button type="button" class="btn btn-lg btn-success" ng-click="runBackupDatabase()">Backup Database</button>
<button type="button" class="btn btn-lg btn-warning" ng-click="runDisplayDatabase()">Display Database</button>
<p>Users API : </p>
<button type="button" class="btn btn-lg btn-info" ng-click="runCreateDemoAccount()">Create Demo Account</button>
<button type="button" class="btn btn-lg btn-info" ng-click="runGetDemoAccount()">Get Demo Account</button>
<button type="button" class="btn btn-lg btn-info" ng-click="runEditDemoAccount()">Edit Demo Account</button>
<button type="button" class="btn btn-lg btn-info" ng-click="runDeleteDemoAccount()">Delete Demo Account</button>
<p>Project API : </p>
<button type="button" class="btn btn-lg btn-info" ng-click="runCreateProject()">Create Project</button>
<button type="button" class="btn btn-lg btn-info" ng-click="runGetProject()">Get Project Info</button>
<button type="button" class="btn btn-lg btn-info" ng-click="runGetProjects()">Get Projects Info</button>
<button type="button" class="btn btn-lg btn-info" ng-click="runEditProject()">Edit Project</button>
<button type="button" class="btn btn-lg btn-info" ng-click="runDeleteProject()">Delete Project</button>
<p>Metric API :</p>
</div>
</div>
function chainedPromise(promises,limit,callback){
var i= 0,
next = function(){
var promise = promises[i];
var promiseName = promise[0];
$scope.appendToLog('Test N°'+i+" - "+promiseName+" - Result :");
var runPromise = promise[1]();
runPromise.then(function(data){
if(callback){
callback(false,data,promiseName)
}
},
function(data){
if(callback){
callback(true,data,promiseName)
}
}).finally(function(){
i++;
if(i<limit)
next();
});
};
next();
};
$scope.runAllTest = function(){
chainedPromise($scope.promises,$scope.promisesNb,function(error,data,promiseName){
if(error){
$scope.appendToLog(' Failed\n');
$scope.setTotalBar("fail");
}else{
$scope.appendToLog(' Success\n');
$scope.setTotalBar("success");
} });
};
var defaultCallbackForUnitTest = function(error,data,promiseName){
if(error)
$scope.appendToLog('Failed\n');
else{
$scope.appendToLog('Success\n');
$scope.request.name=promiseName;
$scope.request.jsonData=data;
}
}
//System
$scope.runCleanDatabase = function(){chainedPromise([["POST Clean Database",cleanDatabase]],1,defaultCallbackForUnitTest);}
$scope.runBackupDatabase=function(){chainedPromise([["POST Backup Database",backupDatabase]],1,defaultCallbackForUnitTest);}
$scope.runDisplayDatabase=function(){chainedPromise([["POST Display Database",displayDatabase]],1,defaultCallbackForUnitTest);}
//User
$scope.runCreateDemoAccount=function(){chainedPromise([["POST Create Demo Account",createDemoAccount]],1,defaultCallbackForUnitTest);}
$scope.runGetDemoAccount=function(){chainedPromise([["POST Get Demo Account",getDemoAccount]],1,defaultCallbackForUnitTest);}
$scope.runEditDemoAccount=function(){chainedPromise([["POST Edit Demo Account",editDemoAccount]],1,defaultCallbackForUnitTest);}
$scope.runDeleteDemoAccount=function(){chainedPromise([["POST Delete Demo Account",deleteDemoAccount]],1,defaultCallbackForUnitTest);}
//Project
$scope.runCreateProject=function(){chainedPromise([["POST Create Demo Account",createProject]],1,defaultCallbackForUnitTest);}
$scope.runGetProject=function(){chainedPromise([["POST Get Demo Account",getProject]],1,defaultCallbackForUnitTest);}
$scope.runGetProjects=function(){chainedPromise([["POST Get Projects",getProjects]],1,defaultCallbackForUnitTest);}
$scope.runEditProject=function(){chainedPromise([["POST Edit Demo Account",editProject]],1,defaultCallbackForUnitTest);}
$scope.runDeleteProject=function(){chainedPromise([["POST Delete Demo Account",deleteProject]],1,defaultCallbackForUnitTest);}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment