layout-dropdown-button.test.jsx 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. import sinon from 'sinon'
  2. import fetchMock from 'fetch-mock'
  3. import { expect } from 'chai'
  4. import { fireEvent, 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.restore()
  23. })
  24. it('should mark current layout option as selected', function () {
  25. // Selected is aria-label, visually we show a checkmark
  26. renderWithEditorContext(<LayoutDropdownButton />, { ui: defaultUi })
  27. screen.getByRole('menuitem', {
  28. name: 'Editor & PDF',
  29. })
  30. screen.getByRole('menuitem', {
  31. name: 'Selected PDF only (hide editor)',
  32. })
  33. screen.getByRole('menuitem', {
  34. name: 'Editor only (hide PDF)',
  35. })
  36. screen.getByRole('menuitem', {
  37. name: 'PDF in separate tab',
  38. })
  39. })
  40. it('should not select any option in history view', function () {
  41. // Selected is aria-label, visually we show a checkmark
  42. renderWithEditorContext(<LayoutDropdownButton />, {
  43. ui: { ...defaultUi, view: 'history' },
  44. })
  45. screen.getByRole('menuitem', {
  46. name: 'Editor & PDF',
  47. })
  48. screen.getByRole('menuitem', {
  49. name: 'PDF only (hide editor)',
  50. })
  51. screen.getByRole('menuitem', {
  52. name: 'Editor only (hide PDF)',
  53. })
  54. screen.getByRole('menuitem', {
  55. name: 'PDF in separate tab',
  56. })
  57. })
  58. it('should treat file and editor views the same way', function () {
  59. // Selected is aria-label, visually we show a checkmark
  60. renderWithEditorContext(<LayoutDropdownButton />, {
  61. ui: {
  62. pdfLayout: 'flat',
  63. view: 'file',
  64. },
  65. })
  66. screen.getByRole('menuitem', {
  67. name: 'Editor & PDF',
  68. })
  69. screen.getByRole('menuitem', {
  70. name: 'PDF only (hide editor)',
  71. })
  72. screen.getByRole('menuitem', {
  73. name: 'Selected Editor only (hide PDF)',
  74. })
  75. screen.getByRole('menuitem', {
  76. name: 'PDF in separate tab',
  77. })
  78. })
  79. describe('on detach', function () {
  80. let originalBroadcastChannel
  81. beforeEach(function () {
  82. window.BroadcastChannel = originalBroadcastChannel || true // ensure that window.BroadcastChannel is truthy
  83. renderWithEditorContext(<LayoutDropdownButton />, {
  84. ui: { ...defaultUi, view: 'editor' },
  85. })
  86. const menuItem = screen.getByRole('menuitem', {
  87. name: 'PDF in separate tab',
  88. })
  89. fireEvent.click(menuItem)
  90. })
  91. afterEach(function () {
  92. window.BroadcastChannel = originalBroadcastChannel
  93. })
  94. it('should show processing', function () {
  95. screen.getByText('Layout processing')
  96. })
  97. it('should record event', function () {
  98. sinon.assert.calledWith(sendMBSpy, 'project-layout-detach')
  99. })
  100. })
  101. describe('on layout change / reattach', function () {
  102. beforeEach(function () {
  103. window.metaAttributesCache.set('ol-detachRole', 'detacher')
  104. renderWithEditorContext(<LayoutDropdownButton />, {
  105. ui: { ...defaultUi, view: 'editor' },
  106. })
  107. const menuItem = screen.getByRole('menuitem', {
  108. name: 'Editor only (hide PDF)',
  109. })
  110. fireEvent.click(menuItem)
  111. })
  112. it('should not show processing', function () {
  113. const processingText = screen.queryByText('Layout processing')
  114. expect(processingText).to.not.exist
  115. })
  116. it('should record events', function () {
  117. sinon.assert.calledWith(sendMBSpy, 'project-layout-reattach')
  118. sinon.assert.calledWith(sendMBSpy, 'project-layout-change', {
  119. layout: 'flat',
  120. view: 'editor',
  121. page: '/detacher',
  122. })
  123. })
  124. it('should select new menu item', function () {
  125. screen.getByRole('menuitem', {
  126. name: 'Selected Editor only (hide PDF)',
  127. })
  128. })
  129. })
  130. })