layout-dropdown-button.test.jsx 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. import sinon from 'sinon'
  2. import fetchMock from 'fetch-mock'
  3. import { expect } from 'chai'
  4. import { screen } from '@testing-library/react'
  5. import LayoutDropdownButton from '../../../../../frontend/js/features/editor-navigation-toolbar/components/layout-dropdown-button'
  6. import { renderWithEditorContext } from '../../../helpers/render-with-context'
  7. import * as eventTracking from '@/infrastructure/event-tracking'
  8. describe('<LayoutDropdownButton />', function () {
  9. let openStub
  10. let sendMBSpy
  11. const defaultUi = {
  12. pdfLayout: 'flat',
  13. view: 'pdf',
  14. }
  15. beforeEach(function () {
  16. openStub = sinon.stub(window, 'open')
  17. sendMBSpy = sinon.spy(eventTracking, 'sendMB')
  18. })
  19. afterEach(function () {
  20. openStub.restore()
  21. sendMBSpy.restore()
  22. fetchMock.removeRoutes().clearHistory()
  23. })
  24. it('should mark current layout option as selected', async function () {
  25. // Selected is aria-label, visually we show a checkmark
  26. renderWithEditorContext(<LayoutDropdownButton />, { ui: defaultUi })
  27. screen.getByRole('button', { name: 'Layout' }).click()
  28. expect(
  29. screen
  30. .getByRole('menuitem', {
  31. name: 'Editor & PDF',
  32. })
  33. .getAttribute('aria-selected')
  34. ).to.equal('false')
  35. expect(
  36. screen
  37. .getByRole('menuitem', {
  38. name: 'PDF only (hide editor)',
  39. })
  40. .getAttribute('aria-selected')
  41. ).to.equal('true')
  42. expect(
  43. screen
  44. .getByRole('menuitem', {
  45. name: 'Editor only (hide PDF)',
  46. })
  47. .getAttribute('aria-selected')
  48. ).to.equal('false')
  49. expect(
  50. screen
  51. .getByRole('menuitem', {
  52. name: 'PDF in separate tab',
  53. })
  54. .getAttribute('aria-selected')
  55. ).to.equal('false')
  56. })
  57. it('should not select any option in history view', function () {
  58. // Selected is aria-label, visually we show a checkmark
  59. renderWithEditorContext(<LayoutDropdownButton />, {
  60. ui: { ...defaultUi, view: 'history' },
  61. })
  62. screen.getByRole('button', { name: 'Layout' }).click()
  63. expect(
  64. screen
  65. .getByRole('menuitem', {
  66. name: 'Editor & PDF',
  67. })
  68. .getAttribute('aria-selected')
  69. ).to.equal('false')
  70. expect(
  71. screen
  72. .getByRole('menuitem', {
  73. name: 'PDF only (hide editor)',
  74. })
  75. .getAttribute('aria-selected')
  76. ).to.equal('false')
  77. expect(
  78. screen
  79. .getByRole('menuitem', {
  80. name: 'Editor only (hide PDF)',
  81. })
  82. .getAttribute('aria-selected')
  83. ).to.equal('false')
  84. expect(
  85. screen
  86. .getByRole('menuitem', {
  87. name: 'PDF in separate tab',
  88. })
  89. .getAttribute('aria-selected')
  90. ).to.equal('false')
  91. })
  92. it('should treat file and editor views the same way', function () {
  93. // Selected is aria-label, visually we show a checkmark
  94. renderWithEditorContext(<LayoutDropdownButton />, {
  95. ui: {
  96. pdfLayout: 'flat',
  97. view: 'file',
  98. },
  99. })
  100. screen.getByRole('button', { name: 'Layout' }).click()
  101. expect(
  102. screen
  103. .getByRole('menuitem', {
  104. name: 'Editor & PDF',
  105. })
  106. .getAttribute('aria-selected')
  107. ).to.equal('false')
  108. expect(
  109. screen
  110. .getByRole('menuitem', {
  111. name: 'PDF only (hide editor)',
  112. })
  113. .getAttribute('aria-selected')
  114. ).to.equal('false')
  115. expect(
  116. screen
  117. .getByRole('menuitem', {
  118. name: 'Editor only (hide PDF)',
  119. })
  120. .getAttribute('aria-selected')
  121. ).to.equal('true')
  122. expect(
  123. screen
  124. .getByRole('menuitem', {
  125. name: 'PDF in separate tab',
  126. })
  127. .getAttribute('aria-selected')
  128. ).to.equal('false')
  129. })
  130. describe('on detach', function () {
  131. let originalBroadcastChannel
  132. beforeEach(function () {
  133. window.BroadcastChannel = originalBroadcastChannel || true // ensure that window.BroadcastChannel is truthy
  134. renderWithEditorContext(<LayoutDropdownButton />, {
  135. ui: { ...defaultUi, view: 'editor' },
  136. })
  137. screen.getByRole('button', { name: 'Layout' }).click()
  138. screen
  139. .getByRole('menuitem', {
  140. name: 'PDF in separate tab',
  141. })
  142. .click()
  143. })
  144. afterEach(function () {
  145. window.BroadcastChannel = originalBroadcastChannel
  146. })
  147. it('should show processing', function () {
  148. screen.getByText('Layout processing')
  149. })
  150. it('should record event', function () {
  151. sinon.assert.calledWith(sendMBSpy, 'project-layout-detach')
  152. })
  153. })
  154. describe('on layout change / reattach', function () {
  155. beforeEach(function () {
  156. window.metaAttributesCache.set('ol-detachRole', 'detacher')
  157. renderWithEditorContext(<LayoutDropdownButton />, {
  158. ui: { ...defaultUi, view: 'editor' },
  159. })
  160. screen.getByRole('button', { name: 'Layout' }).click()
  161. screen
  162. .getByRole('menuitem', {
  163. name: 'Editor only (hide PDF)',
  164. })
  165. .click()
  166. })
  167. it('should not show processing', function () {
  168. const processingText = screen.queryByText('Layout processing')
  169. expect(processingText).to.not.exist
  170. })
  171. it('should record events', function () {
  172. sinon.assert.calledWith(sendMBSpy, 'project-layout-reattach')
  173. sinon.assert.calledWith(sendMBSpy, 'project-layout-change', {
  174. layout: 'flat',
  175. view: 'editor',
  176. page: '/detacher',
  177. })
  178. })
  179. it('should select new menu item', function () {
  180. screen.getByRole('menuitem', {
  181. name: 'Editor only (hide PDF)',
  182. })
  183. })
  184. })
  185. })