import request from 'supertest' ; import { BaseController , EndResult , JSONResult , RedirectResult , RenderResult , SendFileResult , SendResult , SendStatusResult , controller , httpGet , tachijs } from '../index' ; import { ConfigSetter } from '../tachijs' ; import { ErrorRequestHandler } from 'express' ; describe ( 'BaseController' , ( ) => { const baseController = new BaseController ( ) ; describe ( '#end' , ( ) => { it ( 'returns EndResult' , ( ) => { const result = baseController . end ( 'test' ) ; expect ( result ) . toBeInstanceOf ( EndResult ) ; expect ( result ) . toMatchObject ( { data : 'test' } ) ; } ) ; } ) ; describe ( '#json' , ( ) => { it ( 'returns JSONResult' , ( ) => { const result = baseController . json ( { message : 'test' } ) ; expect ( result ) . toBeInstanceOf ( JSONResult ) ; expect ( result ) . toMatchObject ( { data : { message : 'test' } } ) ; } ) ; } ) ; describe ( '#redirect' , ( ) => { it ( 'returns RedirectResult' , ( ) => { const result = baseController . redirect ( '/test' ) ; expect ( result ) . toBeInstanceOf ( RedirectResult ) ; expect ( result ) . toMatchObject ( { location : '/test' } ) ; } ) ; } ) ; describe ( '#render' , ( ) => { it ( 'returns RenderResult' , ( ) => { const result = baseController . render ( 'test' ) ; expect ( result ) . toBeInstanceOf ( RenderResult ) ; expect ( result ) . toMatchObject ( { view : 'test' } ) ; } ) ; } ) ; describe ( '#sendFile' , ( ) => { it ( 'returns SendFileResult' , ( ) => { const result = baseController . sendFile ( '/file-path' ) ; expect ( result ) . toBeInstanceOf ( SendFileResult ) ; expect ( result ) . toMatchObject ( { filePath : '/file-path' } ) ; } ) ; } ) ; describe ( '#send' , ( ) => { it ( 'returns SendResult' , ( ) => { const result = baseController . send ( 'test' ) ; expect ( result ) . toBeInstanceOf ( SendResult ) ; expect ( result ) . toMatchObject ( { data : 'test' } ) ; } ) ; } ) ; describe ( '#sendStatus' , ( ) => { it ( 'returns SendStatusResult' , ( ) => { const result = baseController . sendStatus ( 201 ) ; expect ( result ) . toBeInstanceOf ( SendStatusResult ) ; expect ( result ) . toMatchObject ( { status : 201 } ) ; } ) ; } ) ; describe ( '#context.inject' , ( ) => { it ( 'returns a registered service from the container' , async ( ) => { enum ServiceTypes { MyService = 'MyService' } class MyService { sayHello ( ) { return 'Hello' ; } } const container = { [ ServiceTypes . MyService ] : MyService } ; @ controller ( '/' ) class HomeController extends BaseController { @ httpGet ( '/' ) index ( ) { return this . context ! . inject < MyService > ( ServiceTypes . MyService ) . sayHello ( ) ; } } const app = tachijs ( { controllers : [ HomeController ] , container } ) ; const response = await request ( app ) . get ( '/' ) ; expect ( response ) . toMatchObject ( { status : 200 , text : 'Hello' } ) ; } ) ; it ( 'throws an error if no service registered for the given key' , async ( ) => { enum ServiceTypes { MyService = 'MyService' } class MyService { sayHello ( ) { return 'Hello' ; } } @ controller ( '/' ) class HomeController extends BaseController { @ httpGet ( '/' ) index ( ) { return this . context ! . inject < MyService > ( ServiceTypes . MyService ) . sayHello ( ) ; } } const after = expressApp => { const errorHandler = ( error , req , res , next ) => { res . status ( 500 ) . json ( { message : error . message } ) ; } ; expressApp . use ( errorHandler ) ; } ; const app = tachijs ( { controllers : [ HomeController ] , after } ) ; const response = await request ( app ) . get ( '/' ) ; expect ( response ) . toMatchObject ( { status : 500 , text : JSON . stringify ( { message : 'No service is registered for "MyService" key.' } ) } ) ; } ) ; } ) ; describe ( '#inject' , ( ) => { it ( 'returns a registered service from the container' , async ( ) => { enum ServiceTypes { MyService = 'MyService' } class MyService { sayHello ( ) { return 'Hello' ; } } const container = { [ ServiceTypes . MyService ] : MyService } ; @ controller ( '/' ) class HomeController extends BaseController { @ httpGet ( '/' ) index ( ) { return this . inject < MyService > ( ServiceTypes . MyService ) . sayHello ( ) ; } } const app = tachijs ( { controllers : [ HomeController ] , container } ) ; const response = await request ( app ) . get ( '/' ) ; expect ( response ) . toMatchObject ( { status : 200 , text : 'Hello' } ) ; } ) ; it ( 'throws an error if no service registered for the given key' , async ( ) => { enum ServiceTypes { MyService = 'MyService' } class MyService { sayHello ( ) { return 'Hello' ; } } @ controller ( '/' ) class HomeController extends BaseController { @ httpGet ( '/' ) index ( ) { return this . inject < MyService > ( ServiceTypes . MyService ) . sayHello ( ) ; } } const after = expressApp => { const errorHandler = ( error , req , res , next ) => { res . status ( 500 ) . json ( { message : error . message } ) ; } ; expressApp . use ( errorHandler ) ; } ; const app = tachijs ( { controllers : [ HomeController ] , after } ) ; const response = await request ( app ) . get ( '/' ) ; expect ( response ) . toMatchObject ( { status : 500 , text : JSON . stringify ( { message : 'No service is registered for "MyService" key.' } ) } ) ; } ) ; it ( 'returns a registered service from the container' , async ( ) => { expect . assertions ( 1 ) ; try { baseController . inject ( 'something' ) ; } catch ( error ) { expect ( error ) . toMatchObject ( { message : 'Injector is not set.' } ) ; } } ) ; } ) ; } ) ;