@@ -49,6 +49,13 @@ const MOCK_FIREBASE_USER = buildDecodedToken(
4949
5050type FirebaseRequest = { firebaseUser : DecodedIdToken } ;
5151
52+ type PaginatedLoans = {
53+ data : Loan [ ] ;
54+ total : number ;
55+ page : number ;
56+ limit : number ;
57+ } ;
58+
5259async function buildApp ( ) : Promise < {
5360 app : INestApplication < App > ;
5461 ds : DataSource ;
@@ -302,15 +309,47 @@ describe('LoansModule (e2e)', (): void => {
302309 } ) ;
303310
304311 describe ( 'GET /admin/loans' , ( ) : void => {
305- it ( 'returns all loans without filter' , async ( ) : Promise < void > => {
312+ it ( 'returns a paginated list without filter' , async ( ) : Promise < void > => {
306313 await request ( app . getHttpServer ( ) ) . post ( '/loans' ) . send ( { copyId } ) ;
307314
308315 await request ( app . getHttpServer ( ) )
309316 . get ( '/admin/loans' )
310317 . expect ( StatusCodes . OK )
311318 . expect ( ( res : Response ) => {
312- expect ( Array . isArray ( res . body ) ) . toBe ( true ) ;
313- expect ( ( res . body as Loan [ ] ) . length ) . toBeGreaterThanOrEqual ( 1 ) ;
319+ const body = res . body as PaginatedLoans ;
320+ expect ( Array . isArray ( body . data ) ) . toBe ( true ) ;
321+ expect ( body . data . length ) . toBeGreaterThanOrEqual ( 1 ) ;
322+ expect ( body . total ) . toBeGreaterThanOrEqual ( 1 ) ;
323+ expect ( body . page ) . toBe ( 1 ) ;
324+ expect ( body . limit ) . toBe ( 20 ) ;
325+ } ) ;
326+ } ) ;
327+
328+ it ( 'orders loans by borrowed_at DESC' , async ( ) : Promise < void > => {
329+ await request ( app . getHttpServer ( ) )
330+ . get ( '/admin/loans' )
331+ . expect ( StatusCodes . OK )
332+ . expect ( ( res : Response ) => {
333+ const { data } = res . body as PaginatedLoans ;
334+ const borrowedTimes = data . map ( loan =>
335+ new Date ( loan . borrowed_at ) . getTime ( )
336+ ) ;
337+ const sorted = [ ...borrowedTimes ] . sort ( ( a , b ) => b - a ) ;
338+ expect ( borrowedTimes ) . toEqual ( sorted ) ;
339+ } ) ;
340+ } ) ;
341+
342+ it ( 'applies page and limit' , async ( ) : Promise < void > => {
343+ await request ( app . getHttpServer ( ) ) . post ( '/loans' ) . send ( { copyId } ) ;
344+
345+ await request ( app . getHttpServer ( ) )
346+ . get ( '/admin/loans?page=1&limit=1' )
347+ . expect ( StatusCodes . OK )
348+ . expect ( ( res : Response ) => {
349+ const body = res . body as PaginatedLoans ;
350+ expect ( body . data . length ) . toBe ( 1 ) ;
351+ expect ( body . page ) . toBe ( 1 ) ;
352+ expect ( body . limit ) . toBe ( 1 ) ;
314353 } ) ;
315354 } ) ;
316355
@@ -321,9 +360,9 @@ describe('LoansModule (e2e)', (): void => {
321360 . get ( '/admin/loans?status=active' )
322361 . expect ( StatusCodes . OK )
323362 . expect ( ( res : Response ) => {
324- const body = res . body as Loan [ ] ;
325- expect ( body . length ) . toBeGreaterThanOrEqual ( 1 ) ;
326- body . forEach ( loan => expect ( loan . returned_at ) . toBeNull ( ) ) ;
363+ const { data } = res . body as PaginatedLoans ;
364+ expect ( data . length ) . toBeGreaterThanOrEqual ( 1 ) ;
365+ data . forEach ( loan => expect ( loan . returned_at ) . toBeNull ( ) ) ;
327366 } ) ;
328367 } ) ;
329368
@@ -339,9 +378,9 @@ describe('LoansModule (e2e)', (): void => {
339378 . get ( '/admin/loans?status=returned' )
340379 . expect ( StatusCodes . OK )
341380 . expect ( ( res : Response ) => {
342- const body = res . body as Loan [ ] ;
343- expect ( body . length ) . toBeGreaterThanOrEqual ( 1 ) ;
344- body . forEach ( loan => expectDateTimeString ( loan . returned_at ) ) ;
381+ const { data } = res . body as PaginatedLoans ;
382+ expect ( data . length ) . toBeGreaterThanOrEqual ( 1 ) ;
383+ data . forEach ( loan => expectDateTimeString ( loan . returned_at ) ) ;
345384 } ) ;
346385 } ) ;
347386
@@ -359,9 +398,9 @@ describe('LoansModule (e2e)', (): void => {
359398 . get ( '/admin/loans?status=overdue' )
360399 . expect ( StatusCodes . OK )
361400 . expect ( ( res : Response ) => {
362- const body = res . body as Loan [ ] ;
363- expect ( body . map ( loan => loan . id ) ) . toContain ( created . id ) ;
364- body . forEach ( loan => {
401+ const { data } = res . body as PaginatedLoans ;
402+ expect ( data . map ( loan => loan . id ) ) . toContain ( created . id ) ;
403+ data . forEach ( loan => {
365404 expect ( loan . returned_at ) . toBeNull ( ) ;
366405 expect ( loan . due_date < new Date ( ) . toISOString ( ) . split ( 'T' ) [ 0 ] ) . toBe (
367406 true
0 commit comments