sinon: now supports reset()

This commit is contained in:
Jonatan Nilsson 2021-10-09 23:20:01 +00:00
parent f54fb7d51a
commit 9992c681db
2 changed files with 116 additions and 0 deletions

View file

@ -30,6 +30,15 @@ export function stub(returnFunc = null) {
func.onCall = function(i) {
return calls[i]
}
func.reset = function() {
func.called = false
func.callCount = 0
for (let i = 0; i < indexMap.length; i++) {
func[indexMap[i]] = null
}
returner = returnFunc ? returnFunc : null
calls.splice(0, calls.length)
}
func.returns = function(data) {
returner = function() { return data }
}

View file

@ -4,6 +4,35 @@ import { spy, stub } from '../index.mjs'
[spy, stub].forEach(function(tester, i) {
t.describe(`#${i === 0 ? 'spy' : 'stub'}()`, function() {
t.test('should support reset', function() {
let spyer = tester()
assert.strictEqual(spyer.callCount, 0)
assert.notOk(spyer.called)
assert.strictEqual(spyer.firstCall, null)
assert.strictEqual(spyer.secondCall, null)
assert.strictEqual(spyer.thirdCall, null)
spyer(1)
spyer(2)
spyer(3)
spyer(4)
spyer(5)
assert.notStrictEqual(spyer.callCount, 0)
assert.ok(spyer.called)
assert.notStrictEqual(spyer.firstCall, null)
assert.notStrictEqual(spyer.secondCall, null)
assert.notStrictEqual(spyer.thirdCall, null)
spyer.reset()
assert.strictEqual(spyer.callCount, 0)
assert.notOk(spyer.called)
assert.strictEqual(spyer.firstCall, null)
assert.strictEqual(spyer.secondCall, null)
assert.strictEqual(spyer.thirdCall, null)
})
t.test('should keep track of call count', function() {
let spyer = tester()
assert.strictEqual(spyer.callCount, 0)
@ -179,4 +208,82 @@ t.describe('#stub()', function() {
assert.throws(function() { stub().returnWith(123) })
assert.throws(function() { stub().returnWith('asdf') })
})
t.test('should support reset on returning', function() {
let s = stub()
assert.strictEqual(s.callCount, 0)
assert.notOk(s.called)
assert.strictEqual(s.firstCall, null)
assert.strictEqual(s.secondCall, null)
assert.strictEqual(s.thirdCall, null)
assert.strictEqual(s(), undefined)
s.returns('test')
assert.strictEqual(s(1), 'test')
assert.strictEqual(s(2), 'test')
assert.strictEqual(s(3), 'test')
assert.strictEqual(s(4), 'test')
assert.strictEqual(s(5), 'test')
assert.notStrictEqual(s.callCount, 0)
assert.ok(s.called)
assert.notStrictEqual(s.firstCall, null)
assert.notStrictEqual(s.secondCall, null)
assert.notStrictEqual(s.thirdCall, null)
s.reset()
assert.strictEqual(s.callCount, 0)
assert.notOk(s.called)
assert.strictEqual(s.firstCall, null)
assert.strictEqual(s.secondCall, null)
assert.strictEqual(s.thirdCall, null)
assert.strictEqual(s(), undefined)
s.returnWith(function() { return 'yes' })
assert.strictEqual(s(), 'yes')
s.reset()
assert.strictEqual(s(), undefined)
})
t.test('reset should default though to constructor returner', function() {
let s = stub(function() { return 'success' })
assert.strictEqual(s.callCount, 0)
assert.notOk(s.called)
assert.strictEqual(s.firstCall, null)
assert.strictEqual(s.secondCall, null)
assert.strictEqual(s.thirdCall, null)
assert.strictEqual(s(), 'success')
s.returns('test')
assert.strictEqual(s(1), 'test')
assert.strictEqual(s(2), 'test')
assert.strictEqual(s(3), 'test')
assert.strictEqual(s(4), 'test')
assert.strictEqual(s(5), 'test')
assert.notStrictEqual(s.callCount, 0)
assert.ok(s.called)
assert.notStrictEqual(s.firstCall, null)
assert.notStrictEqual(s.secondCall, null)
assert.notStrictEqual(s.thirdCall, null)
s.reset()
assert.strictEqual(s.callCount, 0)
assert.notOk(s.called)
assert.strictEqual(s.firstCall, null)
assert.strictEqual(s.secondCall, null)
assert.strictEqual(s.thirdCall, null)
assert.strictEqual(s(), 'success')
s.returnWith(function() { return 'yes' })
assert.strictEqual(s(), 'yes')
s.reset()
assert.strictEqual(s(), 'success')
})
})