use-detach-layout.spec.tsx 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. import useDetachLayout from '../../../../frontend/js/shared/hooks/use-detach-layout'
  2. import { detachChannel, testDetachChannel } from '../../helpers/detach-channel'
  3. import { EditorProviders } from '../../helpers/editor-providers'
  4. import { Button, Checkbox, ControlLabel, FormGroup } from 'react-bootstrap'
  5. const DetachLayoutTest = () => {
  6. const { role, reattach, detach, isLinked, isLinking, isRedundant } =
  7. useDetachLayout()
  8. return (
  9. <fieldset>
  10. <legend>
  11. role: <span id="role">{role || 'none'}</span>
  12. </legend>
  13. <FormGroup>
  14. <Checkbox id="isLinked" inline checked={isLinked} readOnly />
  15. <ControlLabel>linked</ControlLabel>
  16. </FormGroup>
  17. <FormGroup>
  18. <Checkbox id="isLinking" inline checked={isLinking} readOnly />
  19. <ControlLabel>linking</ControlLabel>
  20. </FormGroup>
  21. <FormGroup>
  22. <Checkbox id="isRedundant" inline checked={isRedundant} readOnly />
  23. <ControlLabel>redundant</ControlLabel>
  24. </FormGroup>
  25. <Button id="reattach" onClick={reattach}>
  26. reattach
  27. </Button>
  28. <Button id="detach" onClick={detach}>
  29. detach
  30. </Button>
  31. </fieldset>
  32. )
  33. }
  34. describe('useDetachLayout', function () {
  35. beforeEach(function () {
  36. window.metaAttributesCache.set('ol-preventCompileOnLoad', true)
  37. cy.stub(window, 'open').as('openWindow')
  38. cy.stub(window, 'close').as('closeWindow')
  39. cy.interceptEvents()
  40. })
  41. it('detaching', function () {
  42. // 1. create hook in normal mode
  43. cy.mount(
  44. <EditorProviders>
  45. <DetachLayoutTest />
  46. </EditorProviders>
  47. )
  48. cy.get('#isLinked').should('not.be.checked')
  49. cy.get('#isLinking').should('not.be.checked')
  50. cy.get('#role').should('have.text', 'none')
  51. // 2. detach
  52. cy.get('#detach').click()
  53. cy.get('@openWindow').should(
  54. 'have.been.calledOnceWith',
  55. Cypress.sinon.match(/\/detached$/),
  56. '_blank'
  57. )
  58. cy.get('#isLinked').should('not.be.checked')
  59. cy.get('#isLinking').should('be.checked')
  60. cy.get('#role').should('have.text', 'detacher')
  61. })
  62. // eslint-disable-next-line mocha/no-skipped-tests
  63. it.skip('detacher role', function () {
  64. // 1. create hook in detacher mode
  65. window.metaAttributesCache.set('ol-detachRole', 'detacher')
  66. cy.mount(
  67. <EditorProviders>
  68. <DetachLayoutTest />
  69. </EditorProviders>
  70. )
  71. cy.get('#isLinked').should('not.be.checked')
  72. cy.get('#isLinking').should('not.be.checked')
  73. cy.get('#role').should('have.text', 'detacher')
  74. cy.spy(detachChannel, 'postMessage').as('postDetachMessage')
  75. cy.wrap(null).then(() => {
  76. testDetachChannel.postMessage({
  77. role: 'detached',
  78. event: 'connected',
  79. })
  80. })
  81. // 2. simulate connected detached tab
  82. cy.get('@postDetachMessage').should('have.been.calledWith', {
  83. role: 'detacher',
  84. event: 'up',
  85. })
  86. cy.get('#isLinked').should('be.checked')
  87. cy.get('#isLinking').should('not.be.checked')
  88. cy.get('#role').should('have.text', 'detacher')
  89. // 3. simulate closed detached tab
  90. cy.wrap(null).then(() => {
  91. testDetachChannel.postMessage({
  92. role: 'detached',
  93. event: 'closed',
  94. })
  95. })
  96. cy.get('#isLinked').should('not.be.checked')
  97. cy.get('#isLinking').should('not.be.checked')
  98. cy.get('#role').should('have.text', 'detacher')
  99. // 4. simulate up detached tab
  100. cy.wrap(null).then(() => {
  101. testDetachChannel.postMessage({
  102. role: 'detached',
  103. event: 'up',
  104. })
  105. })
  106. cy.get('#isLinked').should('be.checked')
  107. cy.get('#isLinking').should('not.be.checked')
  108. cy.get('#role').should('have.text', 'detacher')
  109. // 5. reattach
  110. cy.get('@postDetachMessage').invoke('resetHistory')
  111. cy.get('#reattach').click()
  112. cy.get('#isLinked').should('not.be.checked')
  113. cy.get('#isLinking').should('not.be.checked')
  114. cy.get('#role').should('have.text', 'none')
  115. cy.get('@postDetachMessage').should('have.been.calledWith', {
  116. role: 'detacher',
  117. event: 'reattach',
  118. })
  119. })
  120. // eslint-disable-next-line mocha/no-skipped-tests
  121. it.skip('reset detacher role when other detacher tab connects', function () {
  122. // 1. create hook in detacher mode
  123. window.metaAttributesCache.set('ol-detachRole', 'detacher')
  124. cy.mount(
  125. <EditorProviders>
  126. <DetachLayoutTest />
  127. </EditorProviders>
  128. )
  129. cy.get('#isLinked').should('not.be.checked')
  130. cy.get('#isLinking').should('not.be.checked')
  131. cy.get('#role').should('have.text', 'detacher')
  132. // 2. simulate other detacher tab
  133. cy.wrap(null).then(() => {
  134. testDetachChannel.postMessage({
  135. role: 'detacher',
  136. event: 'up',
  137. })
  138. })
  139. cy.get('#isRedundant').should('be.checked')
  140. cy.get('#role').should('have.text', 'none')
  141. })
  142. it('detached role', function () {
  143. // 1. create hook in detached mode
  144. window.metaAttributesCache.set('ol-detachRole', 'detached')
  145. cy.mount(
  146. <EditorProviders>
  147. <DetachLayoutTest />
  148. </EditorProviders>
  149. )
  150. cy.get('#isLinked').should('not.be.checked')
  151. cy.get('#isLinking').should('not.be.checked')
  152. cy.get('#role').should('have.text', 'detached')
  153. // 2. simulate up detacher tab
  154. cy.wrap(null).then(() => {
  155. testDetachChannel.postMessage({
  156. role: 'detacher',
  157. event: 'up',
  158. })
  159. })
  160. cy.get('#isLinked').should('be.checked')
  161. cy.get('#isLinking').should('not.be.checked')
  162. cy.get('#role').should('have.text', 'detached')
  163. // 3. simulate closed detacher tab
  164. cy.wrap(null).then(() => {
  165. testDetachChannel.postMessage({
  166. role: 'detacher',
  167. event: 'closed',
  168. })
  169. })
  170. cy.get('#isLinked').should('not.be.checked')
  171. cy.get('#isLinking').should('not.be.checked')
  172. cy.get('#role').should('have.text', 'detached')
  173. // 4. simulate up detacher tab
  174. cy.wrap(null).then(() => {
  175. testDetachChannel.postMessage({
  176. role: 'detacher',
  177. event: 'up',
  178. })
  179. })
  180. cy.get('#isLinked').should('be.checked')
  181. cy.get('#isLinking').should('not.be.checked')
  182. cy.get('#role').should('have.text', 'detached')
  183. // 5. simulate closed detached tab
  184. cy.spy(detachChannel, 'postMessage').as('postDetachMessage')
  185. cy.wrap(null).then(() => {
  186. testDetachChannel.postMessage({
  187. role: 'detached',
  188. event: 'closed',
  189. })
  190. })
  191. cy.get('#isLinked').should('be.checked')
  192. cy.get('#isLinking').should('not.be.checked')
  193. cy.get('#role').should('have.text', 'detached')
  194. cy.get('@postDetachMessage').should('have.been.calledWith', {
  195. role: 'detached',
  196. event: 'up',
  197. })
  198. // 6. simulate reattach event
  199. cy.get('@postDetachMessage').invoke('resetHistory')
  200. cy.wrap(null).then(() => {
  201. testDetachChannel.postMessage({
  202. role: 'detacher',
  203. event: 'reattach',
  204. })
  205. })
  206. cy.get('#isLinked').should('not.be.checked')
  207. cy.get('#isLinking').should('not.be.checked')
  208. cy.get('#role').should('have.text', 'detached')
  209. cy.get('@closeWindow').should('have.been.called')
  210. })
  211. })