use-detach-layout.spec.tsx 7.1 KB

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