Advertisement
Guest User

Untitled

a guest
Aug 5th, 2015
181
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.88 KB | None | 0 0
  1. var _ = require("underscore")
  2. var Promise = require("bluebird")
  3. var chai = require('chai')
  4. var chaiAsPromised = require("chai-as-promised")
  5. var expect = chai.expect
  6. var sinon = require("sinon");
  7. var sinonChai = require("sinon-chai");
  8. chai.use(sinonChai);
  9. chai.use(chaiAsPromised)
  10. chai.should()
  11.  
  12. var example = function(throwError){
  13. return Promise.resolve(throwError).then(function(){
  14. if(throwError) throw new Error("test error")
  15. return throwError
  16. })
  17. }
  18.  
  19. Promise.prototype.thenCatch = function(fn){
  20. return this
  21. .catch(function(e){
  22. return e
  23. })
  24. .then(fn)
  25. }
  26.  
  27. describe("bluebird's promise", function(){
  28.  
  29. it("should produce desired result return rejectedWith error", function(done){
  30. example(true).should.eventually.be.rejectedWith(Error, "test error").notify(done)
  31. })
  32.  
  33. it("should produce desired result return equal false", function(done){
  34. example(false).should.eventually.be.equal(false).notify(done)
  35. })
  36.  
  37. it("should produce desired result .catch() expect error object", function(done){
  38. example(true).catch(function(value){
  39. expect(value).to.exist
  40. .and.be.instanceof(Error)
  41. .and.have.property('message', 'test error')
  42. }).then(done)
  43. })
  44.  
  45. it("should produce desired result .catch() should be called", function(done){
  46. var spy = sinon.spy();
  47. example(true).catch(spy).then(function(){
  48. expect(spy).to.have.been.called
  49. }).then(done).catch(done)
  50. })
  51.  
  52. it("should produce desired result promise does not throw error", function(){
  53. expect(function(){
  54. return example(true)
  55. }).to.not.throw
  56. })
  57.  
  58. it("should produce desired result .catch() does not throw error", function(){
  59. expect(function(){
  60. return example(true).catch()
  61. }).to.not.throw
  62. })
  63.  
  64. it("should produce undesired result .reflect() does not throw error", function(){
  65. expect(function(){
  66. return example(true).reflect()
  67. }).to.not.throw
  68. })
  69.  
  70. it("should produce undesired result .reflect() should be called", function(done){
  71. var spy = sinon.spy();
  72. example(true).reflect(spy).then(function(){
  73. expect(spy).to.have.not.been.called
  74. }).then(done).catch(done)
  75. })
  76.  
  77. it("should produce undesired result .finally() throws error", function(){
  78. expect(function(){
  79. return example(true).finally()
  80. }).to.throw
  81. })
  82.  
  83. it("should produce undesired result .done() throws error", function(){
  84. expect(function(){
  85. return example(true).done()
  86. }).to.throw
  87. })
  88.  
  89. it("should produce desired result .thenCatch() expect error object ", function(done){
  90. example(true).thenCatch(function(value){
  91. expect(value).to.exist
  92. .and.be.instanceof(Error)
  93. .and.have.property('message', 'test error')
  94. }).thenCatch(done)
  95. })
  96.  
  97. it("should produce desired result .thenCatch() expect equal false", function(done){
  98. example(false).thenCatch(function(value){
  99. expect(value).to.equal(false)
  100. }).thenCatch(done)
  101. })
  102.  
  103. })
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement