2020-12-16 17:07:09 -05:00
|
|
|
/*
|
2021-01-06 15:37:59 -05:00
|
|
|
* SPDX-FileCopyrightText: 2021 The HedgeDoc developers (see AUTHORS file)
|
2020-12-16 17:07:09 -05:00
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
*/
|
|
|
|
|
|
|
|
const imageUrl = 'http://example.com/non-existing.png'
|
|
|
|
|
2021-01-11 17:22:11 -05:00
|
|
|
describe('File upload', () => {
|
2020-12-16 17:07:09 -05:00
|
|
|
beforeEach(() => {
|
2021-02-01 16:55:49 -05:00
|
|
|
cy.visitTestEditor()
|
2020-12-16 17:07:09 -05:00
|
|
|
})
|
|
|
|
|
2021-01-11 17:22:11 -05:00
|
|
|
it('doesn\'t prevent drag\'n\'drop of plain text', () => {
|
2020-12-16 17:07:09 -05:00
|
|
|
const dataTransfer = new DataTransfer()
|
2021-01-15 16:54:43 -05:00
|
|
|
cy.codemirrorFill('line 1\nline 2\ndragline')
|
2021-01-11 17:22:11 -05:00
|
|
|
cy.get('.CodeMirror')
|
|
|
|
.click()
|
|
|
|
cy.get('.CodeMirror-line > span')
|
|
|
|
.last()
|
2020-12-16 17:07:09 -05: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 17:22:11 -05:00
|
|
|
.should('have.text', 'lindraglinee 1')
|
2020-12-16 17:07:09 -05:00
|
|
|
})
|
|
|
|
|
|
|
|
describe('upload works', () => {
|
|
|
|
beforeEach(() => {
|
|
|
|
cy.intercept({
|
|
|
|
method: 'POST',
|
2021-05-02 16:38:43 -04:00
|
|
|
url: '/mock-backend/api/private/media/upload'
|
2020-12-16 17:07:09 -05:00
|
|
|
}, {
|
|
|
|
statusCode: 201,
|
|
|
|
body: {
|
|
|
|
link: imageUrl
|
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
|
|
|
it('via button', () => {
|
|
|
|
cy.get('.fa-upload')
|
|
|
|
.click()
|
|
|
|
cy.get('div.btn-group > input[type=file]')
|
2021-03-09 17:03:54 -05:00
|
|
|
.attachFile({ filePath: 'demo.png', mimeType: 'image/png' })
|
2020-12-16 17:07:09 -05:00
|
|
|
cy.get('.CodeMirror-activeline > .CodeMirror-line > span')
|
2021-02-03 16:13:04 -05:00
|
|
|
.should('have.text', `![](${ imageUrl })`)
|
2020-12-16 17:07:09 -05:00
|
|
|
})
|
|
|
|
|
|
|
|
it('via paste', () => {
|
2021-03-09 17:03:54 -05:00
|
|
|
cy.fixture('demo.png')
|
2021-02-03 16:13:04 -05:00
|
|
|
.then((image: string) => {
|
|
|
|
const pasteEvent = {
|
|
|
|
clipboardData: {
|
2021-03-10 16:52:20 -05:00
|
|
|
files: [Cypress.Blob.base64StringToBlob(image, 'image/png')],
|
|
|
|
getData: (_: string) => ''
|
2021-02-03 16:13:04 -05:00
|
|
|
}
|
2021-01-06 07:09:33 -05:00
|
|
|
}
|
2021-02-03 16:13:04 -05:00
|
|
|
cy.get('.CodeMirror-scroll')
|
|
|
|
.trigger('paste', pasteEvent)
|
|
|
|
cy.get('.CodeMirror-activeline > .CodeMirror-line > span')
|
|
|
|
.should('have.text', `![](${ imageUrl })`)
|
|
|
|
})
|
2020-12-16 17:07:09 -05:00
|
|
|
})
|
|
|
|
|
|
|
|
it('via drag and drop', () => {
|
2021-03-09 17:03:54 -05:00
|
|
|
cy.fixture('demo.png')
|
2021-02-03 16:13:04 -05:00
|
|
|
.then((image: string) => {
|
|
|
|
const dropEvent = {
|
|
|
|
dataTransfer: {
|
|
|
|
files: [Cypress.Blob.base64StringToBlob(image, 'image/png')],
|
|
|
|
effectAllowed: 'uninitialized'
|
|
|
|
}
|
2021-01-06 07:09:33 -05:00
|
|
|
}
|
2021-02-03 16:13:04 -05: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 17:07:09 -05:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
it('upload fails', () => {
|
|
|
|
cy.intercept({
|
|
|
|
method: 'POST',
|
2021-05-02 16:38:43 -04:00
|
|
|
url: '/mock-backend/api/private/media/upload'
|
2020-12-16 17:07:09 -05:00
|
|
|
}, {
|
|
|
|
statusCode: 400
|
|
|
|
})
|
|
|
|
cy.get('.fa-upload')
|
|
|
|
.click()
|
2021-03-09 17:03:54 -05:00
|
|
|
cy.fixture('demo.png')
|
2021-02-03 16:13:04 -05:00
|
|
|
.then(() => {
|
|
|
|
cy.get('input[type=file]')
|
2021-03-09 17:03:54 -05:00
|
|
|
.attachFile({ filePath: 'demo.png', mimeType: 'image/png' })
|
2021-02-03 16:13:04 -05:00
|
|
|
})
|
2021-01-15 16:54:43 -05:00
|
|
|
cy.get('.CodeMirror-activeline > .CodeMirror-line > span > span')
|
|
|
|
.should('have.text', String.fromCharCode(8203)) //thanks codemirror....
|
2020-12-16 17:07:09 -05:00
|
|
|
})
|
2020-12-21 05:58:43 -05:00
|
|
|
|
|
|
|
it('text paste still works', () => {
|
|
|
|
const testText = 'a long test text'
|
|
|
|
const pasteEvent = {
|
|
|
|
clipboardData: {
|
|
|
|
getData: (type = 'text') => testText
|
|
|
|
}
|
|
|
|
}
|
2021-02-03 16:13:04 -05:00
|
|
|
cy.get('.CodeMirror-scroll')
|
|
|
|
.trigger('paste', pasteEvent)
|
2020-12-21 05:58:43 -05:00
|
|
|
cy.get('.CodeMirror-activeline > .CodeMirror-line > span')
|
2021-02-03 16:13:04 -05:00
|
|
|
.should('have.text', `${ testText }`)
|
2020-12-21 05:58:43 -05:00
|
|
|
})
|
2020-12-16 17:07:09 -05:00
|
|
|
})
|