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-01-11 22:22:11 +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-01-11 22:22:11 +00:00
|
|
|
cy.get('.CodeMirror')
|
|
|
|
.click()
|
|
|
|
cy.get('.CodeMirror-line > span')
|
|
|
|
.last()
|
2020-12-16 22:07:09 +00:00
|
|
|
.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')
|
2021-01-11 22:22:11 +00:00
|
|
|
.should('have.text', 'lindraglinee 1')
|
2020-12-16 22:07:09 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
describe('upload works', () => {
|
|
|
|
beforeEach(() => {
|
|
|
|
cy.intercept({
|
2021-06-12 16:10:42 +00:00
|
|
|
method: 'GET',
|
2021-06-09 19:54:12 +00:00
|
|
|
url: '/mock-backend/api/private/media/upload-post'
|
2020-12-16 22:07:09 +00:00
|
|
|
}, {
|
2021-06-12 16:12:50 +00:00
|
|
|
statusCode: 200,
|
2020-12-16 22:07:09 +00:00
|
|
|
body: {
|
|
|
|
link: imageUrl
|
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
|
|
|
it('via button', () => {
|
|
|
|
cy.get('.fa-upload')
|
|
|
|
.click()
|
|
|
|
cy.get('div.btn-group > input[type=file]')
|
2021-03-09 22:03:54 +00:00
|
|
|
.attachFile({ filePath: 'demo.png', mimeType: 'image/png' })
|
2020-12-16 22:07:09 +00:00
|
|
|
cy.get('.CodeMirror-activeline > .CodeMirror-line > span')
|
2021-02-03 21:13:04 +00:00
|
|
|
.should('have.text', `![](${ imageUrl })`)
|
2020-12-16 22:07:09 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
it('via paste', () => {
|
2021-03-09 22:03:54 +00:00
|
|
|
cy.fixture('demo.png')
|
2021-02-03 21:13:04 +00:00
|
|
|
.then((image: string) => {
|
|
|
|
const pasteEvent = {
|
|
|
|
clipboardData: {
|
2021-03-10 21:52:20 +00:00
|
|
|
files: [Cypress.Blob.base64StringToBlob(image, 'image/png')],
|
|
|
|
getData: (_: string) => ''
|
2021-02-03 21:13:04 +00:00
|
|
|
}
|
2021-01-06 12:09:33 +00:00
|
|
|
}
|
2021-02-03 21:13: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-03-09 22:03:54 +00:00
|
|
|
cy.fixture('demo.png')
|
2021-02-03 21:13:04 +00:00
|
|
|
.then((image: string) => {
|
|
|
|
const dropEvent = {
|
|
|
|
dataTransfer: {
|
|
|
|
files: [Cypress.Blob.base64StringToBlob(image, 'image/png')],
|
|
|
|
effectAllowed: 'uninitialized'
|
|
|
|
}
|
2021-01-06 12:09:33 +00:00
|
|
|
}
|
2021-02-03 21:13: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
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
it('upload fails', () => {
|
|
|
|
cy.intercept({
|
2021-06-12 15:52:02 +00:00
|
|
|
method: 'GET',
|
2021-06-09 19:54:12 +00:00
|
|
|
url: '/mock-backend/api/private/media/upload-post'
|
2020-12-16 22:07:09 +00:00
|
|
|
}, {
|
|
|
|
statusCode: 400
|
|
|
|
})
|
|
|
|
cy.get('.fa-upload')
|
|
|
|
.click()
|
2021-03-09 22:03:54 +00:00
|
|
|
cy.fixture('demo.png')
|
2021-02-03 21:13:04 +00:00
|
|
|
.then(() => {
|
|
|
|
cy.get('input[type=file]')
|
2021-03-09 22:03:54 +00:00
|
|
|
.attachFile({ filePath: 'demo.png', mimeType: 'image/png' })
|
2021-02-03 21:13:04 +00:00
|
|
|
})
|
2021-01-15 21:54:43 +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
|
|
|
|
|
|
|
it('text paste still works', () => {
|
|
|
|
const testText = 'a long test text'
|
|
|
|
const pasteEvent = {
|
|
|
|
clipboardData: {
|
|
|
|
getData: (type = 'text') => testText
|
|
|
|
}
|
|
|
|
}
|
2021-02-03 21:13:04 +00:00
|
|
|
cy.get('.CodeMirror-scroll')
|
|
|
|
.trigger('paste', pasteEvent)
|
2020-12-21 10:58:43 +00:00
|
|
|
cy.get('.CodeMirror-activeline > .CodeMirror-line > span')
|
2021-02-03 21:13:04 +00:00
|
|
|
.should('have.text', `${ testText }`)
|
2020-12-21 10:58:43 +00:00
|
|
|
})
|
2020-12-16 22:07:09 +00:00
|
|
|
})
|