Skip to content

Commit 1f4fa7a

Browse files
authored
chore(openapi): make code more dry
1 parent c78f4b4 commit 1f4fa7a

File tree

1 file changed

+32
-42
lines changed

1 file changed

+32
-42
lines changed

packages/openapi-ts/src/ir/__tests__/pagination.test.ts

Lines changed: 32 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -75,67 +75,68 @@ describe('paginationKeywordsRegExp', () => {
7575
});
7676

7777
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+
7896
const queryScenarios: Array<{
7997
hasPagination: boolean;
8098
operation: IR.OperationObject;
8199
}> = [
82100
{
83101
hasPagination: true,
84102
operation: {
103+
...baseOperationMeta,
85104
id: 'op1',
86105
method: 'get',
87106
parameters: {
88107
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'),
103110
},
104111
},
105-
path: '/test',
106112
},
107113
},
108114
{
109115
hasPagination: false,
110116
operation: {
117+
...baseOperationMeta,
111118
id: 'op2',
112119
method: 'get',
113120
parameters: {
114121
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'),
122123
},
123124
},
124-
path: '/test',
125125
},
126126
},
127127
];
128128

129129
it.each(queryScenarios)(
130130
'query params for $operation.id → $hasPagination',
131131
({ hasPagination, operation }) => {
132-
const result = operationPagination({ context: {} as any, operation });
132+
const result = operationPagination({ context: emptyContext, operation });
133133
expect(Boolean(result)).toEqual(hasPagination);
134134
},
135135
);
136136

137137
it('body.pagination === true returns entire body', () => {
138138
const operation: IR.OperationObject = {
139+
...baseOperationMeta,
139140
body: {
140141
mediaType: 'application/json',
141142
pagination: true,
@@ -147,11 +148,9 @@ describe('operationPagination', () => {
147148
},
148149
},
149150
id: 'bodyTrue',
150-
method: 'post',
151-
path: '/test',
152151
};
153152

154-
const result = operationPagination({ context: {} as any, operation });
153+
const result = operationPagination({ context: emptyContext, operation });
155154

156155
expect(result?.in).toEqual('body');
157156
expect(result?.name).toEqual('body');
@@ -160,6 +159,7 @@ describe('operationPagination', () => {
160159

161160
it('body.pagination = "pagination" returns the matching property', () => {
162161
const operation: IR.OperationObject = {
162+
...baseOperationMeta,
163163
body: {
164164
mediaType: 'application/json',
165165
pagination: 'pagination',
@@ -176,11 +176,9 @@ describe('operationPagination', () => {
176176
},
177177
},
178178
id: 'bodyField',
179-
method: 'post',
180-
path: '/test',
181179
};
182180

183-
const result = operationPagination({ context: {} as any, operation });
181+
const result = operationPagination({ context: emptyContext, operation });
184182

185183
expect(result?.in).toEqual('body');
186184
expect(result?.name).toEqual('pagination');
@@ -203,14 +201,13 @@ describe('operationPagination', () => {
203201
} as unknown as IR.Context;
204202

205203
const operation: IR.OperationObject = {
204+
...baseOperationMeta,
206205
body: {
207206
mediaType: 'application/json',
208207
pagination: 'pagination',
209208
schema: { $ref: '#/components/schemas/PaginationBody' },
210209
},
211210
id: 'refPagination',
212-
method: 'post',
213-
path: '/test',
214211
};
215212

216213
const result = operationPagination({ context, operation });
@@ -225,6 +222,7 @@ describe('operationPagination', () => {
225222

226223
it('falls back to query when pagination key not found in body', () => {
227224
const operation: IR.OperationObject = {
225+
...baseOperationMeta,
228226
body: {
229227
mediaType: 'application/json',
230228
pagination: 'pagination',
@@ -236,22 +234,14 @@ describe('operationPagination', () => {
236234
},
237235
},
238236
id: 'fallback',
239-
method: 'post',
240237
parameters: {
241238
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'),
249240
},
250241
},
251-
path: '/test',
252242
};
253243

254-
const result = operationPagination({ context: {} as any, operation });
244+
const result = operationPagination({ context: emptyContext, operation });
255245

256246
expect(result?.in).toEqual('query');
257247
expect(result?.schema?.properties?.cursor).toBeDefined();

0 commit comments

Comments
 (0)