preview-recompile-button.test.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. function renderPreviewRecompileButton(compilerState = {}, showText) {
  82. if (!compilerState.logEntries) {
  83. compilerState.logEntries = {}
  84. }
  85. if (showText === undefined) showText = true
  86. render(
  87. <PreviewRecompileButton
  88. compilerState={{
  89. isAutoCompileOn: true,
  90. isClearingCache: false,
  91. isCompiling: false,
  92. isDraftModeOn: false,
  93. isSyntaxCheckOn: false,
  94. ...compilerState
  95. }}
  96. onRecompile={onRecompile}
  97. onRunSyntaxCheckNow={() => {}}
  98. onSetAutoCompile={() => {}}
  99. onSetDraftMode={() => {}}
  100. onSetSyntaxCheck={() => {}}
  101. onRecompileFromScratch={onRecompileFromScratch}
  102. onStopCompilation={onStopCompilation}
  103. showText={showText}
  104. />
  105. )
  106. }
  107. })