overleaf/services/web/test/frontend/helpers/sysend.js
Timothée Alby 4171f9bd92 Merge pull request #8953 from overleaf/ae-pdf-synctex-tests
Fix and enable synctex controls tests

GitOrigin-RevId: 67174b79ed81cf67552fb63447b9c6aa0a99e9fd
2022-07-21 08:04:04 +00:00

53 lines
1.2 KiB
JavaScript

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]
}
function getAllBroacastMessages() {
return getDetachCalls('broadcast')
}
// 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)
}
function getMessageWithEvent(eventName) {
return getAllBroacastMessages()
.map(item => item.args[1])
.find(item => item.event === eventName)
}
export default {
spy: sysendSpy,
resetHistory,
getDetachCalls,
getLastDetachCall,
getLastBroacastMessage,
getAllBroacastMessages,
getMessageWithEvent,
receiveMessage,
}