layout-dropdown-button.test.js 4.0 KB

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