@@ -5,14 +5,40 @@ import request from 'supertest';
55
66describe ( 'createObjectQLRouter' , ( ) => {
77 let mockObjectQL : any ;
8+ let mockRepo : any ;
9+ let mockGetContext : jest . Mock ;
810 let app : express . Express ;
911
1012 beforeEach ( ( ) => {
13+ // Mock Repository methods
14+ mockRepo = {
15+ find : jest . fn ( ) . mockResolvedValue ( [ { id : 1 , name : 'Test' } ] ) ,
16+ findOne : jest . fn ( ) . mockResolvedValue ( { id : 1 , name : 'Test' } ) ,
17+ count : jest . fn ( ) . mockResolvedValue ( 10 ) ,
18+ aggregate : jest . fn ( ) . mockResolvedValue ( [ ] ) ,
19+ create : jest . fn ( ) . mockResolvedValue ( { id : 2 , name : 'New' } ) ,
20+ createMany : jest . fn ( ) . mockResolvedValue ( [ { id : 2 , name : 'New' } ] ) ,
21+ update : jest . fn ( ) . mockResolvedValue ( { id : 1 , name : 'Updated' } ) ,
22+ delete : jest . fn ( ) . mockResolvedValue ( undefined ) ,
23+ deleteMany : jest . fn ( ) . mockResolvedValue ( { deletedCount : 5 } ) ,
24+ call : jest . fn ( ) . mockResolvedValue ( { result : 'ok' } )
25+ } ;
26+
1127 mockObjectQL = {
1228 init : jest . fn ( ) ,
13- getConfigs : jest . fn ( ) . mockReturnValue ( [ ] ) // Add this
29+ getConfigs : jest . fn ( ) . mockReturnValue ( [ { name : 'user' } ] )
1430 } ;
31+
32+ // Mock getContext to return a context that returns our mockRepo
33+ mockGetContext = jest . fn ( ) . mockResolvedValue ( {
34+ object : jest . fn ( ) . mockReturnValue ( mockRepo ) ,
35+ roles : [ ] ,
36+ transaction : jest . fn ( ) ,
37+ sudo : jest . fn ( )
38+ } ) ;
39+
1540 app = express ( ) ;
41+ app . use ( express . json ( ) ) ; // Important for POST/PUT tests
1642 } ) ;
1743
1844 it ( 'should create a router' , ( ) => {
@@ -28,16 +54,97 @@ describe('createObjectQLRouter', () => {
2854 } ) ;
2955
3056 app . use ( router ) ;
31-
32- // Assuming swagger-ui-express serves HTML at /docs
33- // Since we didn't mock swagger-ui-express, it might try to check real files or just work if deps are there.
34- // But in unit test environment we might want to just check if it doesn't crash.
35- // Better: mock swagger-ui-express or check console.log side effect?
36- // Let's just check if the function ran without error.
3757 expect ( router ) . toBeDefined ( ) ;
3858 } ) ;
39-
40- // Future: Add integration tests simulating requests
41- // app.use('/api', router);
42- // await request(app).get('/api/users')...
59+
60+ describe ( 'REST API Endpoints' , ( ) => {
61+ beforeEach ( ( ) => {
62+ const router = createObjectQLRouter ( {
63+ objectql : mockObjectQL ,
64+ getContext : mockGetContext
65+ } ) ;
66+ app . use ( '/api' , router ) ;
67+ } ) ;
68+
69+ it ( 'GET /_schema should return configs' , async ( ) => {
70+ const res = await request ( app ) . get ( '/api/_schema' ) ;
71+ expect ( res . status ) . toBe ( 200 ) ;
72+ expect ( res . body ) . toEqual ( [ { name : 'user' } ] ) ;
73+ expect ( mockObjectQL . getConfigs ) . toHaveBeenCalled ( ) ;
74+ } ) ;
75+
76+ it ( 'GET /:objectName/count should return count' , async ( ) => {
77+ const res = await request ( app )
78+ . get ( '/api/users/count' )
79+ . query ( { filters : JSON . stringify ( [ [ 'age' , '>' , 20 ] ] ) } ) ;
80+
81+ expect ( res . status ) . toBe ( 200 ) ;
82+ expect ( res . body ) . toEqual ( { count : 10 } ) ;
83+ expect ( mockRepo . count ) . toHaveBeenCalledWith ( [ [ 'age' , '>' , 20 ] ] ) ;
84+ } ) ;
85+
86+ it ( 'GET /:objectName should list objects' , async ( ) => {
87+ const res = await request ( app )
88+ . get ( '/api/users' )
89+ . query ( {
90+ limit : 10 ,
91+ skip : 0 ,
92+ sort : 'name:asc' ,
93+ fields : 'id,name'
94+ } ) ;
95+
96+ expect ( res . status ) . toBe ( 200 ) ;
97+ expect ( res . body ) . toHaveLength ( 1 ) ;
98+ expect ( mockRepo . find ) . toHaveBeenCalledWith ( expect . objectContaining ( {
99+ limit : 10 ,
100+ skip : 0 ,
101+ // sort parsing: 'name:asc' -> [['name', 'asc']]
102+ sort : [ [ 'name' , 'asc' ] ] ,
103+ fields : [ 'id' , 'name' ]
104+ } ) ) ;
105+ } ) ;
106+
107+ it ( 'GET /:objectName/:id should get one object' , async ( ) => {
108+ const res = await request ( app ) . get ( '/api/users/1' ) ;
109+ expect ( res . status ) . toBe ( 200 ) ;
110+ expect ( res . body ) . toEqual ( { id : 1 , name : 'Test' } ) ;
111+ expect ( mockRepo . findOne ) . toHaveBeenCalledWith ( '1' ) ;
112+ } ) ;
113+
114+ it ( 'POST /:objectName should create object' , async ( ) => {
115+ const res = await request ( app )
116+ . post ( '/api/users' )
117+ . send ( { name : 'New' } ) ;
118+
119+ expect ( res . status ) . toBe ( 201 ) ;
120+ expect ( res . body ) . toEqual ( { id : 2 , name : 'New' } ) ;
121+ expect ( mockRepo . create ) . toHaveBeenCalledWith ( { name : 'New' } ) ;
122+ } ) ;
123+
124+ it ( 'PUT /:objectName/:id should update object' , async ( ) => {
125+ const res = await request ( app )
126+ . put ( '/api/users/1' )
127+ . send ( { name : 'Updated' } ) ;
128+
129+ expect ( res . status ) . toBe ( 200 ) ;
130+ expect ( res . body ) . toEqual ( { id : 1 , name : 'Updated' } ) ;
131+ expect ( mockRepo . update ) . toHaveBeenCalledWith ( '1' , { name : 'Updated' } ) ;
132+ } ) ;
133+
134+ it ( 'DELETE /:objectName/:id should delete object' , async ( ) => {
135+ const res = await request ( app ) . delete ( '/api/users/1' ) ;
136+ expect ( res . status ) . toBe ( 204 ) ;
137+ expect ( mockRepo . delete ) . toHaveBeenCalledWith ( '1' ) ;
138+ } ) ;
139+
140+ it ( 'POST /:objectName/:id/:actionName should execute action' , async ( ) => {
141+ const res = await request ( app )
142+ . post ( '/api/users/1/activate' )
143+ . send ( { reason : 'testing' } ) ;
144+
145+ expect ( res . status ) . toBe ( 200 ) ;
146+ expect ( res . body ) . toEqual ( { result : 'ok' } ) ;
147+ expect ( mockRepo . call ) . toHaveBeenCalledWith ( 'activate' , { id : '1' , reason : 'testing' } ) ;
148+ } ) ;
149+ } ) ;
43150} ) ;
0 commit comments