sysend.js 961 B

1234567891011121314151617181920212223242526272829303132333435363738394041
  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. // this fakes receiving a message by calling the handler add to `on`. A bit
  23. // funky, but works for now
  24. function receiveMessage(message) {
  25. getLastDetachCall('on').args[1](message)
  26. }
  27. export default {
  28. spy: sysendSpy,
  29. resetHistory,
  30. getDetachCalls,
  31. getLastDetachCall,
  32. getLastBroacastMessage,
  33. receiveMessage,
  34. }