1- import { createMocks } from 'node-mocks-http'
21import { Status } from 'simple-http-status'
32
4- import { createService } from '../src'
3+ import { createService , testService } from '../src'
54
65describe ( '[createService/get]' , ( ) => {
76 test ( 'Should be able to use default id in query' , async ( ) => {
8- const handler = createService ( {
7+ const service = createService ( {
98 get : async ( pk ) => ( {
109 message : `Your favorite animal is ${ pk } ` ,
1110 } ) ,
1211 } )
13- const { req , res } = createMocks ( {
12+ const { statusCode , data } = await testService ( service , {
1413 method : 'GET' ,
1514 query : {
1615 id : 'cat' ,
1716 } ,
1817 } )
19- await handler ( req , res )
2018
21- expect ( res . _getStatusCode ( ) ) . toBe ( Status . HTTP_200_OK )
22- expect ( JSON . parse ( res . _getData ( ) ) ) . toMatchInlineSnapshot ( `
19+ expect ( statusCode ) . toBe ( Status . HTTP_200_OK )
20+ expect ( data ) . toMatchInlineSnapshot ( `
2321 Object {
2422 "message": "Your favorite animal is cat",
2523 }
2624 ` )
2725 } )
2826 test ( 'Should be able to use default pk in query' , async ( ) => {
29- const handler = createService ( {
27+ const service = createService ( {
3028 get : async ( pk ) => ( {
3129 message : `Your favorite animal is ${ pk } ` ,
3230 } ) ,
3331 } )
34- const { req , res } = createMocks ( {
32+ const { statusCode , data } = await testService ( service , {
3533 method : 'GET' ,
3634 query : {
3735 pk : 'fish' ,
3836 } ,
3937 } )
40- await handler ( req , res )
4138
42- expect ( res . _getStatusCode ( ) ) . toBe ( Status . HTTP_200_OK )
43- expect ( JSON . parse ( res . _getData ( ) ) ) . toMatchInlineSnapshot ( `
39+ expect ( statusCode ) . toBe ( Status . HTTP_200_OK )
40+ expect ( data ) . toMatchInlineSnapshot ( `
4441 Object {
4542 "message": "Your favorite animal is fish",
4643 }
4744 ` )
4845 } )
4946
5047 test ( 'Should be able to use custom pk in query' , async ( ) => {
51- const handler = createService ( {
48+ const service = createService ( {
5249 pk : {
5350 name : 'animal' ,
5451 } ,
5552 get : async ( pk ) => ( {
5653 message : `Your favorite animal is ${ pk } ` ,
5754 } ) ,
5855 } )
59- const { req , res } = createMocks ( {
56+ const { statusCode , data } = await testService ( service , {
6057 method : 'GET' ,
6158 query : {
6259 animal : 'dog' ,
6360 } ,
6461 } )
65- await handler ( req , res )
6662
67- expect ( res . _getStatusCode ( ) ) . toBe ( Status . HTTP_200_OK )
68- expect ( JSON . parse ( res . _getData ( ) ) ) . toMatchInlineSnapshot ( `
63+ expect ( statusCode ) . toBe ( Status . HTTP_200_OK )
64+ expect ( data ) . toMatchInlineSnapshot ( `
6965 Object {
7066 "message": "Your favorite animal is dog",
7167 }
7268 ` )
7369 } )
7470
7571 test ( 'Should be able to use custom pk in query and cast it' , async ( ) => {
76- const handler = createService ( {
72+ const service = createService ( {
7773 pk : {
7874 name : 'animal' ,
7975 cast : ( pk ) => `__${ pk } __` ,
@@ -82,16 +78,15 @@ describe('[createService/get]', () => {
8278 message : `Your favorite animal is ${ pk } ` ,
8379 } ) ,
8480 } )
85- const { req , res } = createMocks ( {
81+ const { statusCode , data } = await testService ( service , {
8682 method : 'GET' ,
8783 query : {
8884 animal : 'dog' ,
8985 } ,
9086 } )
91- await handler ( req , res )
9287
93- expect ( res . _getStatusCode ( ) ) . toBe ( Status . HTTP_200_OK )
94- expect ( JSON . parse ( res . _getData ( ) ) ) . toMatchInlineSnapshot ( `
88+ expect ( statusCode ) . toBe ( Status . HTTP_200_OK )
89+ expect ( data ) . toMatchInlineSnapshot ( `
9590 Object {
9691 "message": "Your favorite animal is __dog__",
9792 }
0 commit comments