use-detach-action.spec.tsx 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. import { FC } from 'react'
  2. import useDetachAction from '../../../../frontend/js/shared/hooks/use-detach-action'
  3. import { detachChannel, testDetachChannel } from '../../helpers/detach-channel'
  4. import { EditorProviders } from '../../helpers/editor-providers'
  5. const DetachActionTest: FC<{
  6. actionName: string
  7. actionFunction: () => void
  8. handleClick: (trigger: (value: any) => void) => void
  9. }> = ({ actionName, actionFunction, handleClick }) => {
  10. const trigger = useDetachAction(
  11. actionName,
  12. actionFunction,
  13. 'detacher',
  14. 'detached'
  15. )
  16. return (
  17. <button id="trigger" onClick={() => handleClick(trigger)}>
  18. trigger
  19. </button>
  20. )
  21. }
  22. describe('useDetachAction', function () {
  23. it('broadcast message as sender', function () {
  24. window.metaAttributesCache.set('ol-detachRole', 'detacher')
  25. cy.mount(
  26. <EditorProviders>
  27. <DetachActionTest
  28. actionName="some-action"
  29. actionFunction={cy.stub().as('actionFunction')}
  30. handleClick={trigger => trigger('foo')}
  31. />
  32. </EditorProviders>
  33. )
  34. cy.spy(detachChannel, 'postMessage').as('postDetachMessage')
  35. cy.get('#trigger').click()
  36. cy.get('@postDetachMessage').should('have.been.calledWith', {
  37. role: 'detacher',
  38. event: 'action-some-action',
  39. data: { args: ['foo'] },
  40. })
  41. cy.get('@actionFunction').should('not.have.been.called')
  42. })
  43. it('call function as non-sender', function () {
  44. cy.mount(
  45. <EditorProviders>
  46. <DetachActionTest
  47. actionName="some-action"
  48. actionFunction={cy.stub().as('actionFunction')}
  49. handleClick={trigger => trigger('foo')}
  50. />
  51. </EditorProviders>
  52. )
  53. cy.spy(detachChannel, 'postMessage').as('postDetachMessage')
  54. cy.get('#trigger').click()
  55. cy.get('@postDetachMessage').should('not.have.been.called')
  56. cy.get('@actionFunction').should('have.been.calledWith', 'foo')
  57. })
  58. it('receive message and call function as target', function () {
  59. window.metaAttributesCache.set('ol-detachRole', 'detached')
  60. cy.mount(
  61. <EditorProviders>
  62. <DetachActionTest
  63. actionName="some-action"
  64. actionFunction={cy.stub().as('actionFunction')}
  65. handleClick={trigger => trigger('foo')}
  66. />
  67. </EditorProviders>
  68. )
  69. cy.wrap(null).then(() => {
  70. testDetachChannel.postMessage({
  71. role: 'detached',
  72. event: 'action-some-action',
  73. data: { args: ['foo'] },
  74. })
  75. })
  76. cy.get('@actionFunction').should('have.been.calledWith', 'foo')
  77. })
  78. it('receive message and does not call function as non-target', function () {
  79. window.metaAttributesCache.set('ol-detachRole', 'detacher')
  80. cy.mount(
  81. <EditorProviders>
  82. <DetachActionTest
  83. actionName="some-action"
  84. actionFunction={cy.stub().as('actionFunction')}
  85. handleClick={trigger => trigger('foo')}
  86. />
  87. </EditorProviders>
  88. )
  89. cy.wrap(null).then(() => {
  90. testDetachChannel.postMessage({
  91. role: 'detached',
  92. event: 'action-some-action',
  93. data: { args: [] },
  94. })
  95. })
  96. cy.get('@actionFunction').should('not.have.been.called')
  97. })
  98. })