preview-toolbar.test.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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. isAutoCompileOn: true,
  27. isClearingCache: false,
  28. isCompiling: false,
  29. isDraftModeOn: false,
  30. isSyntaxCheckOn: false,
  31. logEntries: {},
  32. ...compilerState
  33. }}
  34. logsState={{ nErrors: 0, nWarnings: 0, nLogEntries: 0, ...logState }}
  35. onRecompile={onRecompile}
  36. onRecompileFromScratch={onRecompileFromScratch}
  37. onRunSyntaxCheckNow={onRunSyntaxCheckNow}
  38. onSetAutoCompile={onSetAutoCompile}
  39. onSetDraftMode={onSetDraftMode}
  40. onSetSyntaxCheck={onSetSyntaxCheck}
  41. onToggleLogs={onToggleLogs}
  42. outputFiles={[]}
  43. pdfDownloadUrl="/download-pdf-url"
  44. showLogs={showLogs}
  45. splitLayout={splitLayout}
  46. onSetSplitLayout={onSetSplitLayout}
  47. onSetFullLayout={onSetFullLayout}
  48. onStopCompilation={onStopCompilation}
  49. />
  50. )
  51. }
  52. it('renders the toolbar', function() {
  53. renderPreviewToolbar()
  54. screen.getByText('Recompile')
  55. screen.getByText('Download PDF')
  56. screen.getByText('View logs')
  57. })
  58. it('all toolbar items have "toolbar-item" class and text has "toolbar-text"', function() {
  59. renderPreviewToolbar()
  60. const toolbar = screen.getByTestId('toolbar-preview')
  61. for (const toolbarSection of toolbar.children) {
  62. for (const toolbarItem of toolbarSection.children) {
  63. expect(toolbarItem.className).to.contain('toolbar-item')
  64. for (const parts of toolbarItem.children) {
  65. for (const part of parts.children) {
  66. if (part.nodeName !== 'LI' && part.textContent) {
  67. expect(part.className).to.contain('toolbar-text')
  68. }
  69. }
  70. }
  71. }
  72. }
  73. })
  74. it('renders a full-screen button with a tooltip when when in split-screen mode', function() {
  75. renderPreviewToolbar()
  76. const btn = screen.getByLabelText('Full screen')
  77. fireEvent.click(btn)
  78. expect(onSetFullLayout).to.have.been.calledOnce
  79. fireEvent.mouseOver(btn)
  80. screen.getByRole('tooltip', { name: 'Full screen' })
  81. })
  82. it('renders a split-screen button with a tooltip when when in full-screen mode', function() {
  83. renderPreviewToolbar({}, {}, false, false)
  84. const btn = screen.getByLabelText('Split screen')
  85. fireEvent.click(btn)
  86. expect(onSetSplitLayout).to.have.been.calledOnce
  87. fireEvent.mouseOver(btn)
  88. screen.getByRole('tooltip', { name: 'Split screen' })
  89. })
  90. })