@@ -6,14 +6,16 @@ const petstore = require('@readme/oas-examples/3.0/json/petstore.json');
6
6
7
7
const CodeSample = require ( '../src/CodeSample' ) ;
8
8
9
- const { Operation } = Oas ;
9
+ const oas = new Oas ( petstore ) ;
10
+
10
11
const props = {
11
12
auth : { } ,
12
13
formData : { } ,
13
14
language : 'node' ,
14
15
setLanguage : ( ) => { } ,
16
+ oas,
15
17
oasUrl : 'https://example.com/openapi.json' ,
16
- operation : new Operation ( { } , '/pet/{petId}' , 'get' ) ,
18
+ operation : oas . operation ( '/pet/{petId}' , 'get' ) ,
17
19
} ;
18
20
19
21
describe ( 'tabs' , ( ) => {
@@ -27,9 +29,9 @@ describe('tabs', () => {
27
29
{ ...props }
28
30
oas = {
29
31
new Oas ( {
32
+ ...petstore ,
30
33
[ extensions . SAMPLES_ENABLED ] : true ,
31
34
[ extensions . SAMPLES_LANGUAGES ] : languages ,
32
- servers : [ { url : 'http://example.com' } ] ,
33
35
} )
34
36
}
35
37
/>
@@ -58,15 +60,10 @@ describe('tabs', () => {
58
60
59
61
describe ( 'code examples' , ( ) => {
60
62
it ( 'should support the `node-simple` language' , ( ) => {
61
- const docProps = {
62
- ...props ,
63
- language : 'node-simple' ,
64
- operation : new Operation ( { } , '/pet/{petId}' , 'get' ) ,
65
- } ;
66
-
67
63
const codeSample = shallow (
68
64
< CodeSample
69
- { ...docProps }
65
+ { ...props }
66
+ language = { 'node-simple' }
70
67
oas = {
71
68
new Oas ( {
72
69
...petstore ,
@@ -83,25 +80,20 @@ describe('code examples', () => {
83
80
} ) ;
84
81
85
82
it ( 'should display custom examples over pre-filled examples' , ( ) => {
86
- const docProps = {
87
- ...props ,
88
- operation : new Operation ( { } , '/pet/{petId}' , 'get' ) ,
89
- examples : [
90
- {
91
- language : 'javascript' ,
92
- code : 'console.log(1);' ,
93
- } ,
94
- ] ,
95
- } ;
96
-
97
83
const codeSample = shallow (
98
84
< CodeSample
99
- { ...docProps }
85
+ { ...props }
86
+ examples = { [
87
+ {
88
+ language : 'javascript' ,
89
+ code : 'console.log(1);' ,
90
+ } ,
91
+ ] }
100
92
oas = {
101
93
new Oas ( {
94
+ ...petstore ,
102
95
[ extensions . SAMPLES_ENABLED ] : true ,
103
96
[ extensions . SAMPLES_LANGUAGES ] : [ 'node' , 'curl' ] ,
104
- servers : [ { url : 'http://example.com' } ] ,
105
97
} )
106
98
}
107
99
/>
@@ -112,24 +104,19 @@ describe('code examples', () => {
112
104
} ) ;
113
105
114
106
it ( 'should display custom examples even if SAMPLES_ENABLED is false' , ( ) => {
115
- const docProps = {
116
- ...props ,
117
- operation : new Operation ( { } , '/pet/{petId}' , 'get' ) ,
118
- examples : [
119
- {
120
- language : 'javascript' ,
121
- code : 'console.log(1);' ,
122
- } ,
123
- ] ,
124
- } ;
125
-
126
107
const codeSample = shallow (
127
108
< CodeSample
128
- { ...docProps }
109
+ { ...props }
110
+ examples = { [
111
+ {
112
+ language : 'javascript' ,
113
+ code : 'console.log(1);' ,
114
+ } ,
115
+ ] }
129
116
oas = {
130
117
new Oas ( {
118
+ ...petstore ,
131
119
[ extensions . SAMPLES_ENABLED ] : false ,
132
- servers : [ { url : 'http://example.com' } ] ,
133
120
} )
134
121
}
135
122
/>
@@ -140,20 +127,15 @@ describe('code examples', () => {
140
127
} ) ;
141
128
142
129
it ( 'should not error if no code given' , ( ) => {
143
- const docProps = {
144
- ...props ,
145
- operation : new Operation ( { } , '/pet/{petId}' , 'get' ) ,
146
- examples : [
147
- {
148
- language : 'javascript' ,
149
- } ,
150
- ] ,
151
- } ;
152
-
153
130
expect ( ( ) =>
154
131
shallow (
155
132
< CodeSample
156
- { ...docProps }
133
+ { ...props }
134
+ examples = { [
135
+ {
136
+ language : 'javascript' ,
137
+ } ,
138
+ ] }
157
139
oas = {
158
140
new Oas ( {
159
141
[ extensions . SAMPLES_ENABLED ] : true ,
@@ -167,20 +149,15 @@ describe('code examples', () => {
167
149
} ) ;
168
150
169
151
it ( 'should not error if language requested cannot be auto-generated' , ( ) => {
170
- const docProps = {
171
- ...props ,
172
- operation : new Operation ( { } , '/pet/{petId}' , 'get' ) ,
173
- language : 'css' ,
174
- } ;
175
-
176
152
const component = (
177
153
< CodeSample
178
- { ...docProps }
154
+ { ...props }
155
+ language = { 'css' }
179
156
oas = {
180
157
new Oas ( {
158
+ ...petstore ,
181
159
[ extensions . SAMPLES_ENABLED ] : true ,
182
160
[ extensions . SAMPLES_LANGUAGES ] : [ 'css' ] ,
183
- servers : [ { url : 'http://example.com' } ] ,
184
161
} )
185
162
}
186
163
/>
@@ -195,28 +172,23 @@ describe('code examples', () => {
195
172
} ) ;
196
173
197
174
it ( 'should not render sample if language is missing' , ( ) => {
198
- const docProps = {
199
- ...props ,
200
- operation : new Operation ( { } , '/pet/{petId}' , 'get' ) ,
201
- examples : [
202
- {
203
- code : 'console.log(1);' ,
204
- } ,
205
- {
206
- language : 'curl' ,
207
- code : 'curl example.com' ,
208
- } ,
209
- ] ,
210
- } ;
211
-
212
175
const codeSample = shallow (
213
176
< CodeSample
214
- { ...docProps }
177
+ { ...props }
178
+ examples = { [
179
+ {
180
+ code : 'console.log(1);' ,
181
+ } ,
182
+ {
183
+ language : 'curl' ,
184
+ code : 'curl example.com' ,
185
+ } ,
186
+ ] }
215
187
oas = {
216
188
new Oas ( {
189
+ ...petstore ,
217
190
[ extensions . SAMPLES_ENABLED ] : true ,
218
191
[ extensions . SAMPLES_LANGUAGES ] : [ 'node' , 'curl' ] ,
219
- servers : [ { url : 'http://example.com' } ] ,
220
192
} )
221
193
}
222
194
/>
@@ -227,28 +199,23 @@ describe('code examples', () => {
227
199
} ) ;
228
200
229
201
it ( 'should render first of examples if language does not exist' , ( ) => {
230
- const docProps = {
231
- ...props ,
232
- operation : new Operation ( { } , '/pet/{petId}' , 'get' ) ,
233
- examples : [
234
- {
235
- language : 'javascript' ,
236
- } ,
237
- {
238
- language : 'typescript' ,
239
- } ,
240
- ] ,
241
- language : 'perl' ,
242
- } ;
243
-
244
202
const codeSample = shallow (
245
203
< CodeSample
246
- { ...docProps }
204
+ { ...props }
205
+ examples = { [
206
+ {
207
+ language : 'javascript' ,
208
+ } ,
209
+ {
210
+ language : 'typescript' ,
211
+ } ,
212
+ ] }
213
+ language = { 'perl' }
247
214
oas = {
248
215
new Oas ( {
216
+ ...petstore ,
249
217
[ extensions . SAMPLES_ENABLED ] : true ,
250
218
[ extensions . SAMPLES_LANGUAGES ] : [ 'css' ] ,
251
- servers : [ { url : 'http://example.com' } ] ,
252
219
} )
253
220
}
254
221
/>
@@ -258,74 +225,69 @@ describe('code examples', () => {
258
225
} ) ;
259
226
260
227
it ( 'should display examples if SAMPLES_ENABLED is true' , ( ) => {
261
- const languages = [ 'node' , 'curl' ] ;
262
228
const codeSample = shallow (
263
229
< CodeSample
264
230
{ ...props }
265
231
oas = {
266
232
new Oas ( {
233
+ ...petstore ,
267
234
[ extensions . SAMPLES_ENABLED ] : true ,
268
- [ extensions . SAMPLES_LANGUAGES ] : languages ,
269
- servers : [ { url : 'http://example.com' } ] ,
235
+ [ extensions . SAMPLES_LANGUAGES ] : [ 'node' , 'curl' ] ,
270
236
} )
271
237
}
272
238
/>
273
239
) ;
274
240
275
241
expect ( codeSample . find ( '.hub-code-auto' ) ) . toHaveLength ( 1 ) ;
242
+
276
243
// We only render one language at a time
277
244
expect ( codeSample . find ( '.hub-code-auto pre' ) ) . toHaveLength ( 1 ) ;
278
245
expect ( codeSample . find ( '.hub-lang-switch-node' ) . text ( ) ) . toBe ( 'Node' ) ;
279
246
} ) ;
280
247
281
248
// Skipped until https://github.com/readmeio/api-explorer/issues/965 is resolved.
282
249
it . skip ( 'should check the operation level extensions first' , ( ) => {
283
- const operationSamplesEnabled = new Operation ( { } , '/pet/{petId}' , 'get' ) ;
284
- operationSamplesEnabled [ extensions . SAMPLES_ENABLED ] = true ;
285
- const languages = [ 'node' , 'curl' ] ;
250
+ const operationSamplesEnabled = oas . operation ( '/pet/{petId}' , 'get' ) ;
251
+ operationSamplesEnabled . schema [ extensions . SAMPLES_ENABLED ] = true ;
286
252
287
253
const codeSample = shallow (
288
254
< CodeSample
289
255
{ ...props }
290
256
oas = {
291
257
new Oas ( {
258
+ ...petstore ,
292
259
[ extensions . SAMPLES_ENABLED ] : false ,
293
- [ extensions . SAMPLES_LANGUAGES ] : languages ,
294
- servers : [ { url : 'http://example.com' } ] ,
260
+ [ extensions . SAMPLES_LANGUAGES ] : [ 'node' , 'curl' ] ,
295
261
} )
296
262
}
297
263
operation = { operationSamplesEnabled }
298
264
/>
299
265
) ;
300
266
301
267
expect ( codeSample . find ( '.hub-code-auto' ) ) . toHaveLength ( 1 ) ;
268
+
302
269
// We only render one language at a time
303
270
expect ( codeSample . find ( '.hub-code-auto pre' ) ) . toHaveLength ( 1 ) ;
304
271
expect ( codeSample . find ( '.hub-lang-switch-node' ) . text ( ) ) . toBe ( 'Node' ) ;
305
272
} ) ;
306
273
307
274
it ( 'should not display more than one example block at a time' , ( ) => {
308
- const docProps = {
309
- ...props ,
310
- operation : new Operation ( { } , '/pet/{petId}' , 'get' ) ,
311
- language : 'javascript' ,
312
- examples : [
313
- {
314
- name : 'Javascript/Node.js' ,
315
- code : 'console.log(1);' ,
316
- language : 'javascript' ,
317
- } ,
318
- {
319
- name : 'TypeScript' ,
320
- code : 'console.log(1)' ,
321
- language : 'javascript' ,
322
- } ,
323
- ] ,
324
- } ;
325
-
326
275
const codeSample = shallow (
327
276
< CodeSample
328
- { ...docProps }
277
+ { ...props }
278
+ examples = { [
279
+ {
280
+ name : 'Javascript/Node.js' ,
281
+ code : 'console.log(1);' ,
282
+ language : 'javascript' ,
283
+ } ,
284
+ {
285
+ name : 'TypeScript' ,
286
+ code : 'console.log(1)' ,
287
+ language : 'javascript' ,
288
+ } ,
289
+ ] }
290
+ language = { 'javascript' }
329
291
oas = {
330
292
new Oas ( {
331
293
[ extensions . SAMPLES_ENABLED ] : true ,
@@ -349,9 +311,9 @@ describe('updating language', () => {
349
311
{ ...props }
350
312
oas = {
351
313
new Oas ( {
314
+ ...petstore ,
352
315
[ extensions . SAMPLES_ENABLED ] : true ,
353
316
[ extensions . SAMPLES_LANGUAGES ] : [ 'node' ] ,
354
- servers : [ { url : 'http://example.com' } ] ,
355
317
} )
356
318
}
357
319
setLanguage = { setLanguage }
0 commit comments