sysend.js 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. import sysend from 'sysend'
  2. import sinon from 'sinon'
  3. const sysendSpy = sinon.spy(sysend)
  4. function resetHistory() {
  5. for (const method of Object.keys(sysendSpy)) {
  6. if (sysendSpy[method].resetHistory) sysendSpy[method].resetHistory()
  7. }
  8. }
  9. // sysends sends and receives custom calls in the background. This Helps
  10. // filtering them out
  11. function getDetachCalls(method) {
  12. return sysend[method]
  13. .getCalls()
  14. .filter(call => call.args[0].startsWith('detach-'))
  15. }
  16. function getLastDetachCall(method) {
  17. return getDetachCalls(method).pop()
  18. }
  19. function getLastBroacastMessage() {
  20. return getLastDetachCall('broadcast')?.args[1]
  21. }
  22. function getAllBroacastMessages() {
  23. return getDetachCalls('broadcast')
  24. }
  25. // this fakes receiving a message by calling the handler add to `on`. A bit
  26. // funky, but works for now
  27. function receiveMessage(message) {
  28. getLastDetachCall('on').args[1](message)
  29. }
  30. function getMessageWithEvent(eventName) {
  31. return getAllBroacastMessages()
  32. .map(item => item.args[1])
  33. .find(item => item.event === eventName)
  34. }
  35. export default {
  36. spy: sysendSpy,
  37. resetHistory,
  38. getDetachCalls,
  39. getLastDetachCall,
  40. getLastBroacastMessage,
  41. getAllBroacastMessages,
  42. getMessageWithEvent,
  43. receiveMessage,
  44. }