layout-dropdown-button.test.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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. fetchMock.post('express:/project/:projectId/compile/stop', () => 204)
  19. })
  20. afterEach(function () {
  21. openStub.restore()
  22. window.metaAttributesCache = new Map()
  23. fetchMock.restore()
  24. })
  25. it('should mark current layout option as selected', function () {
  26. // Selected is aria-label, visually we show a checkmark
  27. renderWithEditorContext(<LayoutDropdownButton />, { ui: defaultUi })
  28. screen.getByRole('menuitem', {
  29. name: 'Editor & PDF',
  30. })
  31. screen.getByRole('menuitem', {
  32. name: 'Selected PDF only (hide editor)',
  33. })
  34. screen.getByRole('menuitem', {
  35. name: 'Editor only (hide PDF)',
  36. })
  37. screen.getByRole('menuitem', {
  38. name: 'PDF in separate tab',
  39. })
  40. })
  41. describe('on detach', function () {
  42. beforeEach(function () {
  43. renderWithEditorContext(<LayoutDropdownButton />, {
  44. ui: { ...defaultUi, view: 'editor' },
  45. })
  46. const menuItem = screen.getByRole('menuitem', {
  47. name: 'PDF in separate tab',
  48. })
  49. fireEvent.click(menuItem)
  50. })
  51. it('should show processing', function () {
  52. screen.getByText('Layout processing')
  53. })
  54. it('should stop compile when detaching', function () {
  55. expect(fetchMock.called('express:/project/:projectId/compile/stop')).to.be
  56. .true
  57. })
  58. it('should record event', function () {
  59. sinon.assert.calledWith(eventTrackingSpy.sendMB, 'project-layout-detach')
  60. })
  61. })
  62. describe('on layout change / reattach', function () {
  63. beforeEach(function () {
  64. window.metaAttributesCache.set('ol-detachRole', 'detacher')
  65. renderWithEditorContext(<LayoutDropdownButton />, {
  66. ui: { ...defaultUi, view: 'editor' },
  67. })
  68. const menuItem = screen.getByRole('menuitem', {
  69. name: 'Editor only (hide PDF)',
  70. })
  71. fireEvent.click(menuItem)
  72. })
  73. it('should not show processing', function () {
  74. const processingText = screen.queryByText('Layout processing')
  75. expect(processingText).to.not.exist
  76. })
  77. it('should record events', function () {
  78. sinon.assert.calledWith(
  79. eventTrackingSpy.sendMB,
  80. 'project-layout-reattach'
  81. )
  82. sinon.assert.calledWith(
  83. eventTrackingSpy.sendMB,
  84. 'project-layout-change',
  85. { layout: 'flat', view: 'editor' }
  86. )
  87. })
  88. it('should select new menu item', function () {
  89. screen.getByRole('menuitem', {
  90. name: 'Selected Editor only (hide PDF)',
  91. })
  92. })
  93. })
  94. })