use-detach-action.spec.tsx 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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. beforeEach(function () {
  24. window.metaAttributesCache = new Map()
  25. })
  26. afterEach(function () {
  27. window.metaAttributesCache = new Map()
  28. })
  29. it('broadcast message as sender', function () {
  30. window.metaAttributesCache.set('ol-detachRole', 'detacher')
  31. cy.mount(
  32. <EditorProviders>
  33. <DetachActionTest
  34. actionName="some-action"
  35. actionFunction={cy.stub().as('actionFunction')}
  36. handleClick={trigger => trigger('foo')}
  37. />
  38. </EditorProviders>
  39. )
  40. cy.spy(detachChannel, 'postMessage').as('postDetachMessage')
  41. cy.get('#trigger').click()
  42. cy.get('@postDetachMessage').should('be.calledWith', {
  43. role: 'detacher',
  44. event: 'action-some-action',
  45. data: { args: ['foo'] },
  46. })
  47. cy.get('@actionFunction').should('not.be.called')
  48. })
  49. it('call function as non-sender', function () {
  50. cy.mount(
  51. <EditorProviders>
  52. <DetachActionTest
  53. actionName="some-action"
  54. actionFunction={cy.stub().as('actionFunction')}
  55. handleClick={trigger => trigger('foo')}
  56. />
  57. </EditorProviders>
  58. )
  59. cy.spy(detachChannel, 'postMessage').as('postDetachMessage')
  60. cy.get('#trigger').click()
  61. cy.get('@postDetachMessage').should('not.be.called')
  62. cy.get('@actionFunction').should('be.calledWith', 'foo')
  63. })
  64. it('receive message and call function as target', function () {
  65. window.metaAttributesCache.set('ol-detachRole', 'detached')
  66. cy.mount(
  67. <EditorProviders>
  68. <DetachActionTest
  69. actionName="some-action"
  70. actionFunction={cy.stub().as('actionFunction')}
  71. handleClick={trigger => trigger('foo')}
  72. />
  73. </EditorProviders>
  74. )
  75. cy.wrap(null).then(() => {
  76. testDetachChannel.postMessage({
  77. role: 'detached',
  78. event: 'action-some-action',
  79. data: { args: ['foo'] },
  80. })
  81. })
  82. cy.get('@actionFunction').should('be.calledWith', 'foo')
  83. })
  84. it('receive message and does not call function as non-target', function () {
  85. window.metaAttributesCache.set('ol-detachRole', 'detacher')
  86. cy.mount(
  87. <EditorProviders>
  88. <DetachActionTest
  89. actionName="some-action"
  90. actionFunction={cy.stub().as('actionFunction')}
  91. handleClick={trigger => trigger('foo')}
  92. />
  93. </EditorProviders>
  94. )
  95. cy.wrap(null).then(() => {
  96. testDetachChannel.postMessage({
  97. role: 'detached',
  98. event: 'action-some-action',
  99. data: { args: [] },
  100. })
  101. })
  102. cy.get('@actionFunction').should('not.be.called')
  103. })
  104. })