|
1 | 1 | import { MemberRole } from '@domain/entities/team.js'; |
2 | 2 | import { describe, test, expect, beforeEach } from 'vitest'; |
3 | 3 | import type User from '@domain/entities/user.js'; |
| 4 | +import type { Note } from '@domain/entities/note.js'; |
4 | 5 |
|
5 | 6 | describe('Note API', () => { |
6 | 7 | /** |
@@ -2284,64 +2285,85 @@ describe('Note API', () => { |
2284 | 2285 | accessToken = global.auth(user.id); |
2285 | 2286 | }); |
2286 | 2287 |
|
2287 | | - test('Get note hierarchy with no parent or child', async () => { |
2288 | | - /** |
2289 | | - * Insert test note, note history record will be inserted automatically |
2290 | | - */ |
2291 | | - const note = await global.db.insertNote({ |
2292 | | - creatorId: user.id, |
2293 | | - }); |
2294 | | - |
2295 | | - const response = await global.api?.fakeRequest({ |
2296 | | - method: 'GET', |
2297 | | - headers: { |
2298 | | - authorization: `Bearer ${accessToken}`, |
2299 | | - }, |
2300 | | - url: `/note/note-hierarchy/${note.publicId}`, |
2301 | | - }); |
2302 | | - |
2303 | | - expect(response?.json().notehierarchy.id).toBe(note.publicId); |
2304 | | - expect(response?.json().notehierarchy.childNotes).toHaveLength(0); |
2305 | | - }); |
| 2288 | + test.each([ |
| 2289 | + // Test case 1: No parent or child |
| 2290 | + { |
| 2291 | + description: 'Get note hierarchy with no parent or child', |
| 2292 | + setup: async () => { |
| 2293 | + const note = await global.db.insertNote({ creatorId: user.id }); |
2306 | 2294 |
|
2307 | | - test('Get note hierarchy with child', async () => { |
2308 | | - /* create test child note */ |
2309 | | - const childNote = await global.db.insertNote({ |
2310 | | - creatorId: user.id, |
2311 | | - }); |
| 2295 | + await global.db.insertNoteSetting({ |
| 2296 | + noteId: note.id, |
| 2297 | + isPublic: true, |
| 2298 | + }); |
2312 | 2299 |
|
2313 | | - /* create test parent note */ |
2314 | | - const parentNote = await global.db.insertNote({ |
2315 | | - creatorId: user.id, |
2316 | | - }); |
| 2300 | + return { |
| 2301 | + note: note, |
| 2302 | + childNote: null, |
| 2303 | + }; |
| 2304 | + }, |
2317 | 2305 |
|
2318 | | - /* create note settings for child note */ |
2319 | | - await global.db.insertNoteSetting({ |
2320 | | - noteId: childNote.id, |
2321 | | - isPublic: true, |
2322 | | - }); |
| 2306 | + expected: (note: Note, childNote: Note | null) => ({ |
| 2307 | + id: note.publicId, |
| 2308 | + content: note.content, |
| 2309 | + childNotes: childNote, |
| 2310 | + }), |
| 2311 | + }, |
2323 | 2312 |
|
2324 | | - /* create test relation */ |
2325 | | - await global.db.insertNoteRelation({ |
2326 | | - noteId: childNote.id, |
2327 | | - parentId: parentNote.id, |
2328 | | - }); |
| 2313 | + // Test case 2: With child |
| 2314 | + { |
| 2315 | + description: 'Get note hierarchy with child', |
| 2316 | + setup: async () => { |
| 2317 | + const childNote = await global.db.insertNote({ creatorId: user.id }); |
| 2318 | + const parentNote = await global.db.insertNote({ creatorId: user.id }); |
| 2319 | + |
| 2320 | + await global.db.insertNoteSetting({ |
| 2321 | + noteId: childNote.id, |
| 2322 | + isPublic: true, |
| 2323 | + }); |
| 2324 | + await global.db.insertNoteSetting({ |
| 2325 | + noteId: parentNote.id, |
| 2326 | + isPublic: true, |
| 2327 | + }); |
| 2328 | + await global.db.insertNoteRelation({ |
| 2329 | + noteId: childNote.id, |
| 2330 | + parentId: parentNote.id, |
| 2331 | + }); |
| 2332 | + |
| 2333 | + return { |
| 2334 | + note: parentNote, |
| 2335 | + childNote: childNote, |
| 2336 | + }; |
| 2337 | + }, |
| 2338 | + expected: (note: Note, childNote: Note | null) => ({ |
| 2339 | + id: note.publicId, |
| 2340 | + content: note.content, |
| 2341 | + childNotes: [ |
| 2342 | + { |
| 2343 | + id: childNote?.publicId, |
| 2344 | + content: childNote?.content, |
| 2345 | + childNotes: null, |
| 2346 | + }, |
| 2347 | + ], |
| 2348 | + }), |
| 2349 | + }, |
| 2350 | + ])('$description', async ({ setup, expected }) => { |
| 2351 | + // Setup the test data |
| 2352 | + const { note, childNote } = await setup(); |
2329 | 2353 |
|
| 2354 | + // Make the API request |
2330 | 2355 | const response = await global.api?.fakeRequest({ |
2331 | 2356 | method: 'GET', |
2332 | 2357 | headers: { |
2333 | 2358 | authorization: `Bearer ${accessToken}`, |
2334 | 2359 | }, |
2335 | | - url: `/note/note-hierarchy/${parentNote.publicId}`, |
| 2360 | + url: `/note/note-hierarchy/${note.publicId}`, |
2336 | 2361 | }); |
2337 | 2362 |
|
2338 | | - const childNoteObj = { |
2339 | | - id: childNote.publicId, |
2340 | | - content: childNote.content, |
2341 | | - childNotes: [], |
2342 | | - }; |
2343 | | - |
2344 | | - expect(response?.json().notehierarchy.childNotes[0]).toStrictEqual(childNoteObj); |
| 2363 | + // Verify the response |
| 2364 | + expect(response?.json().noteHierarchy).toStrictEqual( |
| 2365 | + expected(note, childNote) |
| 2366 | + ); |
2345 | 2367 | }); |
2346 | 2368 | }); |
2347 | 2369 | }); |
0 commit comments