layout-dropdown-button.test.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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 '../../../../../frontend/js/infrastructure/event-tracking'
  8. const eventTrackingSpy = sinon.spy(eventTracking)
  9. describe('<LayoutDropdownButton />', function () {
  10. let openStub
  11. const defaultUi = {
  12. pdfLayout: 'flat',
  13. view: 'pdf',
  14. }
  15. beforeEach(function () {
  16. openStub = sinon.stub(window, 'open')
  17. window.metaAttributesCache = new Map()
  18. })
  19. afterEach(function () {
  20. openStub.restore()
  21. window.metaAttributesCache = new Map()
  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. beforeEach(function () {
  81. renderWithEditorContext(<LayoutDropdownButton />, {
  82. ui: { ...defaultUi, view: 'editor' },
  83. })
  84. const menuItem = screen.getByRole('menuitem', {
  85. name: 'PDF in separate tab',
  86. })
  87. fireEvent.click(menuItem)
  88. })
  89. it('should show processing', function () {
  90. screen.getByText('Layout processing')
  91. })
  92. it('should record event', function () {
  93. sinon.assert.calledWith(eventTrackingSpy.sendMB, 'project-layout-detach')
  94. })
  95. })
  96. describe('on layout change / reattach', function () {
  97. beforeEach(function () {
  98. window.metaAttributesCache.set('ol-detachRole', 'detacher')
  99. renderWithEditorContext(<LayoutDropdownButton />, {
  100. ui: { ...defaultUi, view: 'editor' },
  101. })
  102. const menuItem = screen.getByRole('menuitem', {
  103. name: 'Editor only (hide PDF)',
  104. })
  105. fireEvent.click(menuItem)
  106. })
  107. it('should not show processing', function () {
  108. const processingText = screen.queryByText('Layout processing')
  109. expect(processingText).to.not.exist
  110. })
  111. it('should record events', function () {
  112. sinon.assert.calledWith(
  113. eventTrackingSpy.sendMB,
  114. 'project-layout-reattach'
  115. )
  116. sinon.assert.calledWith(
  117. eventTrackingSpy.sendMB,
  118. 'project-layout-change',
  119. { layout: 'flat', view: 'editor' }
  120. )
  121. })
  122. it('should select new menu item', function () {
  123. screen.getByRole('menuitem', {
  124. name: 'Selected Editor only (hide PDF)',
  125. })
  126. })
  127. })
  128. })