test.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. 'use strict'
  2. var assert = require('assert')
  3. var stubs = require('./')
  4. var tess = require('tess')
  5. tess('init', function(it) {
  6. it('is a function', function() {
  7. assert.equal(typeof stubs, 'function')
  8. })
  9. it('throws without obj || method', function() {
  10. assert.throws(function() {
  11. stubs()
  12. }, /must provide/)
  13. assert.throws(function() {
  14. stubs({})
  15. }, /must provide/)
  16. })
  17. })
  18. tess('stubs', function(it) {
  19. it('stubs a method with a noop', function() {
  20. var originalCalled = false
  21. var obj = {}
  22. obj.method = function() {
  23. originalCalled = true
  24. }
  25. stubs(obj, 'method')
  26. obj.method()
  27. assert(!originalCalled)
  28. })
  29. it('accepts an override', function() {
  30. var originalCalled = false
  31. var obj = {}
  32. obj.method = function() {
  33. originalCalled = true
  34. }
  35. var replacementCalled = false
  36. stubs(obj, 'method', function() {
  37. replacementCalled = true
  38. })
  39. obj.method()
  40. assert(!originalCalled)
  41. assert(replacementCalled)
  42. })
  43. it('returns value of overridden method call', function() {
  44. var overriddenUniqueVal = {}
  45. var obj = {}
  46. obj.method = function() {}
  47. stubs(obj, 'method', { callthrough: true }, function() {
  48. return overriddenUniqueVal
  49. })
  50. assert.strictEqual(obj.method(), overriddenUniqueVal)
  51. })
  52. it('calls through to original method', function() {
  53. var originalCalled = false
  54. var obj = {}
  55. obj.method = function() {
  56. originalCalled = true
  57. }
  58. var replacementCalled = false
  59. stubs(obj, 'method', { callthrough: true }, function() {
  60. replacementCalled = true
  61. })
  62. obj.method()
  63. assert(originalCalled)
  64. assert(replacementCalled)
  65. })
  66. it('returns value of original method call', function() {
  67. var uniqueVal = Date.now()
  68. var obj = {}
  69. obj.method = function() {
  70. return uniqueVal
  71. }
  72. stubs(obj, 'method', { callthrough: true }, function() {})
  73. assert.equal(obj.method(), uniqueVal)
  74. })
  75. it('returns calls override and returns original value', function() {
  76. var uniqueVal = {}
  77. var overrideWasCalled = false
  78. var obj = {}
  79. obj.method = function() {
  80. return uniqueVal
  81. }
  82. stubs(obj, 'method', { callthrough: true }, function() {
  83. // does not return anything
  84. overrideWasCalled = true
  85. })
  86. assert.strictEqual(obj.method(), uniqueVal)
  87. assert.strictEqual(overrideWasCalled, true)
  88. })
  89. it('returns value of overridden method call', function() {
  90. var uniqueVal = {}
  91. var overriddenUniqueVal = {}
  92. var obj = {}
  93. obj.method = function() {
  94. return uniqueVal
  95. }
  96. stubs(obj, 'method', { callthrough: true }, function() {
  97. return overriddenUniqueVal
  98. })
  99. assert.strictEqual(obj.method(), overriddenUniqueVal)
  100. })
  101. it('stops calling stub after n calls', function() {
  102. var timesToCall = 5
  103. var timesCalled = 0
  104. var obj = {}
  105. obj.method = function() {
  106. assert.equal(timesCalled, timesToCall)
  107. }
  108. stubs(obj, 'method', { calls: timesToCall }, function() {
  109. timesCalled++
  110. })
  111. obj.method() // 1 (stub)
  112. obj.method() // 2 (stub)
  113. obj.method() // 3 (stub)
  114. obj.method() // 4 (stub)
  115. obj.method() // 5 (stub)
  116. obj.method() // 6 (original)
  117. })
  118. it('calls stub in original context of obj', function() {
  119. var secret = 'brownies'
  120. function Class() {
  121. this.method = function() {}
  122. this.secret = secret
  123. }
  124. var cl = new Class()
  125. stubs(cl, 'method', function() {
  126. assert.equal(this.secret, secret)
  127. })
  128. cl.method()
  129. })
  130. })