1+ import express from 'express' ;
2+ import request from 'supertest' ;
3+ import { createNotesRouter } from '../../src/routes/notes-routes.js' ;
4+ import { Note } from '../../src/models/note.js' ;
5+
6+ // Mock repository for testing
7+ class MockNoteRepository {
8+ constructor ( ) {
9+ this . notes = [ ] ;
10+ this . nextId = 1 ;
11+ }
12+
13+ async findAll ( ) {
14+ return [ ...this . notes ] ;
15+ }
16+
17+ async findById ( id ) {
18+ const note = this . notes . find ( n => n . id === id ) ;
19+ return note || null ;
20+ }
21+
22+ async create ( noteData ) {
23+ const id = String ( this . nextId ++ ) ;
24+ const note = new Note (
25+ id ,
26+ noteData . title ,
27+ noteData . content ,
28+ new Date ( ) ,
29+ new Date ( )
30+ ) ;
31+ this . notes . push ( note ) ;
32+ return note ;
33+ }
34+
35+ async update ( id , noteData ) {
36+ const index = this . notes . findIndex ( n => n . id === id ) ;
37+ if ( index === - 1 ) return null ;
38+
39+ const note = this . notes [ index ] ;
40+ note . title = noteData . title ;
41+ note . content = noteData . content ;
42+ note . updatedAt = new Date ( ) ;
43+
44+ return note ;
45+ }
46+
47+ async delete ( id ) {
48+ const index = this . notes . findIndex ( n => n . id === id ) ;
49+ if ( index === - 1 ) return false ;
50+
51+ this . notes . splice ( index , 1 ) ;
52+ return true ;
53+ }
54+ }
55+
56+ describe ( 'Notes Routes' , ( ) => {
57+ let app ;
58+ let repository ;
59+
60+ beforeEach ( ( ) => {
61+ repository = new MockNoteRepository ( ) ;
62+ app = express ( ) ;
63+ app . use ( express . json ( ) ) ;
64+ app . use ( '/api/notes' , createNotesRouter ( repository ) ) ;
65+ } ) ;
66+
67+ describe ( 'GET /api/notes' , ( ) => {
68+ test ( 'should return empty array when no notes exist' , async ( ) => {
69+ const response = await request ( app ) . get ( '/api/notes' ) ;
70+
71+ expect ( response . status ) . toBe ( 200 ) ;
72+ expect ( response . body ) . toEqual ( [ ] ) ;
73+ } ) ;
74+
75+ test ( 'should return all notes' , async ( ) => {
76+ // Add some test notes
77+ await repository . create ( { title : 'Note 1' , content : 'Content 1' } ) ;
78+ await repository . create ( { title : 'Note 2' , content : 'Content 2' } ) ;
79+
80+ const response = await request ( app ) . get ( '/api/notes' ) ;
81+
82+ expect ( response . status ) . toBe ( 200 ) ;
83+ expect ( response . body . length ) . toBe ( 2 ) ;
84+ expect ( response . body [ 0 ] . title ) . toBe ( 'Note 1' ) ;
85+ expect ( response . body [ 1 ] . title ) . toBe ( 'Note 2' ) ;
86+ } ) ;
87+ } ) ;
88+
89+ describe ( 'POST /api/notes' , ( ) => {
90+ test ( 'should create a new note' , async ( ) => {
91+ const noteData = { title : 'New Note' , content : 'New Content' } ;
92+
93+ const response = await request ( app )
94+ . post ( '/api/notes' )
95+ . send ( noteData )
96+ . set ( 'Content-Type' , 'application/json' ) ;
97+
98+ expect ( response . status ) . toBe ( 201 ) ;
99+ expect ( response . body . title ) . toBe ( noteData . title ) ;
100+ expect ( response . body . content ) . toBe ( noteData . content ) ;
101+ expect ( response . body . id ) . toBeDefined ( ) ;
102+ } ) ;
103+
104+ test ( 'should return 400 if title or content is missing' , async ( ) => {
105+ const response = await request ( app )
106+ . post ( '/api/notes' )
107+ . send ( { title : 'Missing Content' } )
108+ . set ( 'Content-Type' , 'application/json' ) ;
109+
110+ expect ( response . status ) . toBe ( 400 ) ;
111+ expect ( response . body . error ) . toBeDefined ( ) ;
112+ } ) ;
113+ } ) ;
114+ } ) ;
0 commit comments