preview-recompile-button.test.js 4.7 KB

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