2020-10-20 08:44:32 -04:00
|
|
|
import { expect } from 'chai'
|
|
|
|
import { screen, render } from '@testing-library/react'
|
|
|
|
|
2020-12-02 05:03:03 -05:00
|
|
|
import PreviewDownloadButton from '../../../../../frontend/js/features/preview/components/preview-download-button'
|
2020-10-20 08:44:32 -04:00
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
describe('<PreviewDownloadButton />', function () {
|
2020-10-20 08:44:32 -04:00
|
|
|
const projectId = 'projectId123'
|
|
|
|
const pdfDownloadUrl = `/download/project/${projectId}/build/17523aaafdf-1ad9063af140f004/output/output.pdf?compileGroup=priority&popupDownload=true`
|
|
|
|
|
2020-11-09 09:52:22 -05:00
|
|
|
function renderPreviewDownloadButton(
|
|
|
|
isCompiling,
|
|
|
|
outputFiles,
|
|
|
|
pdfDownloadUrl,
|
|
|
|
showText
|
|
|
|
) {
|
|
|
|
if (isCompiling === undefined) isCompiling = false
|
|
|
|
if (showText === undefined) showText = true
|
2020-10-20 08:44:32 -04:00
|
|
|
render(
|
|
|
|
<PreviewDownloadButton
|
|
|
|
isCompiling={isCompiling}
|
2020-11-09 09:52:22 -05:00
|
|
|
outputFiles={outputFiles || []}
|
|
|
|
pdfDownloadUrl={pdfDownloadUrl}
|
|
|
|
showText={showText}
|
2020-10-20 08:44:32 -04:00
|
|
|
/>
|
|
|
|
)
|
2020-11-09 09:52:22 -05:00
|
|
|
}
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
it('should disable the button and dropdown toggle when compiling', function () {
|
2020-11-09 09:52:22 -05:00
|
|
|
const isCompiling = true
|
|
|
|
const outputFiles = undefined
|
|
|
|
|
|
|
|
renderPreviewDownloadButton(isCompiling, outputFiles)
|
|
|
|
|
|
|
|
expect(
|
2021-04-14 09:17:21 -04:00
|
|
|
screen.getByText('Download PDF').closest('a').getAttribute('disabled')
|
2020-11-09 09:52:22 -05:00
|
|
|
).to.exist
|
2020-10-20 08:44:32 -04:00
|
|
|
const buttons = screen.getAllByRole('button')
|
|
|
|
expect(buttons.length).to.equal(1) // the dropdown toggle
|
|
|
|
expect(buttons[0].getAttribute('disabled')).to.exist
|
|
|
|
expect(buttons[0].getAttribute('aria-label')).to.equal(
|
|
|
|
'Toggle output files list'
|
|
|
|
)
|
|
|
|
})
|
2021-04-14 09:17:21 -04:00
|
|
|
it('should disable the PDF button when there is no PDF', function () {
|
2020-10-20 08:44:32 -04:00
|
|
|
const isCompiling = false
|
|
|
|
const outputFiles = []
|
2020-11-09 09:52:22 -05:00
|
|
|
renderPreviewDownloadButton(isCompiling, outputFiles)
|
|
|
|
expect(
|
2021-04-14 09:17:21 -04:00
|
|
|
screen.getByText('Download PDF').closest('a').getAttribute('disabled')
|
2020-11-09 09:52:22 -05:00
|
|
|
).to.exist
|
2020-10-20 08:44:32 -04:00
|
|
|
})
|
2021-04-14 09:17:21 -04:00
|
|
|
it('should enable the PDF button when there is a main PDF', function () {
|
2020-10-20 08:44:32 -04:00
|
|
|
const isCompiling = false
|
|
|
|
const outputFiles = []
|
2020-11-09 09:52:22 -05:00
|
|
|
renderPreviewDownloadButton(isCompiling, outputFiles, pdfDownloadUrl)
|
|
|
|
expect(
|
2021-04-14 09:17:21 -04:00
|
|
|
screen.getByText('Download PDF').closest('a').getAttribute('href')
|
2020-11-09 09:52:22 -05:00
|
|
|
).to.equal(pdfDownloadUrl)
|
|
|
|
expect(
|
2021-04-14 09:17:21 -04:00
|
|
|
screen.getByText('Download PDF').closest('a').getAttribute('disabled')
|
2020-11-09 09:52:22 -05:00
|
|
|
).to.not.exist
|
2020-10-20 08:44:32 -04:00
|
|
|
})
|
2021-04-14 09:17:21 -04:00
|
|
|
it('should enable the dropdown when not compiling', function () {
|
2020-10-20 08:44:32 -04:00
|
|
|
const isCompiling = false
|
|
|
|
const outputFiles = []
|
2020-11-09 09:52:22 -05:00
|
|
|
renderPreviewDownloadButton(isCompiling, outputFiles, pdfDownloadUrl)
|
2020-10-20 08:44:32 -04:00
|
|
|
const buttons = screen.getAllByRole('button')
|
|
|
|
expect(buttons[0]).to.exist
|
|
|
|
expect(buttons[0].getAttribute('disabled')).to.not.exist
|
|
|
|
})
|
2020-11-09 09:52:22 -05:00
|
|
|
|
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 isCompiling = false
|
|
|
|
const showText = true
|
|
|
|
renderPreviewDownloadButton(isCompiling, [], pdfDownloadUrl, showText)
|
|
|
|
expect(screen.getByText('Download PDF').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 isCompiling = false
|
|
|
|
const showText = false
|
|
|
|
renderPreviewDownloadButton(isCompiling, [], pdfDownloadUrl, showText)
|
|
|
|
expect(screen.getByText('Download PDF').getAttribute('style')).to.equal(
|
|
|
|
'position: absolute; right: -100vw;'
|
|
|
|
)
|
|
|
|
})
|
2020-10-20 08:44:32 -04:00
|
|
|
})
|