import { Middleware } from '@/application/middlewares' ; import { adaptExpressMiddleware } from '@/main/adapters' ; import { getMockReq , getMockRes } from '@jest-mock/express' ; import { NextFunction , Request , Response , RequestHandler } from 'express' ; import { mock , MockProxy } from 'jest-mock-extended' ; describe ( 'ExpressMidleware' , ( ) => { let req ; let res ; let next ; let middleware ; let sut ; beforeAll ( ( ) => { req = getMockReq ( { headers : { any : 'any' } } ) ; res = getMockRes ( ) . res ; next = getMockRes ( ) . next ; middleware = mock < Middleware > ( ) ; middleware . handle . mockResolvedValue ( { statusCode : 200 , data : { emptyProp : '' , nullProp : null , undefinedProp : undefined , prop : 'any_value' } } ) ; } ) ; beforeEach ( ( ) => { sut = adaptExpressMiddleware ( middleware ) ; } ) ; it ( 'should call handle with correct request' , async ( ) => { await sut ( req , res , next ) ; expect ( middleware . handle ) . toHaveBeenCalledWith ( { any : 'any' } ) ; expect ( middleware . handle ) . toHaveBeenCalledTimes ( 1 ) ; } ) ; it ( 'should call handle with empty request' , async ( ) => { req = getMockReq ( ) ; await sut ( req , res , next ) ; expect ( middleware . handle ) . toHaveBeenCalledWith ( { } ) ; expect ( middleware . handle ) . toHaveBeenCalledTimes ( 1 ) ; } ) ; it ( 'should respond with correct error and statusCode' , async ( ) => { middleware . handle . mockResolvedValueOnce ( { statusCode : 500 , data : new Error ( 'any_error' ) } ) ; await sut ( req , res , next ) ; expect ( res . status ) . toHaveBeenCalledWith ( 500 ) ; expect ( res . status ) . toHaveBeenCalledTimes ( 1 ) ; expect ( res . json ) . toHaveBeenCalledWith ( { error : 'any_error' } ) ; expect ( res . json ) . toHaveBeenCalledTimes ( 1 ) ; } ) ; it ( 'should add valid data to req.locals' , async ( ) => { await sut ( req , res , next ) ; expect ( req . locals ) . toEqual ( { prop : 'any_value' } ) ; expect ( next ) . toHaveBeenCalledTimes ( 1 ) ; } ) ; } ) ;