sysend.js 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  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. export default {
  31. spy: sysendSpy,
  32. resetHistory,
  33. getDetachCalls,
  34. getLastDetachCall,
  35. getLastBroacastMessage,
  36. getAllBroacastMessages,
  37. receiveMessage,
  38. }