preview-recompile-button.test.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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, onClearCache
  8. beforeEach(function() {
  9. onRecompile = sinon.stub().resolves()
  10. onClearCache = sinon.stub().resolves()
  11. })
  12. it('renders all items', function() {
  13. renderPreviewRecompileButton()
  14. const menuItems = screen.getAllByRole('menuitem')
  15. expect(menuItems.length).to.equal(8)
  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. 'Recompile from scratch'
  25. ])
  26. const menuHeadingItems = screen.getAllByRole('heading')
  27. expect(menuHeadingItems.length).to.equal(3)
  28. expect(menuHeadingItems.map(item => item.textContent)).to.deep.equal([
  29. 'Auto Compile',
  30. 'Compile Mode',
  31. 'Syntax Checks'
  32. ])
  33. })
  34. describe('Recompile from scratch', function() {
  35. describe('click', function() {
  36. it('should call onClearCache and onRecompile', async function() {
  37. renderPreviewRecompileButton()
  38. const button = screen.getByRole('menuitem', {
  39. name: 'Recompile from scratch'
  40. })
  41. await fireEvent.click(button)
  42. expect(onClearCache).to.have.been.calledOnce
  43. expect(onRecompile).to.have.been.calledOnce
  44. })
  45. })
  46. describe('processing', function() {
  47. it('shows processing view and disable menuItem when clearing cache', function() {
  48. renderPreviewRecompileButton({ isClearingCache: 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. it('shows processing view and disable menuItem when recompiling', function() {
  67. renderPreviewRecompileButton({ isCompiling: true })
  68. screen.getByRole('button', { name: 'Compiling …' })
  69. expect(
  70. screen
  71. .getByRole('menuitem', {
  72. name: 'Recompile from scratch'
  73. })
  74. .getAttribute('aria-disabled')
  75. ).to.equal('true')
  76. expect(
  77. screen
  78. .getByRole('menuitem', {
  79. name: 'Recompile from scratch'
  80. })
  81. .closest('li')
  82. .getAttribute('class')
  83. ).to.equal('disabled')
  84. })
  85. })
  86. })
  87. function renderPreviewRecompileButton(compilerState = {}) {
  88. render(
  89. <PreviewRecompileButton
  90. compilerState={{
  91. isAutoCompileOn: true,
  92. isClearingCache: false,
  93. isCompiling: false,
  94. isDraftModeOn: false,
  95. isSyntaxCheckOn: false,
  96. ...compilerState
  97. }}
  98. onRecompile={onRecompile}
  99. onRunSyntaxCheckNow={() => {}}
  100. onSetAutoCompile={() => {}}
  101. onSetDraftMode={() => {}}
  102. onSetSyntaxCheck={() => {}}
  103. onClearCache={onClearCache}
  104. />
  105. )
  106. }
  107. })