1
1
import { describe , expect , test } from 'vitest'
2
+ import {
3
+ shouldShowRequestContentType ,
4
+ shouldShowResponseContentType ,
5
+ generateExampleOptionTexts ,
6
+ } from '@/rest/lib/content-type-utils'
2
7
3
8
describe ( 'Request Content Type Logic' , ( ) => {
4
- // Helper function to extract the logic from RestCodeSamples
5
- function shouldShowRequestContentType ( codeExamples ) {
6
- const requestContentTypesDiffer =
7
- codeExamples . length > 1 &&
8
- ! codeExamples . every (
9
- ( example ) => example . request ?. contentType === codeExamples [ 0 ] . request ?. contentType ,
10
- )
11
- return requestContentTypesDiffer
12
- }
13
-
14
- function shouldShowResponseContentType ( codeExamples ) {
15
- const responseContentTypesDiffer =
16
- codeExamples . length > 1 &&
17
- ! codeExamples . every (
18
- ( example ) => example . response ?. contentType === codeExamples [ 0 ] . response ?. contentType ,
19
- )
20
- return responseContentTypesDiffer
21
- }
22
-
23
- function generateExampleOptions ( codeExamples ) {
24
- const requestContentTypesDiffer = shouldShowRequestContentType ( codeExamples )
25
- const responseContentTypesDiffer = shouldShowResponseContentType ( codeExamples )
26
- const showExampleOptionMediaType = responseContentTypesDiffer || requestContentTypesDiffer
27
-
28
- return codeExamples . map ( ( example , index ) => {
29
- const requestContentType = example . request ?. contentType
30
- const responseContentType = example . response ?. contentType
31
-
32
- let text = example . request ?. description || `Example ${ index + 1 } `
33
-
34
- if ( showExampleOptionMediaType ) {
35
- if ( requestContentTypesDiffer && responseContentTypesDiffer ) {
36
- // Show both request and response content types
37
- text = `${ text } (${ requestContentType } → ${ responseContentType } )`
38
- } else if ( requestContentTypesDiffer ) {
39
- // Show only request content type
40
- text = `${ text } (${ requestContentType } )`
41
- } else if ( responseContentTypesDiffer ) {
42
- // Show only response content type
43
- text = `${ text } (${ responseContentType } )`
44
- }
45
- }
46
-
47
- return text
48
- } )
49
- }
50
-
51
9
test ( 'detects request content types differ correctly' , ( ) => {
52
10
const codeExamples = [
53
11
{
54
- request : { contentType : 'text/plain' , description : 'Example' } ,
12
+ description : 'Example' ,
13
+ request : { contentType : 'text/plain' } ,
55
14
response : { contentType : 'text/html' } ,
56
15
} ,
57
16
{
58
- request : { contentType : 'text/x-markdown' , description : 'Rendering markdown' } ,
17
+ description : 'Rendering markdown' ,
18
+ request : { contentType : 'text/x-markdown' } ,
59
19
response : { contentType : 'text/html' } ,
60
20
} ,
61
21
]
@@ -67,11 +27,13 @@ describe('Request Content Type Logic', () => {
67
27
test ( 'detects response content types differ correctly' , ( ) => {
68
28
const codeExamples = [
69
29
{
70
- request : { contentType : 'application/json' , description : 'JSON example' } ,
30
+ description : 'JSON example' ,
31
+ request : { contentType : 'application/json' } ,
71
32
response : { contentType : 'application/json' } ,
72
33
} ,
73
34
{
74
- request : { contentType : 'application/json' , description : 'Another JSON example' } ,
35
+ description : 'Another JSON example' ,
36
+ request : { contentType : 'application/json' } ,
75
37
response : { contentType : 'text/html' } ,
76
38
} ,
77
39
]
@@ -83,53 +45,53 @@ describe('Request Content Type Logic', () => {
83
45
test ( 'generates correct options for markdown/raw scenario' , ( ) => {
84
46
const markdownRawExamples = [
85
47
{
48
+ description : 'Example' ,
86
49
request : {
87
50
contentType : 'text/plain' ,
88
- description : 'Example' ,
89
51
} ,
90
52
response : {
91
53
contentType : 'text/html' ,
92
54
} ,
93
55
} ,
94
56
{
57
+ description : 'Rendering markdown' ,
95
58
request : {
96
59
contentType : 'text/x-markdown' ,
97
- description : 'Rendering markdown' ,
98
60
} ,
99
61
response : {
100
62
contentType : 'text/html' ,
101
63
} ,
102
64
} ,
103
65
]
104
66
105
- const options = generateExampleOptions ( markdownRawExamples )
67
+ const options = generateExampleOptionTexts ( markdownRawExamples )
106
68
107
69
expect ( options ) . toEqual ( [ 'Example (text/plain)' , 'Rendering markdown (text/x-markdown)' ] )
108
70
} )
109
71
110
72
test ( 'generates correct options when both request and response differ' , ( ) => {
111
73
const mixedExamples = [
112
74
{
75
+ description : 'JSON request' ,
113
76
request : {
114
77
contentType : 'application/json' ,
115
- description : 'JSON request' ,
116
78
} ,
117
79
response : {
118
80
contentType : 'application/json' ,
119
81
} ,
120
82
} ,
121
83
{
84
+ description : 'Plain text request' ,
122
85
request : {
123
86
contentType : 'text/plain' ,
124
- description : 'Plain text request' ,
125
87
} ,
126
88
response : {
127
89
contentType : 'text/html' ,
128
90
} ,
129
91
} ,
130
92
]
131
93
132
- const options = generateExampleOptions ( mixedExamples )
94
+ const options = generateExampleOptionTexts ( mixedExamples )
133
95
134
96
expect ( options ) . toEqual ( [
135
97
'JSON request (application/json → application/json)' ,
@@ -140,36 +102,36 @@ describe('Request Content Type Logic', () => {
140
102
test ( 'does not show content types when they are all the same' , ( ) => {
141
103
const sameContentTypeExamples = [
142
104
{
105
+ description : 'First example' ,
143
106
request : {
144
107
contentType : 'application/json' ,
145
- description : 'First example' ,
146
108
} ,
147
109
response : {
148
110
contentType : 'application/json' ,
149
111
} ,
150
112
} ,
151
113
{
114
+ description : 'Second example' ,
152
115
request : {
153
116
contentType : 'application/json' ,
154
- description : 'Second example' ,
155
117
} ,
156
118
response : {
157
119
contentType : 'application/json' ,
158
120
} ,
159
121
} ,
160
122
]
161
123
162
- const options = generateExampleOptions ( sameContentTypeExamples )
124
+ const options = generateExampleOptionTexts ( sameContentTypeExamples )
163
125
164
126
expect ( options ) . toEqual ( [ 'First example' , 'Second example' ] )
165
127
} )
166
128
167
129
test ( 'handles single example correctly' , ( ) => {
168
130
const singleExample = [
169
131
{
132
+ description : 'Only example' ,
170
133
request : {
171
134
contentType : 'application/json' ,
172
- description : 'Only example' ,
173
135
} ,
174
136
response : {
175
137
contentType : 'application/json' ,
@@ -180,7 +142,7 @@ describe('Request Content Type Logic', () => {
180
142
expect ( shouldShowRequestContentType ( singleExample ) ) . toBe ( false )
181
143
expect ( shouldShowResponseContentType ( singleExample ) ) . toBe ( false )
182
144
183
- const options = generateExampleOptions ( singleExample )
145
+ const options = generateExampleOptionTexts ( singleExample )
184
146
expect ( options ) . toEqual ( [ 'Only example' ] )
185
147
} )
186
148
} )
0 commit comments