import tachijs , { controller , httpMethod , httpGet , httpPost , httpPut , httpPatch , httpDelete , httpOptions , httpHead , httpAll } from '../../index' ; import request from 'supertest' ; import { RequestHandler } from 'express' ; describe ( 'httpMethod' , ( ) => { it ( `sets get method route` , async ( ) => { @ controller ( '/' ) class HomeController { @ httpMethod ( 'get' , '/test' ) index ( ) { return 'Hello' ; } } const app = tachijs ( { controllers : [ HomeController ] } ) ; const response = await request ( app ) . get ( '/test' ) ; expect ( response ) . toMatchObject ( { status : 200 , text : 'Hello' } ) ; } ) ; it ( `sets post method route` , async ( ) => { @ controller ( '/' ) class HomeController { @ httpMethod ( 'post' , '/test' ) index ( ) { return 'Hello' ; } } const app = tachijs ( { controllers : [ HomeController ] } ) ; const response = await request ( app ) . post ( '/test' ) ; expect ( response ) . toMatchObject ( { status : 200 , text : 'Hello' } ) ; } ) ; it ( `sets put method route` , async ( ) => { @ controller ( '/' ) class HomeController { @ httpMethod ( 'put' , '/test' ) index ( ) { return 'Hello' ; } } const app = tachijs ( { controllers : [ HomeController ] } ) ; const response = await request ( app ) . put ( '/test' ) ; expect ( response ) . toMatchObject ( { status : 200 , text : 'Hello' } ) ; } ) ; it ( `sets patch method route` , async ( ) => { @ controller ( '/' ) class HomeController { @ httpMethod ( 'patch' , '/test' ) index ( ) { return 'Hello' ; } } const app = tachijs ( { controllers : [ HomeController ] } ) ; const response = await request ( app ) . patch ( '/test' ) ; expect ( response ) . toMatchObject ( { status : 200 , text : 'Hello' } ) ; } ) ; it ( `sets delete method route` , async ( ) => { @ controller ( '/' ) class HomeController { @ httpMethod ( 'delete' , '/test' ) index ( ) { return 'Hello' ; } } const app = tachijs ( { controllers : [ HomeController ] } ) ; const response = await request ( app ) . delete ( '/test' ) ; expect ( response ) . toMatchObject ( { status : 200 , text : 'Hello' } ) ; } ) ; it ( `sets options method route` , async ( ) => { @ controller ( '/' ) class HomeController { @ httpMethod ( 'options' , '/test' ) index ( ) { return '' ; } } const app = tachijs ( { controllers : [ HomeController ] } ) ; const response = await request ( app ) . options ( '/test' ) ; expect ( response ) . toMatchObject ( { status : 200 } ) ; } ) ; it ( `sets head method route` , async ( ) => { @ controller ( '/' ) class HomeController { @ httpMethod ( 'head' , '/test' ) index ( ) { return '' ; } } const app = tachijs ( { controllers : [ HomeController ] } ) ; const response = await request ( app ) . head ( '/test' ) ; expect ( response ) . toMatchObject ( { status : 200 } ) ; } ) ; it ( `sets all method route` , async ( ) => { @ controller ( '/' ) class HomeController { @ httpMethod ( 'all' , '/test' ) index ( ) { return 'Hello' ; } } const app = tachijs ( { controllers : [ HomeController ] } ) ; const response = await request ( app ) . get ( '/test' ) ; expect ( response ) . toMatchObject ( { status : 200 , text : 'Hello' } ) ; } ) ; it ( 'throws an error if the method is not valid' , async ( ) => { @ controller ( '/' ) class HomeController { @ httpMethod ( 'wrong' , '/' ) index ( ) { return 'Hello' ; } } try { tachijs ( { controllers : [ HomeController ] } ) ; } catch ( error ) { expect ( error ) . toMatchObject ( { message : '"wrong" is not a valid method.' } ) ; } } ) ; it ( 'accepts middlewares' , async ( ) => { const spy = jest . fn ( ) ; const middleware = ( req , res , next ) => { spy ( ) ; next ( ) ; } ; @ controller ( '/' ) class HomeController { @ httpMethod ( 'get' , '/' , [ middleware ] ) index ( ) { return 'Hello' ; } } const app = tachijs ( { controllers : [ HomeController ] } ) ; const response = await request ( app ) . get ( '/' ) ; expect ( response ) . toMatchObject ( { status : 200 , text : 'Hello' } ) ; expect ( spy ) . toBeCalled ( ) ; } ) ; it ( 'registers routes from top to bottom' , async ( ) => { @ controller ( '/' ) class HomeController { @ httpMethod ( 'get' , '/' ) index ( ) { return 'Hello' ; } @ httpMethod ( 'get' , '/' ) secondIndex ( ) { return 'Hello Second' ; } } const app = tachijs ( { controllers : [ HomeController ] } ) ; const response = await request ( app ) . get ( '/' ) ; expect ( response ) . toMatchObject ( { status : 200 , text : 'Hello' } ) ; } ) ; } ) ; describe ( 'httpGet' , ( ) => { it ( `sets get method route` , async ( ) => { @ controller ( '/' ) class HomeController { @ httpGet ( '/test' ) index ( ) { return 'Hello' ; } } const app = tachijs ( { controllers : [ HomeController ] } ) ; const response = await request ( app ) . get ( '/test' ) ; expect ( response ) . toMatchObject ( { status : 200 , text : 'Hello' } ) ; } ) ; } ) ; describe ( 'httpPost' , ( ) => { it ( `sets post method route` , async ( ) => { @ controller ( '/' ) class HomeController { @ httpPost ( '/test' ) index ( ) { return 'Hello' ; } } const app = tachijs ( { controllers : [ HomeController ] } ) ; const response = await request ( app ) . post ( '/test' ) ; expect ( response ) . toMatchObject ( { status : 200 , text : 'Hello' } ) ; } ) ; } ) ; describe ( 'httpPut' , ( ) => { it ( `sets put method route` , async ( ) => { @ controller ( '/' ) class HomeController { @ httpPut ( '/test' ) index ( ) { return 'Hello' ; } } const app = tachijs ( { controllers : [ HomeController ] } ) ; const response = await request ( app ) . put ( '/test' ) ; expect ( response ) . toMatchObject ( { status : 200 , text : 'Hello' } ) ; } ) ; } ) ; describe ( 'httpPatch' , ( ) => { it ( `sets patch method route` , async ( ) => { @ controller ( '/' ) class HomeController { @ httpPatch ( '/test' ) index ( ) { return 'Hello' ; } } const app = tachijs ( { controllers : [ HomeController ] } ) ; const response = await request ( app ) . patch ( '/test' ) ; expect ( response ) . toMatchObject ( { status : 200 , text : 'Hello' } ) ; } ) ; } ) ; describe ( 'httpDelete' , ( ) => { it ( `sets delete method route` , async ( ) => { @ controller ( '/' ) class HomeController { @ httpDelete ( '/test' ) index ( ) { return 'Hello' ; } } const app = tachijs ( { controllers : [ HomeController ] } ) ; const response = await request ( app ) . delete ( '/test' ) ; expect ( response ) . toMatchObject ( { status : 200 , text : 'Hello' } ) ; } ) ; } ) ; describe ( 'httpOptions' , ( ) => { it ( `sets options method route` , async ( ) => { @ controller ( '/' ) class HomeController { @ httpOptions ( '/test' ) index ( ) { return '' ; } } const app = tachijs ( { controllers : [ HomeController ] } ) ; const response = await request ( app ) . options ( '/test' ) ; expect ( response ) . toMatchObject ( { status : 200 } ) ; } ) ; } ) ; describe ( 'httpHead' , ( ) => { it ( `sets head method route` , async ( ) => { @ controller ( '/' ) class HomeController { @ httpHead ( '/test' ) index ( ) { return '' ; } } const app = tachijs ( { controllers : [ HomeController ] } ) ; const response = await request ( app ) . head ( '/test' ) ; expect ( response ) . toMatchObject ( { status : 200 } ) ; } ) ; } ) ; describe ( 'httpAll' , ( ) => { it ( `sets a route for all methods` , async ( ) => { @ controller ( '/' ) class HomeController { @ httpAll ( '/test' ) index ( ) { return 'Hello' ; } } const app = tachijs ( { controllers : [ HomeController ] } ) ; const response = await request ( app ) . get ( '/test' ) ; expect ( response ) . toMatchObject ( { status : 200 , text : 'Hello' } ) ; } ) ; it ( `sets multiple routes in same time` , async ( ) => { @ controller ( '/' ) class HomeController { @ httpMethod ( 'get' , '/test' ) index ( ) { return 'Hello' ; } @ httpMethod ( 'get' , '/test2' ) index2 ( ) { return 'Hello2' ; } } const app = tachijs ( { controllers : [ HomeController ] } ) ; const client = request ( app ) ; const response = await client . get ( '/test' ) ; const response2 = await client . get ( '/test2' ) ; expect ( response ) . toMatchObject ( { status : 200 , text : 'Hello' } ) ; expect ( response2 ) . toMatchObject ( { status : 200 , text : 'Hello2' } ) ; } ) ; } ) ;