-
-
Save telekosmos/915bcf3fcddef053fbedc0768a753bdd to your computer and use it in GitHub Desktop.
Angular/chai-as-promised (real) setup
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Test | |
*/ | |
// from http://plnkr.co/edit/WAvvu99uLhVRmdlwRWDv | |
describe("Promise", function() { | |
var $q, | |
intervalRef; | |
beforeEach(module(function(_$exceptionHandlerProvider_) { | |
_$exceptionHandlerProvider_.mode('log'); | |
})); | |
beforeEach(angular.mock.inject(function(_$rootScope_, _$q_) { | |
$q = _$q_; | |
intervalRef = setInterval(function() { | |
_$rootScope_.$apply(); | |
console.log("doing interval"); | |
}, 1); | |
})); | |
afterEach(function() { | |
clearInterval(intervalRef); | |
}); | |
it("should resolve promise and eventually return", function() { | |
var promise = $q(function(resolve, reject) { | |
resolve("result"); | |
}); | |
return promise.should.eventually.deep.equal("result"); | |
}); | |
it("should error when not resolving", function() { | |
var promise = $q(function(resolve, reject) {}); | |
return promise.should.eventually.deep.equal("result"); | |
}); | |
it("should error when resolving with the wrong value", function() { | |
var promise = $q(function(resolve, reject) { | |
resolve("something different"); | |
}); | |
return promise.should.eventually.deep.equal("result"); | |
}); | |
it("should error when actually rejected", function() { | |
var promise = $q(function(resolve, reject) { | |
reject("result"); | |
}); | |
return promise.should.eventually.deep.equal("result"); | |
}); | |
it("should resolve multiple promises", function() { | |
var fst = $q(function(resolve, reject) { | |
reject("result1"); | |
}), | |
snd = $q.defer(); | |
snd.resolve("result2"); | |
var all = $q.all([ | |
fst, | |
snd.promise | |
]); | |
return all.should.be.rejected; | |
}); | |
it("should handle multiple promises when one is rejected and transformed", function() { | |
var fst = $q(function(resolve, reject) { | |
reject("result1"); | |
}), | |
snd = $q.defer(); | |
snd.resolve("result2"); | |
fst = fst.catch(function(a) { | |
return a; | |
}) | |
var all = $q.all([ | |
fst, | |
snd.promise | |
]); | |
return all.should.become(["result1", "result2"]); | |
}); | |
it("should handle multiple promise checks", function() { | |
var fst = $q(function(resolve, reject) { | |
reject("result1"); | |
}), | |
snd = $q.defer(); | |
snd.resolve("result2"); | |
return $q.all([ | |
fst.should.be.rejected, | |
snd.promise.should.become("result2"), | |
]).should.be.fullfilled; | |
}); | |
it("should reject promise and eventually return", function() { | |
return $q.reject("result1") | |
.should.be.rejectedWith("result1") | |
}); | |
it("should error when promise rejected with wrong value", function() { | |
return $q.reject("result2") | |
.should.be.rejectedWith("result1") | |
}); | |
it("should error when expecting fullfilled and is rejected", function() { | |
return $q.reject("result1") | |
.should.not.be.rejected; | |
}); | |
it("should reject multiple promises as expected", function() { | |
var promise = $q.reject("rejected1"); | |
var all = $q.all([ | |
promise, | |
$q(function(resolve) { | |
resolve("1"); | |
}) | |
]); | |
return all.should.be.rejected; | |
}); | |
}); | |
/** | |
* Run | |
*/ | |
mocha.run(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<link data-require="mocha@*" data-semver="2.2.5" rel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/mocha/2.2.5/mocha.css" /> | |
<script data-require="mocha@*" data-semver="2.2.5" src="//cdnjs.cloudflare.com/ajax/libs/mocha/2.2.5/mocha.js"></script> | |
<script data-require="mocha-chai@*" data-semver="3.0.0" src="//cdnjs.cloudflare.com/ajax/libs/chai/3.0.0/chai.js"></script> | |
<script src="http://rawgit.com/domenic/chai-as-promised/master/lib/chai-as-promised.js"></script> | |
<script data-require="[email protected]" data-semver="1.4.0" src="https://code.angularjs.org/1.4.0/angular.js"></script> | |
</head> | |
<body> | |
<div id="mocha"></div> | |
<script> | |
/** | |
* Configure Mocha | |
*/ | |
mocha.setup("bdd"); | |
chai.should(); | |
</script> | |
<script data-require="[email protected]" data-semver="1.4.0" src="https://code.angularjs.org/1.4.0/angular-mocks.js"></script> | |
<script src="angular-chai-as-promised.js"></script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment