1
1
const nock = require ( 'nock' ) ;
2
+ const chalk = require ( 'chalk' ) ;
2
3
const config = require ( 'config' ) ;
3
4
const fs = require ( 'fs' ) ;
4
5
const path = require ( 'path' ) ;
5
6
const crypto = require ( 'crypto' ) ;
6
7
const frontMatter = require ( 'gray-matter' ) ;
7
8
9
+ const APIError = require ( '../../src/lib/apiError' ) ;
10
+
8
11
const docs = require ( '../../src/cmds/docs' ) ;
9
12
const docsEdit = require ( '../../src/cmds/docs/edit' ) ;
10
13
@@ -186,18 +189,21 @@ describe('rdme docs', () => {
186
189
} ) ;
187
190
} ) ;
188
191
189
- it ( 'should create only valid docs' , ( ) => {
190
- console . log = jest . fn ( ) ;
191
- expect . assertions ( 2 ) ;
192
-
192
+ it ( 'should fail if any docs are invalid' , async ( ) => {
193
+ const folder = 'failure-docs' ;
193
194
const slug = 'fail-doc' ;
194
195
const slugTwo = 'new-doc' ;
195
196
196
- const doc = frontMatter ( fs . readFileSync ( path . join ( fixturesDir , `/failure-docs/${ slug } .md` ) ) ) ;
197
- const docTwo = frontMatter ( fs . readFileSync ( path . join ( fixturesDir , `/failure-docs/${ slugTwo } .md` ) ) ) ;
197
+ const errorObject = {
198
+ error : 'DOC_INVALID' ,
199
+ message : "We couldn't save this doc (Path `category` is required.)." ,
200
+ } ;
201
+
202
+ const doc = frontMatter ( fs . readFileSync ( path . join ( fixturesDir , `/${ folder } /${ slug } .md` ) ) ) ;
203
+ const docTwo = frontMatter ( fs . readFileSync ( path . join ( fixturesDir , `/${ folder } /${ slugTwo } .md` ) ) ) ;
198
204
199
- const hash = hashFileContents ( fs . readFileSync ( path . join ( fixturesDir , `/failure-docs /${ slug } .md` ) ) ) ;
200
- const hashTwo = hashFileContents ( fs . readFileSync ( path . join ( fixturesDir , `/failure-docs /${ slugTwo } .md` ) ) ) ;
205
+ const hash = hashFileContents ( fs . readFileSync ( path . join ( fixturesDir , `/${ folder } /${ slug } .md` ) ) ) ;
206
+ const hashTwo = hashFileContents ( fs . readFileSync ( path . join ( fixturesDir , `/${ folder } /${ slugTwo } .md` ) ) ) ;
201
207
202
208
const getMocks = getNockWithVersionHeader ( version )
203
209
. get ( `/api/v1/docs/${ slug } ` )
@@ -238,43 +244,27 @@ describe('rdme docs', () => {
238
244
} )
239
245
. post ( '/api/v1/docs' , { slug, body : doc . content , ...doc . data , lastUpdatedHash : hash } )
240
246
. basicAuth ( { user : key } )
241
- . reply ( 400 , {
242
- error : 'DOC_INVALID' ,
243
- message : "We couldn't save this doc (Path `category` is required.)." ,
244
- } ) ;
247
+ . reply ( 400 , errorObject ) ;
245
248
246
249
const versionMock = nock ( config . host )
247
250
. get ( `/api/v1/version/${ version } ` )
248
251
. basicAuth ( { user : key } )
249
252
. reply ( 200 , { version } ) ;
250
253
251
- return docs . run ( { folder : './__tests__/__fixtures__/failure-docs' , key, version } ) . then ( message => {
252
- expect ( console . log ) . toHaveBeenCalledTimes ( 1 ) ;
253
- expect ( message ) . toStrictEqual ( [
254
- {
255
- metadata : { image : [ ] , title : '' , description : '' } ,
256
- api : {
257
- method : 'post' ,
258
- url : '' ,
259
- auth : 'required' ,
260
- params : [ ] ,
261
- apiSetting,
262
- } ,
263
- title : 'This is the document title' ,
264
- updates : [ ] ,
265
- type : 'endpoint' ,
266
- slug : slugTwo ,
267
- body : 'Body' ,
268
- category,
269
- } ,
270
- ] ) ;
254
+ const fullDirectory = `__tests__/__fixtures__/${ folder } ` ;
271
255
272
- getMocks . done ( ) ;
273
- postMocks . done ( ) ;
274
- versionMock . done ( ) ;
256
+ const formattedErrorObject = {
257
+ ...errorObject ,
258
+ message : `Error uploading ${ chalk . underline ( `${ fullDirectory } /${ slug } .md` ) } :\n\n${ errorObject . message } ` ,
259
+ } ;
275
260
276
- console . log . mockRestore ( ) ;
277
- } ) ;
261
+ await expect ( docs . run ( { folder : `./${ fullDirectory } ` , key, version } ) ) . rejects . toStrictEqual (
262
+ new APIError ( formattedErrorObject )
263
+ ) ;
264
+
265
+ getMocks . done ( ) ;
266
+ postMocks . done ( ) ;
267
+ versionMock . done ( ) ;
278
268
} ) ;
279
269
} ) ;
280
270
@@ -314,14 +304,6 @@ describe('rdme docs', () => {
314
304
} ) ;
315
305
316
306
describe ( 'rdme docs:edit' , ( ) => {
317
- beforeEach ( ( ) => {
318
- console . log = jest . fn ( ) ;
319
- } ) ;
320
-
321
- afterEach ( ( ) => {
322
- console . log . mockRestore ( ) ;
323
- } ) ;
324
-
325
307
it ( 'should error if no api key provided' , ( ) => {
326
308
return expect ( docsEdit . run ( { } ) ) . rejects . toThrow ( 'No project API key provided. Please use `--key`.' ) ;
327
309
} ) ;
@@ -332,8 +314,9 @@ describe('rdme docs:edit', () => {
332
314
) ;
333
315
} ) ;
334
316
335
- it ( 'should fetch the doc from the api' , ( ) => {
336
- expect . assertions ( 4 ) ;
317
+ it ( 'should fetch the doc from the api' , async ( ) => {
318
+ expect . assertions ( 5 ) ;
319
+ console . info = jest . fn ( ) ;
337
320
const slug = 'getting-started' ;
338
321
const body = 'abcdef' ;
339
322
const edits = 'ghijkl' ;
@@ -363,55 +346,55 @@ describe('rdme docs:edit', () => {
363
346
fs . appendFile ( filename , edits , cb . bind ( null , 0 ) ) ;
364
347
}
365
348
366
- return docsEdit . run ( { slug, key, version : '1.0.0' , mockEditor } ) . then ( ( ) => {
367
- getMock . done ( ) ;
368
- putMock . done ( ) ;
369
- versionMock . done ( ) ;
370
- expect ( fs . existsSync ( `${ slug } .md` ) ) . toBe ( false ) ;
349
+ await expect ( docsEdit . run ( { slug, key, version : '1.0.0' , mockEditor } ) ) . resolves . toBeUndefined ( ) ;
371
350
372
- expect ( console . log ) . toHaveBeenCalledWith ( 'Doc successfully updated. Cleaning up local file.' ) ;
373
- } ) ;
351
+ getMock . done ( ) ;
352
+ putMock . done ( ) ;
353
+ versionMock . done ( ) ;
354
+
355
+ expect ( fs . existsSync ( `${ slug } .md` ) ) . toBe ( false ) ;
356
+ expect ( console . info ) . toHaveBeenCalledWith ( 'Doc successfully updated. Cleaning up local file.' ) ;
357
+ return console . info . mockRestore ( ) ;
374
358
} ) ;
375
359
376
- it ( 'should error if remote doc does not exist' , ( ) => {
377
- expect . assertions ( 2 ) ;
360
+ it ( 'should error if remote doc does not exist' , async ( ) => {
378
361
const slug = 'no-such-doc' ;
379
362
380
- const getMock = nock ( config . host )
381
- . get ( `/api/v1/docs/ ${ slug } ` )
382
- . reply ( 404 , {
383
- error : 'DOC_NOTFOUND ' ,
384
- message : `The doc with the slug ' ${ slug } ' couldn't be found` ,
385
- suggestion : '...a suggestion to resolve the issue...' ,
386
- help : 'If you need help, email support@readme.io and mention log "fake-metrics-uuid".' ,
387
- } ) ;
363
+ const errorObject = {
364
+ error : 'DOC_NOTFOUND' ,
365
+ message : `The doc with the slug ' ${ slug } ' couldn't be found` ,
366
+ suggestion : '...a suggestion to resolve the issue... ' ,
367
+ help : 'If you need help, email support@readme.io and mention log "fake-metrics-uuid".' ,
368
+ } ;
369
+
370
+ const getMock = nock ( config . host ) . get ( `/api/v1/docs/ ${ slug } ` ) . reply ( 404 , errorObject ) ;
388
371
389
372
const versionMock = nock ( config . host )
390
373
. get ( `/api/v1/version/${ version } ` )
391
374
. basicAuth ( { user : key } )
392
375
. reply ( 200 , { version } ) ;
393
376
394
- return docsEdit . run ( { slug, key, version : '1.0.0' } ) . catch ( err => {
395
- getMock . done ( ) ;
396
- versionMock . done ( ) ;
397
- expect ( err . code ) . toBe ( 'DOC_NOTFOUND' ) ;
398
- expect ( err . message ) . toContain ( "The doc with the slug 'no-such-doc' couldn't be found" ) ;
399
- } ) ;
377
+ await expect ( docsEdit . run ( { slug, key, version : '1.0.0' } ) ) . rejects . toThrow ( new APIError ( errorObject ) ) ;
378
+
379
+ getMock . done ( ) ;
380
+ return versionMock . done ( ) ;
400
381
} ) ;
401
382
402
- it ( 'should error if doc fails validation' , ( ) => {
383
+ it ( 'should error if doc fails validation' , async ( ) => {
403
384
expect . assertions ( 2 ) ;
404
385
const slug = 'getting-started' ;
405
386
const body = 'abcdef' ;
406
387
407
- const getMock = nock ( config . host ) . get ( `/api/v1/docs/${ slug } ` ) . reply ( 200 , { body } ) ;
408
-
409
- const putMock = nock ( config . host ) . put ( `/api/v1/docs/${ slug } ` ) . reply ( 400 , {
388
+ const errorObject = {
410
389
error : 'DOC_INVALID' ,
411
- message : " We couldn't save this doc ({error})" ,
390
+ message : ` We couldn't save this doc (${ slug } )` ,
412
391
suggestion : '...a suggestion to resolve the issue...' ,
413
392
help : 'If you need help, email support@readme.io and mention log "fake-metrics-uuid".' ,
414
- } ) ;
393
+ } ;
394
+
395
+ const getMock = nock ( config . host ) . get ( `/api/v1/docs/${ slug } ` ) . reply ( 200 , { body } ) ;
396
+
397
+ const putMock = nock ( config . host ) . put ( `/api/v1/docs/${ slug } ` ) . reply ( 400 , errorObject ) ;
415
398
416
399
const versionMock = nock ( config . host )
417
400
. get ( `/api/v1/version/${ version } ` )
@@ -422,17 +405,17 @@ describe('rdme docs:edit', () => {
422
405
return cb ( 0 ) ;
423
406
}
424
407
425
- return docsEdit . run ( { slug, key, version : '1.0.0' , mockEditor } ) . catch ( err => {
426
- expect ( err . code ) . toBe ( 'DOC_INVALID' ) ;
427
- getMock . done ( ) ;
428
- putMock . done ( ) ;
429
- versionMock . done ( ) ;
430
- expect ( fs . existsSync ( ` ${ slug } .md` ) ) . toBe ( true ) ;
431
- fs . unlinkSync ( `${ slug } .md` ) ;
432
- } ) ;
408
+ await expect ( docsEdit . run ( { slug, key, version : '1.0.0' , mockEditor } ) ) . rejects . toThrow ( new APIError ( errorObject ) ) ;
409
+
410
+ getMock . done ( ) ;
411
+ putMock . done ( ) ;
412
+ versionMock . done ( ) ;
413
+
414
+ expect ( fs . existsSync ( `${ slug } .md` ) ) . toBe ( true ) ;
415
+ fs . unlinkSync ( ` ${ slug } .md` ) ;
433
416
} ) ;
434
417
435
- it ( 'should handle error if $EDITOR fails' , ( ) => {
418
+ it ( 'should handle error if $EDITOR fails' , async ( ) => {
436
419
expect . assertions ( 1 ) ;
437
420
const slug = 'getting-started' ;
438
421
const body = 'abcdef' ;
@@ -448,10 +431,11 @@ describe('rdme docs:edit', () => {
448
431
return cb ( 1 ) ;
449
432
}
450
433
451
- return docsEdit . run ( { slug, key, version : '1.0.0' , mockEditor } ) . catch ( err => {
452
- getMock . done ( ) ;
453
- expect ( err . message ) . toBe ( 'Non zero exit code from $EDITOR' ) ;
454
- fs . unlinkSync ( `${ slug } .md` ) ;
455
- } ) ;
434
+ await expect ( docsEdit . run ( { slug, key, version : '1.0.0' , mockEditor } ) ) . rejects . toThrow (
435
+ new Error ( 'Non zero exit code from $EDITOR' )
436
+ ) ;
437
+
438
+ getMock . done ( ) ;
439
+ fs . unlinkSync ( `${ slug } .md` ) ;
456
440
} ) ;
457
441
} ) ;
0 commit comments