use-detach-layout.spec.tsx 6.9 KB

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