@@ -2,11 +2,16 @@ import {ActionParameterHandler} from "../src/ActionParameterHandler";
2
2
import { ActionMetadata , ControllerMetadata , ExpressDriver , ParamMetadata } from "../src" ;
3
3
import { ActionMetadataArgs } from "../src/metadata/args/ActionMetadataArgs" ;
4
4
import { ControllerMetadataArgs } from "../src/metadata/args/ControllerMetadataArgs" ;
5
+ import { ParamType } from "../src/metadata/types/ParamType" ;
5
6
6
7
const expect = require ( "chakram" ) . expect ;
7
8
8
9
describe ( "ActionParameterHandler" , ( ) => {
9
- const buildParamMetadata = ( ) : ParamMetadata => {
10
+ const buildParamMetadata = (
11
+ name : string = "id" ,
12
+ type : ParamType = "param" ,
13
+ isRequired : boolean = false ,
14
+ ) : ParamMetadata => {
10
15
const controllerMetadataArgs : ControllerMetadataArgs = {
11
16
target : function ( ) {
12
17
} ,
@@ -27,6 +32,8 @@ describe("ActionParameterHandler", () => {
27
32
const actionMetadata = new ActionMetadata ( controllerMetadata , args , { } ) ;
28
33
29
34
return {
35
+ type,
36
+ name,
30
37
targetName : "product" ,
31
38
isTargetObject : true ,
32
39
actionMetadata,
@@ -36,10 +43,8 @@ describe("ActionParameterHandler", () => {
36
43
object : "getProduct" ,
37
44
extraOptions : undefined ,
38
45
index : 0 ,
39
- type : "param" ,
40
- name : "id" ,
41
46
parse : undefined ,
42
- required : false ,
47
+ required : isRequired ,
43
48
transform : function ( action , value ) {
44
49
return value ;
45
50
} ,
@@ -53,45 +58,45 @@ describe("ActionParameterHandler", () => {
53
58
it ( "handle - should process string parameters" , async ( ) => {
54
59
const driver = new ExpressDriver ( ) ;
55
60
const actionParameterHandler = new ActionParameterHandler ( driver ) ;
56
- const param = buildParamMetadata ( ) ;
61
+ const param = buildParamMetadata ( "uuid" ) ;
57
62
58
63
const action = {
59
64
request : {
60
65
params : {
61
- id : "0b5ec98f-e26d-4414-b798-dcd35a5ef859" ,
66
+ uuid : "0b5ec98f-e26d-4414-b798-dcd35a5ef859" ,
62
67
} ,
63
68
} ,
64
69
response : { } ,
65
70
} ;
66
71
67
72
const processedValue = await actionParameterHandler . handle ( action , param ) ;
68
73
69
- expect ( processedValue ) . to . be . eq ( action . request . params . id ) ;
74
+ expect ( processedValue ) . to . be . eq ( action . request . params . uuid ) ;
70
75
} ) ;
71
76
72
77
it ( "handle - should process string parameters, returns empty if a given string is empty" , async ( ) => {
73
78
const driver = new ExpressDriver ( ) ;
74
79
const actionParameterHandler = new ActionParameterHandler ( driver ) ;
75
- const param = buildParamMetadata ( ) ;
80
+ const param = buildParamMetadata ( "uuid" ) ;
76
81
77
82
const action = {
78
83
request : {
79
84
params : {
80
- id : "" ,
85
+ uuid : "" ,
81
86
} ,
82
87
} ,
83
88
response : { } ,
84
89
} ;
85
90
86
91
const processedValue = await actionParameterHandler . handle ( action , param ) ;
87
92
88
- expect ( processedValue ) . to . be . eq ( action . request . params . id ) ;
93
+ expect ( processedValue ) . to . be . eq ( action . request . params . uuid ) ;
89
94
} ) ;
90
95
91
96
it ( "handle - should process number parameters" , async ( ) => {
92
97
const driver = new ExpressDriver ( ) ;
93
98
const actionParameterHandler = new ActionParameterHandler ( driver ) ;
94
- const param = buildParamMetadata ( ) ;
99
+ const param = buildParamMetadata ( "id" ) ;
95
100
96
101
const action = {
97
102
request : {
@@ -124,4 +129,46 @@ describe("ActionParameterHandler", () => {
124
129
expect ( processedValue ) . to . be . eq ( undefined ) ;
125
130
} ) ;
126
131
132
+ it ( "handle - throws error if the parameter is required" , async ( ) => {
133
+ const driver = new ExpressDriver ( ) ;
134
+ const actionParameterHandler = new ActionParameterHandler ( driver ) ;
135
+ const param = buildParamMetadata ( "uuid" , "param" , true ) ;
136
+
137
+ const action = {
138
+ request : { } ,
139
+ response : { } ,
140
+ } ;
141
+ let error ;
142
+
143
+ try {
144
+ await actionParameterHandler . handle ( action , param ) ;
145
+ } catch ( e ) {
146
+ error = e ;
147
+ }
148
+
149
+ expect ( error . toString ( ) ) . to . be . eq ( 'TypeError: Cannot read property \'uuid\' of undefined' ) ;
150
+ } ) ;
151
+
152
+ it ( "handle - throws error if the parameter is required, type file provided" , async ( ) => {
153
+ const driver = new ExpressDriver ( ) ;
154
+ const actionParameterHandler = new ActionParameterHandler ( driver ) ;
155
+ const param = buildParamMetadata ( "uuid" , "file" , true ) ;
156
+
157
+ const action = {
158
+ request : { } ,
159
+ response : { } ,
160
+ } ;
161
+ let error ;
162
+
163
+ try {
164
+ await actionParameterHandler . handle ( action , param ) ;
165
+ } catch ( e ) {
166
+ error = e ;
167
+ }
168
+
169
+ expect ( error . httpCode ) . to . be . eq ( 400 ) ;
170
+ expect ( error . name ) . to . be . eq ( "ParamRequiredError" ) ;
171
+ expect ( error . message ) . to . be . eq ( "Uploaded file \"uuid\" is required for request on undefined undefined" ) ;
172
+ } ) ;
173
+
127
174
} ) ;
0 commit comments