use-detach-action.test.js 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. import sinon from 'sinon'
  2. import { expect } from 'chai'
  3. import { renderHookWithEditorContext } from '../../helpers/render-with-context'
  4. import sysendTestHelper from '../../helpers/sysend'
  5. import useDetachAction from '../../../../frontend/js/shared/hooks/use-detach-action'
  6. const actionName = 'some-action'
  7. const actionFunction = sinon.stub()
  8. describe('useDetachAction', function () {
  9. beforeEach(function () {
  10. window.metaAttributesCache = new Map()
  11. })
  12. afterEach(function () {
  13. window.metaAttributesCache = new Map()
  14. actionFunction.reset()
  15. })
  16. it('broadcast message as sender', async function () {
  17. window.metaAttributesCache.set('ol-detachRole', 'detacher')
  18. const { result } = renderHookWithEditorContext(() =>
  19. useDetachAction(actionName, actionFunction, 'detacher', 'detached')
  20. )
  21. const triggerFn = result.current
  22. sysendTestHelper.resetHistory()
  23. triggerFn('param')
  24. expect(sysendTestHelper.getLastBroacastMessage()).to.deep.equal({
  25. role: 'detacher',
  26. event: `action-${actionName}`,
  27. data: { args: ['param'] },
  28. })
  29. sinon.assert.notCalled(actionFunction)
  30. })
  31. it('call function as non-sender', async function () {
  32. const { result } = renderHookWithEditorContext(() =>
  33. useDetachAction(actionName, actionFunction, 'detacher', 'detached')
  34. )
  35. const triggerFn = result.current
  36. sysendTestHelper.resetHistory()
  37. triggerFn('param')
  38. expect(sysendTestHelper.getDetachCalls('broadcast').length).to.equal(0)
  39. sinon.assert.calledWith(actionFunction, 'param')
  40. })
  41. it('receive message and call function as target', async function () {
  42. window.metaAttributesCache.set('ol-detachRole', 'detached')
  43. renderHookWithEditorContext(() =>
  44. useDetachAction(actionName, actionFunction, 'detacher', 'detached')
  45. )
  46. sysendTestHelper.receiveMessage({
  47. role: 'detached',
  48. event: `action-${actionName}`,
  49. data: { args: ['param'] },
  50. })
  51. sinon.assert.calledWith(actionFunction, 'param')
  52. })
  53. it('receive message and does not call function as non-target', async function () {
  54. window.metaAttributesCache.set('ol-detachRole', 'detacher')
  55. renderHookWithEditorContext(() =>
  56. useDetachAction(actionName, actionFunction, 'detacher', 'detached')
  57. )
  58. sysendTestHelper.receiveMessage({
  59. role: 'detached',
  60. event: `action-${actionName}`,
  61. data: { args: [] },
  62. })
  63. sinon.assert.notCalled(actionFunction)
  64. })
  65. })