2021-10-09 23:02:51 +00:00
|
|
|
const indexMap = [
|
|
|
|
'firstCall',
|
|
|
|
'secondCall',
|
|
|
|
'thirdCall',
|
|
|
|
]
|
|
|
|
|
|
|
|
export function spy() {
|
|
|
|
return stub()
|
|
|
|
}
|
|
|
|
|
|
|
|
export function stub(returnFunc = null) {
|
|
|
|
if (returnFunc && typeof(returnFunc) !== 'function') {
|
|
|
|
throw new Error('stub() was called with non-function argument')
|
|
|
|
}
|
2022-01-11 09:37:05 +00:00
|
|
|
let manualReturners = new Map()
|
|
|
|
let nextManual = null
|
2021-10-09 23:02:51 +00:00
|
|
|
let returner = returnFunc ? returnFunc : null
|
|
|
|
let calls = []
|
|
|
|
let func = function(...args) {
|
|
|
|
func.called = true
|
|
|
|
calls.push(args)
|
2021-10-10 03:24:41 +00:00
|
|
|
func.lastCall = args
|
2021-10-09 23:02:51 +00:00
|
|
|
if (func.callCount < indexMap.length) {
|
|
|
|
func[indexMap[func.callCount]] = args
|
|
|
|
}
|
|
|
|
func.callCount++
|
2022-01-11 09:37:05 +00:00
|
|
|
if (manualReturners.has(func.callCount -1)) {
|
|
|
|
return manualReturners.get(func.callCount -1)(...args)
|
|
|
|
}
|
2021-10-09 23:02:51 +00:00
|
|
|
if (returner) {
|
|
|
|
return returner(...args)
|
|
|
|
}
|
|
|
|
}
|
2021-10-10 03:24:41 +00:00
|
|
|
func.lastCall = null
|
2021-10-09 23:02:51 +00:00
|
|
|
func.called = false
|
|
|
|
func.callCount = 0
|
2022-01-11 09:37:05 +00:00
|
|
|
|
|
|
|
func.getCall = function(i) {
|
2021-10-09 23:02:51 +00:00
|
|
|
return calls[i]
|
|
|
|
}
|
2022-01-11 09:37:05 +00:00
|
|
|
func.getCallN = function(i) {
|
|
|
|
return calls[i - 1]
|
|
|
|
}
|
|
|
|
func.onCall = function(i) {
|
|
|
|
if (i !== null && typeof(i) !== 'number') {
|
|
|
|
throw new Error('onCall must be called with either null or number')
|
|
|
|
}
|
|
|
|
nextManual = i
|
|
|
|
return func
|
|
|
|
}
|
|
|
|
func.onCallN = function(i) {
|
|
|
|
return func.onCall(i - 1)
|
|
|
|
}
|
|
|
|
|
2021-10-09 23:20:01 +00:00
|
|
|
func.reset = function() {
|
2021-10-10 03:24:41 +00:00
|
|
|
func.lastCall = null
|
2021-10-09 23:20:01 +00:00
|
|
|
func.called = false
|
|
|
|
func.callCount = 0
|
2022-01-11 09:37:05 +00:00
|
|
|
manualReturners.clear()
|
2021-10-09 23:20:01 +00:00
|
|
|
for (let i = 0; i < indexMap.length; i++) {
|
|
|
|
func[indexMap[i]] = null
|
|
|
|
}
|
|
|
|
returner = returnFunc ? returnFunc : null
|
|
|
|
calls.splice(0, calls.length)
|
2022-01-11 09:37:05 +00:00
|
|
|
return func
|
2021-10-09 23:20:01 +00:00
|
|
|
}
|
2021-10-09 23:02:51 +00:00
|
|
|
func.returns = function(data) {
|
2022-01-11 09:37:05 +00:00
|
|
|
func.returnWith(function() { return data })
|
|
|
|
return func
|
2021-10-09 23:02:51 +00:00
|
|
|
}
|
|
|
|
func.throws = function(data) {
|
2022-01-11 09:37:05 +00:00
|
|
|
func.returnWith(function() { throw data })
|
|
|
|
return func
|
2021-10-09 23:02:51 +00:00
|
|
|
}
|
|
|
|
func.resolves = function(data) {
|
2022-01-11 09:37:05 +00:00
|
|
|
func.returnWith(function() { return Promise.resolve(data) })
|
|
|
|
return func
|
2021-10-09 23:02:51 +00:00
|
|
|
}
|
|
|
|
func.rejects = function(data) {
|
2022-01-11 09:37:05 +00:00
|
|
|
func.returnWith(function() { return Promise.reject(data) })
|
|
|
|
return func
|
|
|
|
}
|
|
|
|
func.returnWith = function(returnFunc) {
|
|
|
|
if (typeof(returnFunc) !== 'function') {
|
|
|
|
throw new Error('stub() was called with non-function argument')
|
|
|
|
}
|
|
|
|
if (nextManual !== null) {
|
|
|
|
manualReturners.set(nextManual, returnFunc)
|
|
|
|
nextManual = null
|
|
|
|
} else {
|
|
|
|
returner = returnFunc
|
|
|
|
}
|
|
|
|
return func
|
2021-10-09 23:02:51 +00:00
|
|
|
}
|
|
|
|
for (let i = 0; i < indexMap.length; i++) {
|
|
|
|
func[indexMap[i]] = null
|
|
|
|
}
|
|
|
|
return func
|
|
|
|
}
|