2020-12-16 17:07:09 -05:00
|
|
|
/*
|
2022-04-15 17:03:15 -04:00
|
|
|
* SPDX-FileCopyrightText: 2022 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(() => {
|
2022-01-30 15:46:43 -05:00
|
|
|
cy.visitTestNote()
|
2022-02-05 13:32:58 -05:00
|
|
|
cy.fixture('demo.png').as('demoImage')
|
2020-12-16 17:07:09 -05:00
|
|
|
})
|
|
|
|
|
2021-11-19 12:04:04 -05:00
|
|
|
describe('works', () => {
|
2020-12-16 17:07:09 -05:00
|
|
|
beforeEach(() => {
|
2021-11-19 12:04:04 -05:00
|
|
|
cy.intercept(
|
|
|
|
{
|
2022-04-15 17:03:15 -04:00
|
|
|
method: 'POST',
|
|
|
|
url: '/api/mock-backend/private/media'
|
2021-11-19 12:04:04 -05:00
|
|
|
},
|
|
|
|
{
|
2022-04-15 17:03:15 -04:00
|
|
|
statusCode: 201,
|
2021-11-19 12:04:04 -05:00
|
|
|
body: {
|
2022-04-15 17:03:15 -04:00
|
|
|
url: imageUrl
|
2021-11-19 12:04:04 -05:00
|
|
|
}
|
2020-12-16 17:07:09 -05:00
|
|
|
}
|
2021-11-19 12:04:04 -05:00
|
|
|
)
|
2020-12-16 17:07:09 -05:00
|
|
|
})
|
|
|
|
it('via button', () => {
|
2022-06-07 19:10:49 -04:00
|
|
|
cy.getByCypressId('editor-pane').should('have.attr', 'data-cypress-editor-ready', 'true')
|
2021-12-25 10:44:24 -05:00
|
|
|
cy.getByCypressId('editor-toolbar-upload-image-button').should('be.visible')
|
2022-02-05 13:32:58 -05:00
|
|
|
cy.getByCypressId('editor-toolbar-upload-image-input').selectFile(
|
|
|
|
{
|
|
|
|
contents: '@demoImage',
|
|
|
|
fileName: 'demo.png',
|
|
|
|
mimeType: 'image/png'
|
|
|
|
},
|
|
|
|
{ force: true }
|
|
|
|
)
|
2022-06-07 19:10:49 -04:00
|
|
|
cy.get('.cm-line').contains(`![demo.png](${imageUrl})`)
|
2020-12-16 17:07:09 -05:00
|
|
|
})
|
|
|
|
|
|
|
|
it('via paste', () => {
|
2022-06-07 19:10:49 -04:00
|
|
|
cy.getByCypressId('editor-pane').should('have.attr', 'data-cypress-editor-ready', 'true')
|
2021-11-19 12:04:04 -05:00
|
|
|
cy.fixture('demo.png').then((image: string) => {
|
|
|
|
const pasteEvent = {
|
|
|
|
clipboardData: {
|
|
|
|
files: [Cypress.Blob.base64StringToBlob(image, 'image/png')],
|
2022-06-07 19:10:49 -04:00
|
|
|
getData: () => ''
|
2021-01-06 07:09:33 -05:00
|
|
|
}
|
2021-11-19 12:04:04 -05:00
|
|
|
}
|
2022-02-13 06:14:01 -05:00
|
|
|
cy.get('.cm-content').trigger('paste', pasteEvent)
|
|
|
|
cy.get('.cm-line').contains(`![](${imageUrl})`)
|
2021-11-19 12:04:04 -05:00
|
|
|
})
|
2020-12-16 17:07:09 -05:00
|
|
|
})
|
|
|
|
|
|
|
|
it('via drag and drop', () => {
|
2022-06-07 19:10:49 -04:00
|
|
|
cy.getByCypressId('editor-pane').should('have.attr', 'data-cypress-editor-ready', 'true')
|
2022-02-13 06:14:01 -05:00
|
|
|
cy.get('.cm-content').selectFile(
|
2022-02-05 13:32:58 -05:00
|
|
|
{
|
|
|
|
contents: '@demoImage',
|
|
|
|
fileName: 'demo.png',
|
|
|
|
mimeType: 'image/png'
|
|
|
|
},
|
|
|
|
{ action: 'drag-drop', force: true }
|
|
|
|
)
|
2022-06-07 19:10:49 -04:00
|
|
|
cy.get('.cm-line').contains(`![demo.png](${imageUrl})`)
|
2020-12-16 17:07:09 -05:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2021-11-19 12:04:04 -05:00
|
|
|
it('fails', () => {
|
2022-06-07 19:10:49 -04:00
|
|
|
cy.getByCypressId('editor-pane').should('have.attr', 'data-cypress-editor-ready', 'true')
|
2021-11-19 12:04:04 -05:00
|
|
|
cy.intercept(
|
|
|
|
{
|
2022-04-15 17:03:15 -04:00
|
|
|
method: 'POST',
|
|
|
|
url: '/api/mock-backend/private/media'
|
2021-11-19 12:04:04 -05:00
|
|
|
},
|
|
|
|
{
|
|
|
|
statusCode: 400
|
|
|
|
}
|
|
|
|
)
|
2021-12-25 10:44:24 -05:00
|
|
|
cy.getByCypressId('editor-toolbar-upload-image-button').should('be.visible')
|
2022-02-05 13:32:58 -05:00
|
|
|
cy.getByCypressId('editor-toolbar-upload-image-input').selectFile(
|
|
|
|
{
|
|
|
|
contents: '@demoImage',
|
|
|
|
fileName: 'demo.png',
|
|
|
|
mimeType: 'image/png'
|
|
|
|
},
|
|
|
|
{ force: true }
|
|
|
|
)
|
2022-02-13 06:14:01 -05:00
|
|
|
cy.get('.cm-line').contains('![upload of demo.png failed]()')
|
2020-12-16 17:07:09 -05:00
|
|
|
})
|
2020-12-21 05:58:43 -05:00
|
|
|
|
2021-11-19 12:04:04 -05:00
|
|
|
it('lets text paste still work', () => {
|
2022-06-07 19:10:49 -04:00
|
|
|
cy.getByCypressId('editor-pane').should('have.attr', 'data-cypress-editor-ready', 'true')
|
2020-12-21 05:58:43 -05:00
|
|
|
const testText = 'a long test text'
|
2022-06-07 19:10:49 -04:00
|
|
|
|
|
|
|
const pasteEvent: Event = Object.assign(new Event('paste', { bubbles: true, cancelable: true }), {
|
2020-12-21 05:58:43 -05:00
|
|
|
clipboardData: {
|
2022-06-07 19:10:49 -04:00
|
|
|
files: [],
|
|
|
|
getData: () => testText
|
2020-12-21 05:58:43 -05:00
|
|
|
}
|
2022-06-07 19:10:49 -04:00
|
|
|
})
|
|
|
|
|
2022-02-13 06:14:01 -05:00
|
|
|
cy.get('.cm-content').trigger('paste', pasteEvent)
|
|
|
|
cy.get('.cm-line').contains(`${testText}`)
|
2020-12-21 05:58:43 -05:00
|
|
|
})
|
2020-12-16 17:07:09 -05:00
|
|
|
})
|