2020-10-20 08:44:19 -04:00
|
|
|
|
import sinon from 'sinon'
|
|
|
|
|
import { screen, render, fireEvent } from '@testing-library/react'
|
|
|
|
|
import PreviewRecompileButton from '../../../../../frontend/js/features/preview/components/preview-recompile-button'
|
|
|
|
|
const { expect } = require('chai')
|
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
|
describe('<PreviewRecompileButton />', function () {
|
2020-12-02 05:03:03 -05:00
|
|
|
|
let onRecompile, onRecompileFromScratch, onStopCompilation
|
2020-10-20 08:44:19 -04:00
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
|
beforeEach(function () {
|
2020-10-20 08:44:19 -04:00
|
|
|
|
onRecompile = sinon.stub().resolves()
|
2020-12-02 05:03:03 -05:00
|
|
|
|
onRecompileFromScratch = sinon.stub().resolves()
|
|
|
|
|
onStopCompilation = sinon.stub().resolves()
|
2020-10-20 08:44:19 -04:00
|
|
|
|
})
|
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
|
it('renders all items', function () {
|
2020-10-20 08:44:19 -04:00
|
|
|
|
renderPreviewRecompileButton()
|
|
|
|
|
|
|
|
|
|
const menuItems = screen.getAllByRole('menuitem')
|
2020-12-02 05:03:03 -05:00
|
|
|
|
expect(menuItems.length).to.equal(9)
|
2020-10-20 08:44:19 -04:00
|
|
|
|
expect(menuItems.map(item => item.textContent)).to.deep.equal([
|
|
|
|
|
'On',
|
|
|
|
|
'Off',
|
|
|
|
|
'Normal',
|
|
|
|
|
'Fast [draft]',
|
|
|
|
|
'Check syntax before compile',
|
2021-08-16 10:59:04 -04:00
|
|
|
|
'Don’t check syntax',
|
2020-10-20 08:44:19 -04:00
|
|
|
|
'Run syntax check now',
|
2020-12-02 05:03:03 -05:00
|
|
|
|
'Stop compilation',
|
2021-04-27 03:52:58 -04:00
|
|
|
|
'Recompile from scratch',
|
2020-10-20 08:44:19 -04:00
|
|
|
|
])
|
|
|
|
|
|
|
|
|
|
const menuHeadingItems = screen.getAllByRole('heading')
|
|
|
|
|
expect(menuHeadingItems.length).to.equal(3)
|
|
|
|
|
expect(menuHeadingItems.map(item => item.textContent)).to.deep.equal([
|
|
|
|
|
'Auto Compile',
|
|
|
|
|
'Compile Mode',
|
2021-04-27 03:52:58 -04:00
|
|
|
|
'Syntax Checks',
|
2020-10-20 08:44:19 -04:00
|
|
|
|
])
|
|
|
|
|
})
|
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
|
describe('Recompile from scratch', function () {
|
|
|
|
|
describe('click', function () {
|
|
|
|
|
it('should call onRecompileFromScratch', async function () {
|
2020-10-20 08:44:19 -04:00
|
|
|
|
renderPreviewRecompileButton()
|
|
|
|
|
|
|
|
|
|
const button = screen.getByRole('menuitem', {
|
2021-04-27 03:52:58 -04:00
|
|
|
|
name: 'Recompile from scratch',
|
2020-10-20 08:44:19 -04:00
|
|
|
|
})
|
|
|
|
|
await fireEvent.click(button)
|
2020-12-02 05:03:03 -05:00
|
|
|
|
expect(onRecompileFromScratch).to.have.been.calledOnce
|
2020-10-20 08:44:19 -04:00
|
|
|
|
})
|
|
|
|
|
})
|
2021-04-14 09:17:21 -04:00
|
|
|
|
describe('processing', function () {
|
|
|
|
|
it('shows processing view and disable menuItem when recompiling', function () {
|
2020-10-20 08:44:19 -04:00
|
|
|
|
renderPreviewRecompileButton({ isCompiling: true })
|
|
|
|
|
|
2021-02-03 07:36:55 -05:00
|
|
|
|
screen.getByRole('button', { name: 'Compiling…' })
|
2020-10-20 08:44:19 -04:00
|
|
|
|
expect(
|
|
|
|
|
screen
|
|
|
|
|
.getByRole('menuitem', {
|
2021-04-27 03:52:58 -04:00
|
|
|
|
name: 'Recompile from scratch',
|
2020-10-20 08:44:19 -04:00
|
|
|
|
})
|
|
|
|
|
.getAttribute('aria-disabled')
|
|
|
|
|
).to.equal('true')
|
|
|
|
|
expect(
|
|
|
|
|
screen
|
|
|
|
|
.getByRole('menuitem', {
|
2021-04-27 03:52:58 -04:00
|
|
|
|
name: 'Recompile from scratch',
|
2020-10-20 08:44:19 -04:00
|
|
|
|
})
|
|
|
|
|
.closest('li')
|
|
|
|
|
.getAttribute('class')
|
|
|
|
|
).to.equal('disabled')
|
|
|
|
|
})
|
|
|
|
|
})
|
|
|
|
|
})
|
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
|
it('should show the button text when prop showText=true', function () {
|
2020-11-09 09:52:22 -05:00
|
|
|
|
const showText = true
|
|
|
|
|
renderPreviewRecompileButton({}, showText)
|
|
|
|
|
expect(screen.getByText('Recompile').getAttribute('style')).to.be.null
|
|
|
|
|
})
|
2021-04-14 09:17:21 -04:00
|
|
|
|
it('should not show the button text when prop showText=false', function () {
|
2020-11-09 09:52:22 -05:00
|
|
|
|
const showText = false
|
|
|
|
|
renderPreviewRecompileButton({}, showText)
|
|
|
|
|
expect(screen.getByText('Recompile').getAttribute('style')).to.equal(
|
|
|
|
|
'position: absolute; right: -100vw;'
|
|
|
|
|
)
|
|
|
|
|
})
|
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
|
describe('Autocompile feedback', function () {
|
|
|
|
|
it('shows animated visual feedback via CSS class when there are uncompiled changes', function () {
|
2021-03-18 09:49:01 -04:00
|
|
|
|
const { container } = renderPreviewRecompileButton({
|
|
|
|
|
autoCompileHasChanges: true,
|
2021-04-27 03:52:58 -04:00
|
|
|
|
autoCompileHasLintingError: false,
|
2021-03-18 09:49:01 -04:00
|
|
|
|
})
|
|
|
|
|
const recompileBtnGroupEl = container.querySelector(
|
|
|
|
|
'.btn-recompile-group'
|
|
|
|
|
)
|
|
|
|
|
expect(
|
|
|
|
|
recompileBtnGroupEl.classList.contains(
|
|
|
|
|
'btn-recompile-group-has-changes'
|
|
|
|
|
)
|
|
|
|
|
).to.be.true
|
|
|
|
|
})
|
2021-04-14 09:17:21 -04:00
|
|
|
|
it('does not show animated visual feedback via CSS class when there are no uncompiled changes', function () {
|
2021-03-18 09:49:01 -04:00
|
|
|
|
const { container } = renderPreviewRecompileButton({
|
|
|
|
|
autoCompileHasChanges: false,
|
2021-04-27 03:52:58 -04:00
|
|
|
|
autoCompileHasLintingError: false,
|
2021-03-18 09:49:01 -04:00
|
|
|
|
})
|
|
|
|
|
const recompileBtnGroupEl = container.querySelector(
|
|
|
|
|
'.btn-recompile-group'
|
|
|
|
|
)
|
|
|
|
|
expect(
|
|
|
|
|
recompileBtnGroupEl.classList.contains(
|
|
|
|
|
'btn-recompile-group-has-changes'
|
|
|
|
|
)
|
|
|
|
|
).to.be.false
|
|
|
|
|
})
|
|
|
|
|
})
|
|
|
|
|
|
2020-11-09 09:52:22 -05:00
|
|
|
|
function renderPreviewRecompileButton(compilerState = {}, showText) {
|
2020-11-02 09:45:55 -05:00
|
|
|
|
if (!compilerState.logEntries) {
|
|
|
|
|
compilerState.logEntries = {}
|
|
|
|
|
}
|
2020-11-09 09:52:22 -05:00
|
|
|
|
if (showText === undefined) showText = true
|
2021-03-18 09:49:01 -04:00
|
|
|
|
return render(
|
2020-10-20 08:44:19 -04:00
|
|
|
|
<PreviewRecompileButton
|
|
|
|
|
compilerState={{
|
2021-03-18 09:49:01 -04:00
|
|
|
|
autoCompileHasChanges: false,
|
2020-10-20 08:44:19 -04:00
|
|
|
|
isAutoCompileOn: true,
|
|
|
|
|
isClearingCache: false,
|
|
|
|
|
isCompiling: false,
|
|
|
|
|
isDraftModeOn: false,
|
|
|
|
|
isSyntaxCheckOn: false,
|
2021-04-27 03:52:58 -04:00
|
|
|
|
...compilerState,
|
2020-10-20 08:44:19 -04:00
|
|
|
|
}}
|
|
|
|
|
onRecompile={onRecompile}
|
|
|
|
|
onRunSyntaxCheckNow={() => {}}
|
|
|
|
|
onSetAutoCompile={() => {}}
|
|
|
|
|
onSetDraftMode={() => {}}
|
|
|
|
|
onSetSyntaxCheck={() => {}}
|
2020-12-02 05:03:03 -05:00
|
|
|
|
onRecompileFromScratch={onRecompileFromScratch}
|
|
|
|
|
onStopCompilation={onStopCompilation}
|
2020-11-09 09:52:22 -05:00
|
|
|
|
showText={showText}
|
2020-10-20 08:44:19 -04:00
|
|
|
|
/>
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
})
|