2021-12-14 08:24:53 -05:00
|
|
|
import sysend from 'sysend'
|
|
|
|
import sinon from 'sinon'
|
|
|
|
|
|
|
|
const sysendSpy = sinon.spy(sysend)
|
|
|
|
|
|
|
|
function resetHistory() {
|
|
|
|
for (const method of Object.keys(sysendSpy)) {
|
|
|
|
if (sysendSpy[method].resetHistory) sysendSpy[method].resetHistory()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// sysends sends and receives custom calls in the background. This Helps
|
|
|
|
// filtering them out
|
|
|
|
function getDetachCalls(method) {
|
|
|
|
return sysend[method]
|
|
|
|
.getCalls()
|
|
|
|
.filter(call => call.args[0].startsWith('detach-'))
|
|
|
|
}
|
|
|
|
|
|
|
|
function getLastDetachCall(method) {
|
|
|
|
return getDetachCalls(method).pop()
|
|
|
|
}
|
|
|
|
|
|
|
|
function getLastBroacastMessage() {
|
|
|
|
return getLastDetachCall('broadcast').args[1]
|
|
|
|
}
|
|
|
|
|
2022-03-31 07:22:36 -04:00
|
|
|
function getAllBroacastMessages() {
|
|
|
|
return getDetachCalls('broadcast')
|
|
|
|
}
|
|
|
|
|
2021-12-14 08:24:53 -05:00
|
|
|
// this fakes receiving a message by calling the handler add to `on`. A bit
|
|
|
|
// funky, but works for now
|
|
|
|
function receiveMessage(message) {
|
|
|
|
getLastDetachCall('on').args[1](message)
|
|
|
|
}
|
|
|
|
|
|
|
|
export default {
|
|
|
|
spy: sysendSpy,
|
|
|
|
resetHistory,
|
|
|
|
getDetachCalls,
|
|
|
|
getLastDetachCall,
|
|
|
|
getLastBroacastMessage,
|
2022-03-31 07:22:36 -04:00
|
|
|
getAllBroacastMessages,
|
2021-12-14 08:24:53 -05:00
|
|
|
receiveMessage,
|
|
|
|
}
|