Add mock for upload response

Signed-off-by: Tilman Vatteroth <git@tilmanvatteroth.de>
This commit is contained in:
Tilman Vatteroth 2021-06-09 21:54:12 +02:00
parent 5933fb801d
commit ed9f4aefab
4 changed files with 14 additions and 11 deletions

View file

@ -31,7 +31,7 @@ describe('File upload', () => {
beforeEach(() => {
cy.intercept({
method: 'POST',
url: '/mock-backend/api/private/media/upload'
url: '/mock-backend/api/private/media/upload-post'
}, {
statusCode: 201,
body: {
@ -86,7 +86,7 @@ describe('File upload', () => {
it('upload fails', () => {
cy.intercept({
method: 'POST',
url: '/mock-backend/api/private/media/upload'
url: '/mock-backend/api/private/media/upload-post'
}, {
statusCode: 400
})

View file

@ -0,0 +1,3 @@
{
"link": "/mock-backend/public/img/avatar.png"
}

View file

@ -5,6 +5,7 @@
*/
import { ImageProxyResponse } from '../../components/markdown-renderer/replace-components/image/types'
import { isMockMode } from '../../utils/test-modes'
import { defaultFetchConfig, expectResponseCode, getApiUrl } from '../utils'
export const getProxiedUrl = async (imageUrl: string): Promise<ImageProxyResponse> => {
@ -23,16 +24,16 @@ export interface UploadedMedia {
link: string
}
export const uploadFile = async (noteId: string, contentType: string, media: Blob): Promise<UploadedMedia> => {
const response = await fetch(getApiUrl() + 'media/upload', {
export const uploadFile = async (noteId: string, media: Blob): Promise<UploadedMedia> => {
const response = await fetch(`${getApiUrl()}media/upload${isMockMode() ? '-post' : ''}`, {
...defaultFetchConfig,
headers: {
'Content-Type': contentType,
'Content-Type': media.type,
'HedgeDoc-Note': noteId
},
method: 'POST',
body: media
method: isMockMode() ? 'GET' : 'POST',
body: isMockMode() ? undefined : media
})
expectResponseCode(response, 201)
expectResponseCode(response, isMockMode() ? 200 : 201)
return (await response.json()) as Promise<UploadedMedia>
}

View file

@ -14,8 +14,7 @@ export const handleUpload = (file: File, editor: Editor): void => {
if (!file) {
return
}
const mimeType = file.type
if (!supportedMimeTypes.includes(mimeType)) {
if (!supportedMimeTypes.includes(file.type)) {
// this mimetype is not supported
return
}
@ -23,7 +22,7 @@ export const handleUpload = (file: File, editor: Editor): void => {
const uploadPlaceholder = `![${i18n.t('editor.upload.uploadFile', { fileName: file.name })}]()`
const noteId = store.getState().noteDetails.id
editor.replaceRange(uploadPlaceholder, cursor, cursor, '+input')
uploadFile(noteId, mimeType, file)
uploadFile(noteId, file)
.then(({ link }) => {
editor.replaceRange(
getCorrectSyntaxForLink(mimeType, link),