2020-12-16 22:07:09 +00:00
|
|
|
/*
|
2021-01-06 20:37:59 +00:00
|
|
|
* SPDX-FileCopyrightText: 2021 The HedgeDoc developers (see AUTHORS file)
|
2020-12-16 22:07:09 +00:00
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
*/
|
|
|
|
|
|
|
|
const imageUrl = 'http://example.com/non-existing.png'
|
|
|
|
|
2021-01-11 22:22:11 +00:00
|
|
|
describe('File upload', () => {
|
2020-12-16 22:07:09 +00:00
|
|
|
beforeEach(() => {
|
2021-02-01 21:55:49 +00:00
|
|
|
cy.visitTestEditor()
|
2020-12-16 22:07:09 +00:00
|
|
|
})
|
|
|
|
|
2021-11-19 17:04:04 +00:00
|
|
|
it("doesn't prevent drag'n'drop of plain text", () => {
|
2020-12-16 22:07:09 +00:00
|
|
|
const dataTransfer = new DataTransfer()
|
2021-10-04 10:50:39 +00:00
|
|
|
cy.setCodemirrorContent('line 1\nline 2\ndragline')
|
2021-11-19 17:04:04 +00:00
|
|
|
cy.get('.CodeMirror').click()
|
|
|
|
cy.get('.CodeMirror-line > span').last().dblclick()
|
|
|
|
cy.get('.CodeMirror-line > span > .cm-matchhighlight').trigger('dragstart', { dataTransfer })
|
|
|
|
cy.get('.CodeMirror-code > div:nth-of-type(1) > .CodeMirror-line > span > span').trigger('drop', { dataTransfer })
|
|
|
|
cy.get('.CodeMirror-code > div:nth-of-type(1) > .CodeMirror-line > span > span').should(
|
|
|
|
'have.text',
|
|
|
|
'lindraglinee 1'
|
|
|
|
)
|
2020-12-16 22:07:09 +00:00
|
|
|
})
|
|
|
|
|
2021-11-19 17:04:04 +00:00
|
|
|
describe('works', () => {
|
2020-12-16 22:07:09 +00:00
|
|
|
beforeEach(() => {
|
2021-11-19 17:04:04 +00:00
|
|
|
cy.intercept(
|
|
|
|
{
|
|
|
|
method: 'GET',
|
|
|
|
url: '/mock-backend/api/private/media/upload-post'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
statusCode: 200,
|
|
|
|
body: {
|
|
|
|
link: imageUrl
|
|
|
|
}
|
2020-12-16 22:07:09 +00:00
|
|
|
}
|
2021-11-19 17:04:04 +00:00
|
|
|
)
|
2020-12-16 22:07:09 +00:00
|
|
|
})
|
|
|
|
it('via button', () => {
|
2021-11-19 17:04:04 +00:00
|
|
|
cy.getById('editor-toolbar-upload-image-button').click()
|
|
|
|
cy.getById('editor-toolbar-upload-image-input').attachFile({ filePath: 'demo.png', mimeType: 'image/png' })
|
|
|
|
cy.get('.CodeMirror-activeline > .CodeMirror-line > span').should('have.text', `![](${imageUrl})`)
|
2020-12-16 22:07:09 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
it('via paste', () => {
|
2021-11-19 17:04:04 +00:00
|
|
|
cy.fixture('demo.png').then((image: string) => {
|
|
|
|
const pasteEvent = {
|
|
|
|
clipboardData: {
|
|
|
|
files: [Cypress.Blob.base64StringToBlob(image, 'image/png')],
|
|
|
|
getData: (_: string) => ''
|
2021-01-06 12:09:33 +00:00
|
|
|
}
|
2021-11-19 17:04:04 +00:00
|
|
|
}
|
|
|
|
cy.get('.CodeMirror-scroll').trigger('paste', pasteEvent)
|
|
|
|
cy.get('.CodeMirror-activeline > .CodeMirror-line > span').should('have.text', `![](${imageUrl})`)
|
|
|
|
})
|
2020-12-16 22:07:09 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
it('via drag and drop', () => {
|
2021-11-19 17:04:04 +00:00
|
|
|
cy.fixture('demo.png').then((image: string) => {
|
|
|
|
const dropEvent = {
|
|
|
|
dataTransfer: {
|
|
|
|
files: [Cypress.Blob.base64StringToBlob(image, 'image/png')],
|
|
|
|
effectAllowed: 'uninitialized'
|
2021-01-06 12:09:33 +00:00
|
|
|
}
|
2021-11-19 17:04:04 +00:00
|
|
|
}
|
|
|
|
cy.get('.CodeMirror-scroll').trigger('dragenter', dropEvent)
|
|
|
|
cy.get('.CodeMirror-scroll').trigger('drop', dropEvent)
|
|
|
|
cy.get('.CodeMirror-activeline > .CodeMirror-line > span').should('have.text', `![](${imageUrl})`)
|
|
|
|
})
|
2020-12-16 22:07:09 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2021-11-19 17:04:04 +00:00
|
|
|
it('fails', () => {
|
|
|
|
cy.intercept(
|
|
|
|
{
|
|
|
|
method: 'GET',
|
|
|
|
url: '/mock-backend/api/private/media/upload-post'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
statusCode: 400
|
|
|
|
}
|
|
|
|
)
|
|
|
|
cy.getById('editor-toolbar-upload-image-button').click()
|
|
|
|
cy.fixture('demo.png').then(() => {
|
|
|
|
cy.getById('editor-toolbar-upload-image-input').attachFile({ filePath: 'demo.png', mimeType: 'image/png' })
|
2020-12-16 22:07:09 +00:00
|
|
|
})
|
2021-11-19 17:04:04 +00:00
|
|
|
cy.get('.CodeMirror-activeline > .CodeMirror-line > span > span').should('have.text', String.fromCharCode(8203)) //thanks codemirror....
|
2020-12-16 22:07:09 +00:00
|
|
|
})
|
2020-12-21 10:58:43 +00:00
|
|
|
|
2021-11-19 17:04:04 +00:00
|
|
|
it('lets text paste still work', () => {
|
2020-12-21 10:58:43 +00:00
|
|
|
const testText = 'a long test text'
|
|
|
|
const pasteEvent = {
|
|
|
|
clipboardData: {
|
|
|
|
getData: (type = 'text') => testText
|
|
|
|
}
|
|
|
|
}
|
2021-11-19 17:04:04 +00:00
|
|
|
cy.get('.CodeMirror-scroll').trigger('paste', pasteEvent)
|
|
|
|
cy.get('.CodeMirror-activeline > .CodeMirror-line > span').should('have.text', `${testText}`)
|
2020-12-21 10:58:43 +00:00
|
|
|
})
|
2020-12-16 22:07:09 +00:00
|
|
|
})
|