mirror of
https://github.com/hedgedoc/hedgedoc.git
synced 2025-05-15 15:44:45 -04:00
Add mock for upload response
Signed-off-by: Tilman Vatteroth <git@tilmanvatteroth.de>
This commit is contained in:
parent
5933fb801d
commit
ed9f4aefab
4 changed files with 14 additions and 11 deletions
|
@ -31,7 +31,7 @@ describe('File upload', () => {
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
cy.intercept({
|
cy.intercept({
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
url: '/mock-backend/api/private/media/upload'
|
url: '/mock-backend/api/private/media/upload-post'
|
||||||
}, {
|
}, {
|
||||||
statusCode: 201,
|
statusCode: 201,
|
||||||
body: {
|
body: {
|
||||||
|
@ -86,7 +86,7 @@ describe('File upload', () => {
|
||||||
it('upload fails', () => {
|
it('upload fails', () => {
|
||||||
cy.intercept({
|
cy.intercept({
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
url: '/mock-backend/api/private/media/upload'
|
url: '/mock-backend/api/private/media/upload-post'
|
||||||
}, {
|
}, {
|
||||||
statusCode: 400
|
statusCode: 400
|
||||||
})
|
})
|
||||||
|
|
3
public/mock-backend/api/private/media/upload-post
Normal file
3
public/mock-backend/api/private/media/upload-post
Normal file
|
@ -0,0 +1,3 @@
|
||||||
|
{
|
||||||
|
"link": "/mock-backend/public/img/avatar.png"
|
||||||
|
}
|
|
@ -5,6 +5,7 @@
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import { ImageProxyResponse } from '../../components/markdown-renderer/replace-components/image/types'
|
import { ImageProxyResponse } from '../../components/markdown-renderer/replace-components/image/types'
|
||||||
|
import { isMockMode } from '../../utils/test-modes'
|
||||||
import { defaultFetchConfig, expectResponseCode, getApiUrl } from '../utils'
|
import { defaultFetchConfig, expectResponseCode, getApiUrl } from '../utils'
|
||||||
|
|
||||||
export const getProxiedUrl = async (imageUrl: string): Promise<ImageProxyResponse> => {
|
export const getProxiedUrl = async (imageUrl: string): Promise<ImageProxyResponse> => {
|
||||||
|
@ -23,16 +24,16 @@ export interface UploadedMedia {
|
||||||
link: string
|
link: string
|
||||||
}
|
}
|
||||||
|
|
||||||
export const uploadFile = async (noteId: string, contentType: string, media: Blob): Promise<UploadedMedia> => {
|
export const uploadFile = async (noteId: string, media: Blob): Promise<UploadedMedia> => {
|
||||||
const response = await fetch(getApiUrl() + 'media/upload', {
|
const response = await fetch(`${getApiUrl()}media/upload${isMockMode() ? '-post' : ''}`, {
|
||||||
...defaultFetchConfig,
|
...defaultFetchConfig,
|
||||||
headers: {
|
headers: {
|
||||||
'Content-Type': contentType,
|
'Content-Type': media.type,
|
||||||
'HedgeDoc-Note': noteId
|
'HedgeDoc-Note': noteId
|
||||||
},
|
},
|
||||||
method: 'POST',
|
method: isMockMode() ? 'GET' : 'POST',
|
||||||
body: media
|
body: isMockMode() ? undefined : media
|
||||||
})
|
})
|
||||||
expectResponseCode(response, 201)
|
expectResponseCode(response, isMockMode() ? 200 : 201)
|
||||||
return (await response.json()) as Promise<UploadedMedia>
|
return (await response.json()) as Promise<UploadedMedia>
|
||||||
}
|
}
|
||||||
|
|
|
@ -14,8 +14,7 @@ export const handleUpload = (file: File, editor: Editor): void => {
|
||||||
if (!file) {
|
if (!file) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
const mimeType = file.type
|
if (!supportedMimeTypes.includes(file.type)) {
|
||||||
if (!supportedMimeTypes.includes(mimeType)) {
|
|
||||||
// this mimetype is not supported
|
// this mimetype is not supported
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
@ -23,7 +22,7 @@ export const handleUpload = (file: File, editor: Editor): void => {
|
||||||
const uploadPlaceholder = `![${i18n.t('editor.upload.uploadFile', { fileName: file.name })}]()`
|
const uploadPlaceholder = `![${i18n.t('editor.upload.uploadFile', { fileName: file.name })}]()`
|
||||||
const noteId = store.getState().noteDetails.id
|
const noteId = store.getState().noteDetails.id
|
||||||
editor.replaceRange(uploadPlaceholder, cursor, cursor, '+input')
|
editor.replaceRange(uploadPlaceholder, cursor, cursor, '+input')
|
||||||
uploadFile(noteId, mimeType, file)
|
uploadFile(noteId, file)
|
||||||
.then(({ link }) => {
|
.then(({ link }) => {
|
||||||
editor.replaceRange(
|
editor.replaceRange(
|
||||||
getCorrectSyntaxForLink(mimeType, link),
|
getCorrectSyntaxForLink(mimeType, link),
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue