use-detach-layout.spec.tsx 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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. // eslint-disable-next-line mocha/no-skipped-tests
  35. describe.skip('useDetachLayout', function () {
  36. beforeEach(function () {
  37. window.metaAttributesCache = new Map()
  38. cy.stub(window, 'open').as('openWindow')
  39. cy.stub(window, 'close').as('closeWindow')
  40. })
  41. afterEach(function () {
  42. window.metaAttributesCache = new Map()
  43. })
  44. it('detaching', function () {
  45. // 1. create hook in normal mode
  46. cy.mount(
  47. <EditorProviders>
  48. <DetachLayoutTest />
  49. </EditorProviders>
  50. )
  51. cy.get('#isLinked').should('not.be.checked')
  52. cy.get('#isLinking').should('not.be.checked')
  53. cy.get('#role').should('have.text', 'none')
  54. // 2. detach
  55. cy.get('#detach').click()
  56. cy.get('@openWindow').should(
  57. 'be.calledOnceWith',
  58. Cypress.sinon.match(/\/detached$/),
  59. '_blank'
  60. )
  61. cy.get('#isLinked').should('not.be.checked')
  62. cy.get('#isLinking').should('be.checked')
  63. cy.get('#role').should('have.text', 'detacher')
  64. })
  65. it('detacher role', function () {
  66. // 1. create hook in detacher mode
  67. window.metaAttributesCache.set('ol-detachRole', 'detacher')
  68. cy.mount(
  69. <EditorProviders>
  70. <DetachLayoutTest />
  71. </EditorProviders>
  72. )
  73. cy.get('#isLinked').should('not.be.checked')
  74. cy.get('#isLinking').should('not.be.checked')
  75. cy.get('#role').should('have.text', 'detacher')
  76. cy.spy(detachChannel, 'postMessage').as('postDetachMessage')
  77. cy.wrap(null).then(() => {
  78. testDetachChannel.postMessage({
  79. role: 'detached',
  80. event: 'connected',
  81. })
  82. })
  83. // 2. simulate connected detached tab
  84. cy.get('@postDetachMessage').should('be.calledWith', {
  85. role: 'detacher',
  86. event: 'up',
  87. })
  88. cy.get('#isLinked').should('be.checked')
  89. cy.get('#isLinking').should('not.be.checked')
  90. cy.get('#role').should('have.text', 'detacher')
  91. // 3. simulate closed detached tab
  92. cy.wrap(null).then(() => {
  93. testDetachChannel.postMessage({
  94. role: 'detached',
  95. event: 'closed',
  96. })
  97. })
  98. cy.get('#isLinked').should('not.be.checked')
  99. cy.get('#isLinking').should('not.be.checked')
  100. cy.get('#role').should('have.text', 'detacher')
  101. // 4. simulate up detached tab
  102. cy.wrap(null).then(() => {
  103. testDetachChannel.postMessage({
  104. role: 'detached',
  105. event: 'up',
  106. })
  107. })
  108. cy.get('#isLinked').should('be.checked')
  109. cy.get('#isLinking').should('not.be.checked')
  110. cy.get('#role').should('have.text', 'detacher')
  111. // 5. reattach
  112. cy.get('@postDetachMessage').invoke('resetHistory')
  113. cy.get('#reattach').click()
  114. cy.get('#isLinked').should('not.be.checked')
  115. cy.get('#isLinking').should('not.be.checked')
  116. cy.get('#role').should('have.text', 'none')
  117. cy.get('@postDetachMessage').should('be.calledWith', {
  118. role: 'detacher',
  119. event: 'reattach',
  120. })
  121. })
  122. it('reset detacher role when other detacher tab connects', function () {
  123. // 1. create hook in detacher mode
  124. window.metaAttributesCache.set('ol-detachRole', 'detacher')
  125. cy.mount(
  126. <EditorProviders>
  127. <DetachLayoutTest />
  128. </EditorProviders>
  129. )
  130. cy.get('#isLinked').should('not.be.checked')
  131. cy.get('#isLinking').should('not.be.checked')
  132. cy.get('#role').should('have.text', 'detacher')
  133. // 2. simulate other detacher tab
  134. cy.wrap(null).then(() => {
  135. testDetachChannel.postMessage({
  136. role: 'detacher',
  137. event: 'up',
  138. })
  139. })
  140. cy.get('#isRedundant').should('be.checked')
  141. cy.get('#role').should('have.text', 'none')
  142. })
  143. it('detached role', function () {
  144. // 1. create hook in detached mode
  145. window.metaAttributesCache.set('ol-detachRole', 'detached')
  146. cy.mount(
  147. <EditorProviders>
  148. <DetachLayoutTest />
  149. </EditorProviders>
  150. )
  151. cy.get('#isLinked').should('not.be.checked')
  152. cy.get('#isLinking').should('not.be.checked')
  153. cy.get('#role').should('have.text', 'detached')
  154. // 2. simulate up detacher tab
  155. cy.wrap(null).then(() => {
  156. testDetachChannel.postMessage({
  157. role: 'detacher',
  158. event: 'up',
  159. })
  160. })
  161. cy.get('#isLinked').should('be.checked')
  162. cy.get('#isLinking').should('not.be.checked')
  163. cy.get('#role').should('have.text', 'detached')
  164. // 3. simulate closed detacher tab
  165. cy.wrap(null).then(() => {
  166. testDetachChannel.postMessage({
  167. role: 'detacher',
  168. event: 'closed',
  169. })
  170. })
  171. cy.get('#isLinked').should('not.be.checked')
  172. cy.get('#isLinking').should('not.be.checked')
  173. cy.get('#role').should('have.text', 'detached')
  174. // 4. simulate up detacher tab
  175. cy.wrap(null).then(() => {
  176. testDetachChannel.postMessage({
  177. role: 'detacher',
  178. event: 'up',
  179. })
  180. })
  181. cy.get('#isLinked').should('be.checked')
  182. cy.get('#isLinking').should('not.be.checked')
  183. cy.get('#role').should('have.text', 'detached')
  184. // 5. simulate closed detached tab
  185. cy.spy(detachChannel, 'postMessage').as('postDetachMessage')
  186. cy.wrap(null).then(() => {
  187. testDetachChannel.postMessage({
  188. role: 'detached',
  189. event: 'closed',
  190. })
  191. })
  192. cy.get('#isLinked').should('be.checked')
  193. cy.get('#isLinking').should('not.be.checked')
  194. cy.get('#role').should('have.text', 'detached')
  195. cy.get('@postDetachMessage').should('be.calledWith', {
  196. role: 'detached',
  197. event: 'up',
  198. })
  199. // 6. simulate reattach event
  200. cy.get('@postDetachMessage').invoke('resetHistory')
  201. cy.wrap(null).then(() => {
  202. testDetachChannel.postMessage({
  203. role: 'detacher',
  204. event: 'reattach',
  205. })
  206. })
  207. cy.get('#isLinked').should('not.be.checked')
  208. cy.get('#isLinking').should('not.be.checked')
  209. cy.get('#role').should('have.text', 'detached')
  210. cy.get('@closeWindow').should('be.called')
  211. })
  212. })