use-detach-state.spec.tsx 2.7 KB

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