Skip to content

Commit

Permalink
fix: 1 test
Browse files Browse the repository at this point in the history
  • Loading branch information
GeekaN2 committed Jul 4, 2024
1 parent 7969916 commit 60a0c18
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 21 deletions.
2 changes: 0 additions & 2 deletions src/presentation/http/router/dto/AddEditorTool.dto.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import EditorTool from '@domain/entities/editorTools.js';
import type { MultipartFields, MultipartFile, MultipartValue } from '@fastify/multipart';
import { Multipart } from '@fastify/multipart';

export interface AddEditorToolDto extends MultipartFields {
name: MultipartValue;
Expand Down
22 changes: 20 additions & 2 deletions src/presentation/http/router/editorTools.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,28 @@ describe('EditorTools API', () => {
title: 'Code Tool',
exportName: 'Code',
isDefault: false,
description: '',
source: {
cdn: 'https://cdn.jsdelivr.net/npm/@editorjs/code@latest',
},
};

const formData = new FormData();

formData.append('name', toolToAdd.name);
formData.append('title', toolToAdd.title);
formData.append('exportName', toolToAdd.exportName);
formData.append('isDefault', String(toolToAdd.isDefault));
formData.append('description', toolToAdd.description);
formData.append('source', JSON.stringify(toolToAdd.source));

const addToolResponse = await global.api?.fakeRequest({
method: 'POST',
headers: {
authorization: `Bearer ${accessToken}`,
},
url: '/editor-tools/add-tool',
body: toolToAdd,
body: formData,
});

expect(addToolResponse?.statusCode).toBe(200);

Check failure on line 46 in src/presentation/http/router/editorTools.test.ts

View workflow job for this annotation

GitHub Actions / tests (feat/descriptions-and-covers)

src/presentation/http/router/editorTools.test.ts > EditorTools API > POST /editor-tools/add-tool > Returns added tool with status code 200 if tool added to all tools

AssertionError: expected 500 to be 200 // Object.is equality - Expected + Received - 200 + 500 ❯ src/presentation/http/router/editorTools.test.ts:46:43
Expand All @@ -39,6 +49,7 @@ describe('EditorTools API', () => {

expect(body.data).toMatchObject({
...toolToAdd,
cover: '',
userId,
});

Expand Down Expand Up @@ -68,13 +79,20 @@ describe('EditorTools API', () => {
},
};

const formData = new FormData();

formData.append('title', toolDataWithoutName.title);
formData.append('exportName', toolDataWithoutName.exportName);
formData.append('isDefault', String(toolDataWithoutName.isDefault));
formData.append('source', JSON.stringify(toolDataWithoutName.source));

const response = await global.api?.fakeRequest({
method: 'POST',
headers: {
authorization: `Bearer ${accessToken}`,
},
url: '/editor-tools/add-tool',
body: toolDataWithoutName,
body: formData,
});

expect(response?.statusCode).toBe(400);

Check failure on line 98 in src/presentation/http/router/editorTools.test.ts

View workflow job for this annotation

GitHub Actions / tests (feat/descriptions-and-covers)

src/presentation/http/router/editorTools.test.ts > EditorTools API > POST /editor-tools/add-tool > Returns 400 if tool data is invalid

AssertionError: expected 500 to be 400 // Object.is equality - Expected + Received - 400 + 500 ❯ src/presentation/http/router/editorTools.test.ts:98:36
Expand Down
23 changes: 10 additions & 13 deletions src/presentation/http/router/editorTools.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import type { FastifyPluginCallback } from 'fastify';
import type EditorToolsService from '@domain/service/editorTools.js';
import type EditorTool from '@domain/entities/editorTools.js';
import type { AddEditorToolDto } from './dto/AddEditorTool.dto.js';
import type FileUploaderService from '@domain/service/fileUploader.service.js';
import fastifyMultipart from '@fastify/multipart';
Expand Down Expand Up @@ -116,19 +115,17 @@ const EditorToolsRouter: FastifyPluginCallback<EditorToolsRouterOptions> = async

let coverKey: string | undefined = undefined;

if (editorTool.cover) {
const coverBuffer = await editorTool.cover.toBuffer();
const coverBuffer = await editorTool.cover.toBuffer();

coverKey = await fileUploaderService.uploadFile({
data: coverBuffer,
name: createFileId(),
mimetype: editorTool.cover.mimetype,
}, {
isEditorToolCover: true,
}, {
userId,
});
}
coverKey = await fileUploaderService.uploadFile({
data: coverBuffer,
name: createFileId(),
mimetype: editorTool.cover.mimetype,
}, {
isEditorToolCover: true,
}, {
userId,
});

const tool = await editorToolsService.addTool({
title: String(editorTool.title?.value),
Expand Down
3 changes: 3 additions & 0 deletions src/presentation/http/router/user.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ describe('User API', () => {
exportName: 'Code',
userId: null,
isDefault: false,
description: 'Code tool for Editor.js',
source: {
cdn: 'https://cdn.jsdelivr.net/npm/@editorjs/code@latest',
},
Expand All @@ -85,6 +86,8 @@ describe('User API', () => {
name: 'code',
title: 'Code Tool',
exportName: 'Code',
description: 'Code tool for Editor.js',
cover: '',
userId: null,
isDefault: false,
source: {
Expand Down
4 changes: 2 additions & 2 deletions src/repository/editorTools.repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,8 @@ export default class EditorToolsRepository {

/**
* Update tool cover
* @param editorToolId
* @param cover
* @param editorToolId - unique tool identifier
* @param cover - new tool cover
*/
public async updateToolCover(editorToolId: EditorTool['id'], cover: EditorTool['cover']): Promise<void> {
return await this.storage.updateToolCover(editorToolId, cover);
Expand Down
4 changes: 2 additions & 2 deletions src/tests/utils/database-helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -246,8 +246,8 @@ export default class DatabaseHelpers {
public async insertEditorTool(editorTool: EditorToolMockCreationAttributes): Promise<EditorTool['id']> {
const isDefault = editorTool.isDefault ?? false;

const [result, _] = await this.orm.connection.query(`INSERT INTO public.editor_tools ("name", "title", "export_name", "source", "is_default")
VALUES ('${editorTool.name}', '${editorTool.title}', '${editorTool.exportName}', '${JSON.stringify(editorTool.source)}', ${isDefault})
const [result, _] = await this.orm.connection.query(`INSERT INTO public.editor_tools ("name", "title", "export_name", "source", "is_default", "description")
VALUES ('${editorTool.name}', '${editorTool.title}', '${editorTool.exportName}', '${JSON.stringify(editorTool.source)}', ${isDefault}, '${editorTool.description}')
RETURNING "id"`);

const addedToolData = result[0];
Expand Down

0 comments on commit 60a0c18

Please sign in to comment.