2020-10-20 08:44:19 -04:00
|
|
|
import React from 'react'
|
|
|
|
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',
|
|
|
|
"Don't check syntax",
|
|
|
|
'Run syntax check now',
|
2020-12-02 05:03:03 -05:00
|
|
|
'Stop compilation',
|
2020-10-20 08:44:19 -04:00
|
|
|
'Recompile from scratch'
|
|
|
|
])
|
|
|
|
|
|
|
|
const menuHeadingItems = screen.getAllByRole('heading')
|
|
|
|
expect(menuHeadingItems.length).to.equal(3)
|
|
|
|
expect(menuHeadingItems.map(item => item.textContent)).to.deep.equal([
|
|
|
|
'Auto Compile',
|
|
|
|
'Compile Mode',
|
|
|
|
'Syntax Checks'
|
|
|
|
])
|
|
|
|
})
|
|
|
|
|
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', {
|
|
|
|
name: 'Recompile from scratch'
|
|
|
|
})
|
|
|
|
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', {
|
|
|
|
name: 'Recompile from scratch'
|
|
|
|
})
|
|
|
|
.getAttribute('aria-disabled')
|
|
|
|
).to.equal('true')
|
|
|
|
expect(
|
|
|
|
screen
|
|
|
|
.getByRole('menuitem', {
|
|
|
|
name: 'Recompile from scratch'
|
|
|
|
})
|
|
|
|
.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,
|
|
|
|
autoCompileHasLintingError: false
|
|
|
|
})
|
|
|
|
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,
|
|
|
|
autoCompileHasLintingError: false
|
|
|
|
})
|
|
|
|
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,
|
|
|
|
...compilerState
|
|
|
|
}}
|
|
|
|
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
|
|
|
/>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
})
|