From 3adff283fbed66d021a8d19b506a82e69bc58e74 Mon Sep 17 00:00:00 2001 From: dependentmadani Date: Mon, 9 Sep 2024 18:26:12 +0100 Subject: [PATCH] update(tests): tests are now functional --- src/presentation/http/router/note.test.ts | 40 ++++++++++++++++++++--- src/presentation/http/router/note.ts | 14 ++++++++ 2 files changed, 49 insertions(+), 5 deletions(-) diff --git a/src/presentation/http/router/note.test.ts b/src/presentation/http/router/note.test.ts index 480302a1..c2cd9e90 100644 --- a/src/presentation/http/router/note.test.ts +++ b/src/presentation/http/router/note.test.ts @@ -519,10 +519,13 @@ describe('Note API', () => { expect(response?.json().message).toStrictEqual(expectedMessage); }); - test('Returns one object of note parent structure by note public id with 200 status', async () => { + test('Returns two objects of note parent structure by note public id with 200 status', async () => { /** Create test user */ const user = await global.db.insertUser(); + /** Create acces token for the user */ + const accessToken = global.auth(user.id); + /** Create test note - a parent note */ const parentNote = await global.db.insertNote({ creatorId: user.id, @@ -547,6 +550,9 @@ describe('Note API', () => { const response = await global.api?.fakeRequest({ method: 'GET', + headers: { + authorization: `Bearer ${accessToken}`, + }, url: `/note/${childNote.publicId}`, }); @@ -558,6 +564,10 @@ describe('Note API', () => { noteId: parentNote.publicId, content: parentNote.content, }, + { + noteId: childNote.publicId, + content: childNote.content, + }, ], }); }); @@ -566,6 +576,9 @@ describe('Note API', () => { /** Create test user */ const user = await global.db.insertUser(); + /** Create acces token for the user */ + const accessToken = global.auth(user.id); + /** Create test note - a parent note */ const parentNote = await global.db.insertNote({ creatorId: user.id, @@ -600,6 +613,9 @@ describe('Note API', () => { const response = await global.api?.fakeRequest({ method: 'GET', + headers: { + authorization: `Bearer ${accessToken}`, + }, url: `/note/${grandchildNote.publicId}`, }); @@ -607,22 +623,28 @@ describe('Note API', () => { expect(response?.json()).toMatchObject({ parentStructure: [ + { + noteId: parentNote.publicId, + content: parentNote.content, + }, { noteId: childNote.publicId, content: childNote.content, }, { - noteId: parentNote.publicId, - content: parentNote.content, + noteId: grandchildNote.publicId, + content: grandchildNote.content, }, ], }); }); - test('Returns empty array of note parent structure by note public id with 200 status', async () => { + test('Returns one object in array of note parent structure by note public id with 200 status', async () => { /** Create test user */ const user = await global.db.insertUser(); + /** Create acces token for the user */ + const accessToken = global.auth(user.id); /** Create test note */ const note = await global.db.insertNote({ creatorId: user.id, @@ -636,13 +658,21 @@ describe('Note API', () => { const response = await global.api?.fakeRequest({ method: 'GET', + headers: { + authorization: `Bearer ${accessToken}`, + }, url: `/note/${note.publicId}`, }); expect(response?.statusCode).toBe(200); expect(response?.json()).toMatchObject({ - parentStructure: [], + parentStructure: [ + { + noteId: note.publicId, + content: note.content, + }, + ], }); }); }); diff --git a/src/presentation/http/router/note.ts b/src/presentation/http/router/note.ts index 31ca6824..5207344e 100644 --- a/src/presentation/http/router/note.ts +++ b/src/presentation/http/router/note.ts @@ -124,6 +124,20 @@ const NoteRouter: FastifyPluginCallback = (fastify, opts, don $ref: 'EditorToolSchema', }, }, + parentStructure: { + type: 'array', + items: { + type: 'object', + properties: { + noteId: { + $ref: 'NoteSchema#/properties/id', + }, + content: { + $ref: 'NoteSchema#/properties/content', + }, + }, + }, + }, }, }, },