import type { Request } from 'express' ; import getMockReq from './request' ; describe ( 'getMockReq' , ( ) => { test ( 'accepts no arguments and return default values' , ( ) => { const testReq = getMockReq ( ) ; expect ( testReq ) . toBeDefined ( ) ; expect ( testReq ) . toBeInstanceOf ( Object ) ; expect ( Object . keys ( testReq ) . length ) . toBe ( 49 ) ; expect ( testReq . params ) . toBeDefined ( ) ; expect ( testReq . params ) . toBeInstanceOf ( Object ) ; expect ( Object . keys ( testReq . params ) . length ) . toBe ( 0 ) ; expect ( testReq . query ) . toBeDefined ( ) ; expect ( testReq . query ) . toBeInstanceOf ( Object ) ; expect ( Object . keys ( testReq . query ) . length ) . toBe ( 0 ) ; expect ( testReq . body ) . toBeDefined ( ) ; expect ( testReq . body ) . toBeInstanceOf ( Object ) ; expect ( Object . keys ( testReq . body ) . length ) . toBe ( 0 ) ; expect ( testReq . cookies ) . toBeDefined ( ) ; expect ( testReq . cookies ) . toBeInstanceOf ( Object ) ; expect ( Object . keys ( testReq . cookies ) . length ) . toBe ( 0 ) ; expect ( testReq . method ) . toBe ( '' ) ; expect ( testReq . protocol ) . toBe ( '' ) ; expect ( testReq . secure ) . toBe ( false ) ; expect ( testReq . ip ) . toBe ( '' ) ; expect ( Array . isArray ( testReq . ips ) ) . toBe ( true ) ; expect ( testReq . ips . length ) . toBe ( 0 ) ; expect ( Array . isArray ( testReq . subdomains ) ) . toBe ( true ) ; expect ( testReq . subdomains . length ) . toBe ( 0 ) ; expect ( testReq . path ) . toBe ( '' ) ; expect ( testReq . hostname ) . toBe ( '' ) ; expect ( testReq . host ) . toBe ( '' ) ; expect ( testReq . fresh ) . toBe ( false ) ; expect ( testReq . stale ) . toBe ( false ) ; expect ( testReq . xhr ) . toBe ( false ) ; expect ( testReq . route ) . toBeDefined ( ) ; expect ( testReq . route ) . toBeInstanceOf ( Object ) ; expect ( Object . keys ( testReq . route ) . length ) . toBe ( 0 ) ; expect ( testReq . signedCookies ) . toBeDefined ( ) ; expect ( testReq . signedCookies ) . toBeInstanceOf ( Object ) ; expect ( Object . keys ( testReq . signedCookies ) . length ) . toBe ( 0 ) ; expect ( testReq . originalUrl ) . toBe ( '' ) ; expect ( testReq . url ) . toBe ( '' ) ; expect ( testReq . baseUrl ) . toBe ( '' ) ; expect ( Array . isArray ( testReq . accepted ) ) . toBe ( true ) ; expect ( testReq . accepted . length ) . toBe ( 0 ) ; expect ( ( testReq . get as jest . Mock ) . getMockName ( ) ) . toBe ( 'get mock default' ) ; expect ( ( testReq . header as jest . Mock ) . getMockName ( ) ) . toBe ( 'header mock default' ) ; expect ( ( testReq . accepts as jest . Mock ) . getMockName ( ) ) . toBe ( 'accepts mock default' ) ; expect ( ( testReq . acceptsCharsets as jest . Mock ) . getMockName ( ) ) . toBe ( 'acceptsCharsets mock default' ) ; expect ( ( testReq . acceptsEncodings as jest . Mock ) . getMockName ( ) ) . toBe ( 'acceptsEncodings mock default' ) ; expect ( ( testReq . acceptsLanguages as jest . Mock ) . getMockName ( ) ) . toBe ( 'acceptsLanguages mock default' ) ; expect ( ( testReq . range as jest . Mock ) . getMockName ( ) ) . toBe ( 'range mock default' ) ; expect ( ( testReq . param as jest . Mock ) . getMockName ( ) ) . toBe ( 'param mock default' ) ; expect ( ( testReq . is as jest . Mock ) . getMockName ( ) ) . toBe ( 'is mock default' ) ; expect ( testReq . app ) . toBeDefined ( ) ; expect ( testReq . app ) . toBeInstanceOf ( Object ) ; expect ( Object . keys ( testReq . app ) . length ) . toBe ( 0 ) ; expect ( ( testReq . res as unknown as jest . Mock ) . getMockName ( ) ) . toBe ( 'res mock default' ) ; expect ( ( testReq . next as jest . Mock ) . getMockName ( ) ) . toBe ( 'next mock default' ) ; expect ( testReq . aborted ) . toBe ( false ) ; expect ( testReq . httpVersion ) . toBe ( '' ) ; expect ( testReq . httpVersionMajor ) . toBe ( 0 ) ; expect ( testReq . httpVersionMinor ) . toBe ( 0 ) ; expect ( testReq . complete ) . toBe ( false ) ; expect ( testReq . connection ) . toBeDefined ( ) ; expect ( testReq . connection ) . toBeInstanceOf ( Object ) ; expect ( Object . keys ( testReq . connection ) . length ) . toBe ( 0 ) ; expect ( testReq . socket ) . toBeDefined ( ) ; expect ( testReq . socket ) . toBeInstanceOf ( Object ) ; expect ( Object . keys ( testReq . socket ) . length ) . toBe ( 0 ) ; expect ( testReq . headers ) . toBeDefined ( ) ; expect ( testReq . headers ) . toBeInstanceOf ( Object ) ; expect ( Object . keys ( testReq . headers ) . length ) . toBe ( 0 ) ; expect ( Array . isArray ( testReq . rawHeaders ) ) . toBe ( true ) ; expect ( testReq . rawHeaders . length ) . toBe ( 0 ) ; expect ( testReq . trailers ) . toBeDefined ( ) ; expect ( testReq . trailers ) . toBeInstanceOf ( Object ) ; expect ( Object . keys ( testReq . trailers ) . length ) . toBe ( 0 ) ; expect ( Array . isArray ( testReq . rawTrailers ) ) . toBe ( true ) ; expect ( testReq . rawTrailers . length ) . toBe ( 0 ) ; expect ( ( testReq . setTimeout as jest . Mock ) . getMockName ( ) ) . toBe ( 'setTimeout mock default' ) ; expect ( testReq . statusCode ) . toBe ( 0 ) ; expect ( testReq . statusMessage ) . toBe ( '' ) ; expect ( ( testReq . destroy as jest . Mock ) . getMockName ( ) ) . toBe ( 'destroy mock default' ) ; } ) ; test ( 'returns provided params' , ( ) => { const testReq = getMockReq ( { params : { one : 1 , two : 2 } } ) ; expect ( testReq . params ) . toBeDefined ( ) ; expect ( testReq . params ) . toBeInstanceOf ( Object ) ; expect ( Object . keys ( testReq . params ) . length ) . toBe ( 2 ) ; expect ( testReq . params [ 'one' ] ) . toBe ( 1 ) ; expect ( testReq . params [ 'two' ] ) . toBe ( 2 ) ; } ) ; test ( 'returns provided query' , ( ) => { const testReq = getMockReq ( { query : { three : 'three' , four : 'four' } } ) ; expect ( testReq . query ) . toBeDefined ( ) ; expect ( testReq . query ) . toBeInstanceOf ( Object ) ; expect ( Object . keys ( testReq . query ) . length ) . toBe ( 2 ) ; expect ( testReq . query [ 'three' ] ) . toBe ( 'three' ) ; expect ( testReq . query [ 'four' ] ) . toBe ( 'four' ) ; } ) ; test ( 'returns provided body' , ( ) => { const testReq = getMockReq ( { body : { five : { six : 6 } , seven : [ 8 , 9 ] } } ) ; expect ( testReq . body ) . toBeDefined ( ) ; expect ( testReq . body ) . toBeInstanceOf ( Object ) ; expect ( Object . keys ( testReq . body ) . length ) . toBe ( 2 ) ; expect ( testReq . body [ 'five' ] ) . toEqual ( expect . objectContaining ( { six : 6 } ) ) ; expect ( testReq . body [ 'seven' ] ) . toEqual ( expect . arrayContaining ( [ 8 , 9 ] ) ) ; } ) ; test ( 'returns provided headers' , ( ) => { const testReq = getMockReq ( { headers : { HeaderOne : 'one' , HeaderTwo : 'two' , HeaderThree : 'three' , } , } ) ; expect ( testReq . headers ) . toBeDefined ( ) ; expect ( testReq . headers ) . toBeInstanceOf ( Object ) ; expect ( Object . keys ( testReq . headers ) . length ) . toBe ( 3 ) ; expect ( testReq . headers [ 'HeaderOne' ] ) . toBe ( 'one' ) ; expect ( testReq . headers [ 'HeaderTwo' ] ) . toBe ( 'two' ) ; expect ( testReq . headers [ 'HeaderThree' ] ) . toBe ( 'three' ) ; } ) ; test ( 'allows custom properties' , ( ) => { interface User { id : string ; name : string ; } interface CustomRequest extends Request { user : User ; } const mockUser = { id : '123' , name : 'Bob' , } ; const testReq = getMockReq < CustomRequest > ( { user : mockUser , query : { id : '123' , limit : 10 , page : 2 , } , } ) ; expect ( testReq . user ) . toBeDefined ( ) ; expect ( testReq . user ) . toBe ( mockUser ) ; expect ( testReq . query ) . toBeDefined ( ) ; expect ( testReq . query ) . toBeInstanceOf ( Object ) ; expect ( Object . keys ( testReq . query ) . length ) . toBe ( 3 ) ; expect ( testReq . query [ 'id' ] ) . toBe ( '123' ) ; expect ( testReq . query [ 'limit' ] ) . toBe ( 10 ) ; expect ( testReq . query [ 'page' ] ) . toBe ( 2 ) ; } ) ; test ( 'issue #6' , ( ) => { enum AppOS { Android = 1 } const testReq = getMockReq ( { query : { os : AppOS . Android , sellerId : '12345' , } , headers : { Authorization : 'token validtoken' , } , } ) ; expect ( testReq . query ) . toBeDefined ( ) ; expect ( testReq . query ) . toBeInstanceOf ( Object ) ; expect ( Object . keys ( testReq . query ) . length ) . toBe ( 2 ) ; expect ( testReq . query [ 'os' ] ) . toBe ( AppOS . Android ) ; expect ( testReq . query [ 'sellerId' ] ) . toBe ( '12345' ) ; expect ( testReq . headers ) . toBeDefined ( ) ; expect ( testReq . headers ) . toBeInstanceOf ( Object ) ; expect ( Object . keys ( testReq . headers ) . length ) . toBe ( 1 ) ; expect ( testReq . headers [ 'Authorization' ] ) . toBe ( 'token validtoken' ) ; } ) ; test ( 'issue #27' , ( ) => { interface CustomRequest extends Request { locals : any ; customProperty : string ; } const testReq = getMockReq < CustomRequest > ( { locals : { var : 'hi there' } , customProperty : 'value' , } ) ; expect ( testReq . locals ) . toBeDefined ( ) ; expect ( testReq . locals ) . toEqual ( { var : 'hi there' } ) ; expect ( testReq . customProperty ) . toBeDefined ( ) ; expect ( testReq . customProperty ) . toEqual ( 'value' ) ; } ) ; } ) ;