preview-toolbar.test.js 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. import React from 'react'
  2. import sinon from 'sinon'
  3. import { expect } from 'chai'
  4. import { screen, render, fireEvent } from '@testing-library/react'
  5. import PreviewToolbar from '../../../../../frontend/js/features/preview/components/preview-toolbar'
  6. describe('<PreviewToolbar />', function () {
  7. const onRecompile = sinon.stub()
  8. const onRecompileFromScratch = sinon.stub()
  9. const onRunSyntaxCheckNow = sinon.stub()
  10. const onSetAutoCompile = sinon.stub()
  11. const onSetDraftMode = sinon.stub()
  12. const onSetSyntaxCheck = sinon.stub()
  13. const onToggleLogs = sinon.stub()
  14. const onSetSplitLayout = sinon.stub()
  15. const onSetFullLayout = sinon.stub()
  16. const onStopCompilation = sinon.stub()
  17. function renderPreviewToolbar(
  18. compilerState = {},
  19. logState = {},
  20. showLogs = false,
  21. splitLayout = true
  22. ) {
  23. render(
  24. <PreviewToolbar
  25. compilerState={{
  26. autoCompileHasChanges: true,
  27. isAutoCompileOn: true,
  28. isClearingCache: false,
  29. isCompiling: false,
  30. isDraftModeOn: false,
  31. isSyntaxCheckOn: false,
  32. logEntries: {},
  33. ...compilerState,
  34. }}
  35. logsState={{ nErrors: 0, nWarnings: 0, ...logState }}
  36. onRecompile={onRecompile}
  37. onRecompileFromScratch={onRecompileFromScratch}
  38. onRunSyntaxCheckNow={onRunSyntaxCheckNow}
  39. onSetAutoCompile={onSetAutoCompile}
  40. onSetDraftMode={onSetDraftMode}
  41. onSetSyntaxCheck={onSetSyntaxCheck}
  42. onToggleLogs={onToggleLogs}
  43. outputFiles={[]}
  44. pdfDownloadUrl="/download-pdf-url"
  45. showLogs={showLogs}
  46. splitLayout={splitLayout}
  47. onSetSplitLayout={onSetSplitLayout}
  48. onSetFullLayout={onSetFullLayout}
  49. onStopCompilation={onStopCompilation}
  50. />
  51. )
  52. }
  53. it('renders the toolbar', function () {
  54. renderPreviewToolbar()
  55. screen.getByText('Recompile')
  56. screen.getByText('Download PDF')
  57. screen.getByText('View logs')
  58. })
  59. it('all toolbar items have "toolbar-item" class and text has "toolbar-text"', function () {
  60. renderPreviewToolbar()
  61. const toolbar = screen.getByTestId('toolbar-preview')
  62. for (const toolbarSection of toolbar.children) {
  63. for (const toolbarItem of toolbarSection.children) {
  64. expect(toolbarItem.className).to.contain('toolbar-item')
  65. for (const parts of toolbarItem.children) {
  66. for (const part of parts.children) {
  67. if (part.nodeName !== 'LI' && part.textContent) {
  68. expect(part.className).to.contain('toolbar-text')
  69. }
  70. }
  71. }
  72. }
  73. }
  74. })
  75. it('renders a full-screen button with a tooltip when when in split-screen mode', function () {
  76. renderPreviewToolbar()
  77. const btn = screen.getByLabelText('Full screen')
  78. fireEvent.click(btn)
  79. expect(onSetFullLayout).to.have.been.calledOnce
  80. fireEvent.mouseOver(btn)
  81. screen.getByRole('tooltip', { name: 'Full screen' })
  82. })
  83. it('renders a split-screen button with a tooltip when when in full-screen mode', function () {
  84. renderPreviewToolbar({}, {}, false, false)
  85. const btn = screen.getByLabelText('Split screen')
  86. fireEvent.click(btn)
  87. expect(onSetSplitLayout).to.have.been.calledOnce
  88. fireEvent.mouseOver(btn)
  89. screen.getByRole('tooltip', { name: 'Split screen' })
  90. })
  91. })