preview-recompile-button.test.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. import sinon from 'sinon'
  2. import { screen, render, fireEvent } from '@testing-library/react'
  3. import PreviewRecompileButton from '../../../../../frontend/js/features/preview/components/preview-recompile-button'
  4. const { expect } = require('chai')
  5. describe('<PreviewRecompileButton />', function () {
  6. let onRecompile, onRecompileFromScratch, onStopCompilation
  7. beforeEach(function () {
  8. onRecompile = sinon.stub().resolves()
  9. onRecompileFromScratch = sinon.stub().resolves()
  10. onStopCompilation = sinon.stub().resolves()
  11. })
  12. it('renders all items', function () {
  13. renderPreviewRecompileButton()
  14. const menuItems = screen.getAllByRole('menuitem')
  15. expect(menuItems.length).to.equal(9)
  16. expect(menuItems.map(item => item.textContent)).to.deep.equal([
  17. 'On',
  18. 'Off',
  19. 'Normal',
  20. 'Fast [draft]',
  21. 'Check syntax before compile',
  22. 'Don’t check syntax',
  23. 'Run syntax check now',
  24. 'Stop compilation',
  25. 'Recompile from scratch',
  26. ])
  27. const menuHeadingItems = screen.getAllByRole('heading')
  28. expect(menuHeadingItems.length).to.equal(3)
  29. expect(menuHeadingItems.map(item => item.textContent)).to.deep.equal([
  30. 'Auto Compile',
  31. 'Compile Mode',
  32. 'Syntax Checks',
  33. ])
  34. })
  35. describe('Recompile from scratch', function () {
  36. describe('click', function () {
  37. it('should call onRecompileFromScratch', async function () {
  38. renderPreviewRecompileButton()
  39. const button = screen.getByRole('menuitem', {
  40. name: 'Recompile from scratch',
  41. })
  42. await fireEvent.click(button)
  43. expect(onRecompileFromScratch).to.have.been.calledOnce
  44. })
  45. })
  46. describe('processing', function () {
  47. it('shows processing view and disable menuItem when recompiling', function () {
  48. renderPreviewRecompileButton({ isCompiling: true })
  49. screen.getByRole('button', { name: 'Compiling…' })
  50. expect(
  51. screen
  52. .getByRole('menuitem', {
  53. name: 'Recompile from scratch',
  54. })
  55. .getAttribute('aria-disabled')
  56. ).to.equal('true')
  57. expect(
  58. screen
  59. .getByRole('menuitem', {
  60. name: 'Recompile from scratch',
  61. })
  62. .closest('li')
  63. .getAttribute('class')
  64. ).to.equal('disabled')
  65. })
  66. })
  67. })
  68. it('should show the button text when prop showText=true', function () {
  69. const showText = true
  70. renderPreviewRecompileButton({}, showText)
  71. expect(screen.getByText('Recompile').getAttribute('style')).to.be.null
  72. })
  73. it('should not show the button text when prop showText=false', function () {
  74. const showText = false
  75. renderPreviewRecompileButton({}, showText)
  76. expect(screen.getByText('Recompile').getAttribute('style')).to.equal(
  77. 'position: absolute; right: -100vw;'
  78. )
  79. })
  80. describe('Autocompile feedback', function () {
  81. it('shows animated visual feedback via CSS class when there are uncompiled changes', function () {
  82. const { container } = renderPreviewRecompileButton({
  83. autoCompileHasChanges: true,
  84. autoCompileHasLintingError: false,
  85. })
  86. const recompileBtnGroupEl = container.querySelector(
  87. '.btn-recompile-group'
  88. )
  89. expect(
  90. recompileBtnGroupEl.classList.contains(
  91. 'btn-recompile-group-has-changes'
  92. )
  93. ).to.be.true
  94. })
  95. it('does not show animated visual feedback via CSS class when there are no uncompiled changes', function () {
  96. const { container } = renderPreviewRecompileButton({
  97. autoCompileHasChanges: false,
  98. autoCompileHasLintingError: false,
  99. })
  100. const recompileBtnGroupEl = container.querySelector(
  101. '.btn-recompile-group'
  102. )
  103. expect(
  104. recompileBtnGroupEl.classList.contains(
  105. 'btn-recompile-group-has-changes'
  106. )
  107. ).to.be.false
  108. })
  109. })
  110. function renderPreviewRecompileButton(compilerState = {}, showText) {
  111. if (!compilerState.logEntries) {
  112. compilerState.logEntries = {}
  113. }
  114. if (showText === undefined) showText = true
  115. return render(
  116. <PreviewRecompileButton
  117. compilerState={{
  118. autoCompileHasChanges: false,
  119. isAutoCompileOn: true,
  120. isClearingCache: false,
  121. isCompiling: false,
  122. isDraftModeOn: false,
  123. isSyntaxCheckOn: false,
  124. ...compilerState,
  125. }}
  126. onRecompile={onRecompile}
  127. onRunSyntaxCheckNow={() => {}}
  128. onSetAutoCompile={() => {}}
  129. onSetDraftMode={() => {}}
  130. onSetSyntaxCheck={() => {}}
  131. onRecompileFromScratch={onRecompileFromScratch}
  132. onStopCompilation={onStopCompilation}
  133. showText={showText}
  134. />
  135. )
  136. }
  137. })