@@ -75,67 +75,68 @@ describe('paginationKeywordsRegExp', () => {
75
75
} ) ;
76
76
77
77
describe ( 'operationPagination' , ( ) => {
78
+ const queryParam = (
79
+ name : string ,
80
+ type : IR . SchemaObject [ 'type' ] = 'string' ,
81
+ ) : IR . ParameterObject => ( {
82
+ explode : true ,
83
+ location : 'query' ,
84
+ name,
85
+ schema : { type } ,
86
+ style : 'form' ,
87
+ } ) ;
88
+
89
+ const emptyContext = { } as IR . Context ;
90
+
91
+ const baseOperationMeta = {
92
+ method : 'post' as const ,
93
+ path : '/test' as const ,
94
+ } ;
95
+
78
96
const queryScenarios : Array < {
79
97
hasPagination : boolean ;
80
98
operation : IR . OperationObject ;
81
99
} > = [
82
100
{
83
101
hasPagination : true ,
84
102
operation : {
103
+ ...baseOperationMeta ,
85
104
id : 'op1' ,
86
105
method : 'get' ,
87
106
parameters : {
88
107
query : {
89
- page : {
90
- explode : true ,
91
- location : 'query' ,
92
- name : 'page' ,
93
- schema : { type : 'integer' } ,
94
- style : 'form' ,
95
- } ,
96
- perPage : {
97
- explode : true ,
98
- location : 'query' ,
99
- name : 'perPage' ,
100
- schema : { type : 'integer' } ,
101
- style : 'form' ,
102
- } ,
108
+ page : queryParam ( 'page' , 'integer' ) ,
109
+ perPage : queryParam ( 'perPage' , 'integer' ) ,
103
110
} ,
104
111
} ,
105
- path : '/test' ,
106
112
} ,
107
113
} ,
108
114
{
109
115
hasPagination : false ,
110
116
operation : {
117
+ ...baseOperationMeta ,
111
118
id : 'op2' ,
112
119
method : 'get' ,
113
120
parameters : {
114
121
query : {
115
- sort : {
116
- explode : true ,
117
- location : 'query' ,
118
- name : 'sort' ,
119
- schema : { type : 'string' } ,
120
- style : 'form' ,
121
- } ,
122
+ sort : queryParam ( 'sort' , 'string' ) ,
122
123
} ,
123
124
} ,
124
- path : '/test' ,
125
125
} ,
126
126
} ,
127
127
] ;
128
128
129
129
it . each ( queryScenarios ) (
130
130
'query params for $operation.id → $hasPagination' ,
131
131
( { hasPagination, operation } ) => {
132
- const result = operationPagination ( { context : { } as any , operation } ) ;
132
+ const result = operationPagination ( { context : emptyContext , operation } ) ;
133
133
expect ( Boolean ( result ) ) . toEqual ( hasPagination ) ;
134
134
} ,
135
135
) ;
136
136
137
137
it ( 'body.pagination === true returns entire body' , ( ) => {
138
138
const operation : IR . OperationObject = {
139
+ ...baseOperationMeta ,
139
140
body : {
140
141
mediaType : 'application/json' ,
141
142
pagination : true ,
@@ -147,11 +148,9 @@ describe('operationPagination', () => {
147
148
} ,
148
149
} ,
149
150
id : 'bodyTrue' ,
150
- method : 'post' ,
151
- path : '/test' ,
152
151
} ;
153
152
154
- const result = operationPagination ( { context : { } as any , operation } ) ;
153
+ const result = operationPagination ( { context : emptyContext , operation } ) ;
155
154
156
155
expect ( result ?. in ) . toEqual ( 'body' ) ;
157
156
expect ( result ?. name ) . toEqual ( 'body' ) ;
@@ -160,6 +159,7 @@ describe('operationPagination', () => {
160
159
161
160
it ( 'body.pagination = "pagination" returns the matching property' , ( ) => {
162
161
const operation : IR . OperationObject = {
162
+ ...baseOperationMeta ,
163
163
body : {
164
164
mediaType : 'application/json' ,
165
165
pagination : 'pagination' ,
@@ -176,11 +176,9 @@ describe('operationPagination', () => {
176
176
} ,
177
177
} ,
178
178
id : 'bodyField' ,
179
- method : 'post' ,
180
- path : '/test' ,
181
179
} ;
182
180
183
- const result = operationPagination ( { context : { } as any , operation } ) ;
181
+ const result = operationPagination ( { context : emptyContext , operation } ) ;
184
182
185
183
expect ( result ?. in ) . toEqual ( 'body' ) ;
186
184
expect ( result ?. name ) . toEqual ( 'pagination' ) ;
@@ -203,14 +201,13 @@ describe('operationPagination', () => {
203
201
} as unknown as IR . Context ;
204
202
205
203
const operation : IR . OperationObject = {
204
+ ...baseOperationMeta ,
206
205
body : {
207
206
mediaType : 'application/json' ,
208
207
pagination : 'pagination' ,
209
208
schema : { $ref : '#/components/schemas/PaginationBody' } ,
210
209
} ,
211
210
id : 'refPagination' ,
212
- method : 'post' ,
213
- path : '/test' ,
214
211
} ;
215
212
216
213
const result = operationPagination ( { context, operation } ) ;
@@ -225,6 +222,7 @@ describe('operationPagination', () => {
225
222
226
223
it ( 'falls back to query when pagination key not found in body' , ( ) => {
227
224
const operation : IR . OperationObject = {
225
+ ...baseOperationMeta ,
228
226
body : {
229
227
mediaType : 'application/json' ,
230
228
pagination : 'pagination' ,
@@ -236,22 +234,14 @@ describe('operationPagination', () => {
236
234
} ,
237
235
} ,
238
236
id : 'fallback' ,
239
- method : 'post' ,
240
237
parameters : {
241
238
query : {
242
- cursor : {
243
- explode : true ,
244
- location : 'query' ,
245
- name : 'cursor' ,
246
- schema : { type : 'string' } ,
247
- style : 'form' ,
248
- } ,
239
+ cursor : queryParam ( 'cursor' , 'string' ) ,
249
240
} ,
250
241
} ,
251
- path : '/test' ,
252
242
} ;
253
243
254
- const result = operationPagination ( { context : { } as any , operation } ) ;
244
+ const result = operationPagination ( { context : emptyContext , operation } ) ;
255
245
256
246
expect ( result ?. in ) . toEqual ( 'query' ) ;
257
247
expect ( result ?. schema ?. properties ?. cursor ) . toBeDefined ( ) ;
0 commit comments