@@ -9,8 +9,22 @@ const docs = require('../../src/cmds/docs');
99const docsEdit = require ( '../../src/cmds/docs/edit' ) ;
1010
1111const fixturesDir = `${ __dirname } ./../__fixtures__` ;
12- const key = 'Xmw4bGctRVIQz7R7dQXqH9nQe5d0SPQs ' ;
12+ const key = 'API_KEY ' ;
1313const version = '1.0.0' ;
14+ const category = 'CATEGORY_ID' ;
15+ const apiSetting = 'API_SETTING_ID' ;
16+
17+ function getNockWithVersionHeader ( v ) {
18+ return nock ( config . host , {
19+ reqheaders : {
20+ 'x-readme-version' : v ,
21+ } ,
22+ } ) ;
23+ }
24+
25+ function hashFileContents ( contents ) {
26+ return crypto . createHash ( 'sha1' ) . update ( contents ) . digest ( 'hex' ) ;
27+ }
1428
1529describe ( 'rdme docs' , ( ) => {
1630 beforeAll ( ( ) => nock . disableNetConnect ( ) ) ;
@@ -39,64 +53,84 @@ describe('rdme docs', () => {
3953 it . todo ( 'should error if the folder contains no markdown files' ) ;
4054
4155 describe ( 'existing docs' , ( ) => {
56+ let simpleDoc ;
57+ let anotherDoc ;
58+
59+ beforeEach ( ( ) => {
60+ let fileContents = fs . readFileSync ( path . join ( fixturesDir , `/existing-docs/simple-doc.md` ) ) ;
61+ simpleDoc = {
62+ slug : 'simple-doc' ,
63+ doc : frontMatter ( fileContents ) ,
64+ hash : hashFileContents ( fileContents ) ,
65+ } ;
66+
67+ fileContents = fs . readFileSync ( path . join ( fixturesDir , `/existing-docs/subdir/another-doc.md` ) ) ;
68+ anotherDoc = {
69+ slug : 'another-doc' ,
70+ doc : frontMatter ( fileContents ) ,
71+ hash : hashFileContents ( fileContents ) ,
72+ } ;
73+ } ) ;
74+
4275 it ( 'should fetch doc and merge with what is returned' , ( ) => {
43- const slug = 'simple-doc' ;
44- const doc = frontMatter ( fs . readFileSync ( path . join ( fixturesDir , `/existing-docs/${ slug } .md` ) ) ) ;
45- const hash = crypto
46- . createHash ( 'sha1' )
47- . update ( fs . readFileSync ( path . join ( fixturesDir , `/existing-docs/${ slug } .md` ) ) )
48- . digest ( 'hex' ) ;
49-
50- const getMock = nock ( config . host , {
51- reqheaders : {
52- 'x-readme-version' : version ,
53- } ,
54- } )
55- . get ( `/api/v1/docs/${ slug } ` )
56- . basicAuth ( { user : key } )
57- . reply ( 200 , { category : '5ae9ece93a685f47efb9a97c' , slug } ) ;
76+ expect . assertions ( 1 ) ;
5877
59- const putMock = nock ( config . host , {
60- reqheaders : {
61- 'x-readme-version' : version ,
62- } ,
63- } )
64- . put ( `/api/v1/docs/${ slug } ` , {
65- category : '5ae9ece93a685f47efb9a97c' ,
66- slug,
67- body : doc . content ,
68- lastUpdatedHash : hash ,
69- ...doc . data ,
78+ const getMocks = getNockWithVersionHeader ( version )
79+ . get ( `/api/v1/docs/simple-doc` )
80+ . basicAuth ( { user : key } )
81+ . reply ( 200 , { category, slug : simpleDoc . slug , lastUpdatedHash : 'anOldHash' } )
82+ . get ( `/api/v1/docs/another-doc` )
83+ . basicAuth ( { user : key } )
84+ . reply ( 200 , { category, slug : anotherDoc . slug , lastUpdatedHash : 'anOldHash' } ) ;
85+
86+ const updateMocks = getNockWithVersionHeader ( version )
87+ . put ( '/api/v1/docs/simple-doc' , {
88+ category,
89+ slug : simpleDoc . slug ,
90+ body : simpleDoc . doc . content ,
91+ lastUpdatedHash : simpleDoc . hash ,
92+ ...simpleDoc . doc . data ,
93+ } )
94+ . basicAuth ( { user : key } )
95+ . reply ( 200 )
96+ . put ( '/api/v1/docs/another-doc' , {
97+ category,
98+ slug : anotherDoc . slug ,
99+ body : anotherDoc . doc . content ,
100+ lastUpdatedHash : anotherDoc . hash ,
101+ ...anotherDoc . doc . data ,
70102 } )
71103 . basicAuth ( { user : key } )
72104 . reply ( 200 ) ;
73105
74- return docs . run ( { folder : './__tests__/__fixtures__/existing-docs' , key, version } ) . then ( ( ) => {
75- getMock . done ( ) ;
76- putMock . done ( ) ;
106+ return docs . run ( { folder : './__tests__/__fixtures__/existing-docs' , key, version } ) . then ( skippedDocs => {
107+ // All docs should have been updated because their hashes from the GET request were different from what they
108+ // are currently.
109+ expect ( skippedDocs ) . toHaveLength ( 0 ) ;
110+
111+ getMocks . done ( ) ;
112+ updateMocks . done ( ) ;
77113 } ) ;
78114 } ) ;
79115
80116 it ( 'should not send requests for docs that have not changed' , ( ) => {
81117 expect . assertions ( 1 ) ;
82- const slug = 'simple-doc' ;
83- const hash = crypto
84- . createHash ( 'sha1' )
85- . update ( fs . readFileSync ( path . join ( fixturesDir , `/existing-docs/${ slug } .md` ) ) )
86- . digest ( 'hex' ) ;
87-
88- const getMock = nock ( config . host , {
89- reqheaders : {
90- 'x-readme-version' : version ,
91- } ,
92- } )
93- . get ( `/api/v1/docs/${ slug } ` )
118+
119+ const getMocks = getNockWithVersionHeader ( version )
120+ . get ( '/api/v1/docs/simple-doc' )
121+ . basicAuth ( { user : key } )
122+ . reply ( 200 , { category, slug : simpleDoc . slug , lastUpdatedHash : simpleDoc . hash } )
123+ . get ( '/api/v1/docs/another-doc' )
94124 . basicAuth ( { user : key } )
95- . reply ( 200 , { category : '5ae9ece93a685f47efb9a97c' , slug, lastUpdatedHash : hash } ) ;
125+ . reply ( 200 , { category, slug : anotherDoc . slug , lastUpdatedHash : anotherDoc . hash } ) ;
96126
97- return docs . run ( { folder : './__tests__/__fixtures__/existing-docs' , key, version } ) . then ( ( [ message ] ) => {
98- expect ( message ) . toBe ( '`simple-doc` not updated. No changes.' ) ;
99- getMock . done ( ) ;
127+ return docs . run ( { folder : './__tests__/__fixtures__/existing-docs' , key, version } ) . then ( skippedDocs => {
128+ expect ( skippedDocs ) . toStrictEqual ( [
129+ '`simple-doc` was not updated because there were no changes.' ,
130+ '`another-doc` was not updated because there were no changes.' ,
131+ ] ) ;
132+
133+ getMocks . done ( ) ;
100134 } ) ;
101135 } ) ;
102136 } ) ;
@@ -105,16 +139,9 @@ describe('rdme docs', () => {
105139 it ( 'should create new doc' , ( ) => {
106140 const slug = 'new-doc' ;
107141 const doc = frontMatter ( fs . readFileSync ( path . join ( fixturesDir , `/new-docs/${ slug } .md` ) ) ) ;
108- const hash = crypto
109- . createHash ( 'sha1' )
110- . update ( fs . readFileSync ( path . join ( fixturesDir , `/new-docs/${ slug } .md` ) ) )
111- . digest ( 'hex' ) ;
112-
113- const getMock = nock ( config . host , {
114- reqheaders : {
115- 'x-readme-version' : version ,
116- } ,
117- } )
142+ const hash = hashFileContents ( fs . readFileSync ( path . join ( fixturesDir , `/new-docs/${ slug } .md` ) ) ) ;
143+
144+ const getMock = getNockWithVersionHeader ( version )
118145 . get ( `/api/v1/docs/${ slug } ` )
119146 . basicAuth ( { user : key } )
120147 . reply ( 404 , {
@@ -124,11 +151,7 @@ describe('rdme docs', () => {
124151 help : 'If you need help, email support@readme.io and mention log "fake-metrics-uuid".' ,
125152 } ) ;
126153
127- const postMock = nock ( config . host , {
128- reqheaders : {
129- 'x-readme-version' : version ,
130- } ,
131- } )
154+ const postMock = getNockWithVersionHeader ( version )
132155 . post ( `/api/v1/docs` , { slug, body : doc . content , ...doc . data , lastUpdatedHash : hash } )
133156 . basicAuth ( { user : key } )
134157 . reply ( 201 ) ;
@@ -142,39 +165,25 @@ describe('rdme docs', () => {
142165 it ( 'should create only valid docs' , ( ) => {
143166 console . log = jest . fn ( ) ;
144167 expect . assertions ( 2 ) ;
168+
145169 const slug = 'fail-doc' ;
146170 const slugTwo = 'new-doc' ;
171+
147172 const doc = frontMatter ( fs . readFileSync ( path . join ( fixturesDir , `/failure-docs/${ slug } .md` ) ) ) ;
148173 const docTwo = frontMatter ( fs . readFileSync ( path . join ( fixturesDir , `/failure-docs/${ slugTwo } .md` ) ) ) ;
149- const hash = crypto
150- . createHash ( 'sha1' )
151- . update ( fs . readFileSync ( path . join ( fixturesDir , `/failure-docs/${ slug } .md` ) ) )
152- . digest ( 'hex' ) ;
153-
154- const hashTwo = crypto
155- . createHash ( 'sha1' )
156- . update ( fs . readFileSync ( path . join ( fixturesDir , `/failure-docs/${ slugTwo } .md` ) ) )
157- . digest ( 'hex' ) ;
158-
159- const getMock = nock ( config . host , {
160- reqheaders : {
161- 'x-readme-version' : version ,
162- } ,
163- } )
174+
175+ const hash = hashFileContents ( fs . readFileSync ( path . join ( fixturesDir , `/failure-docs/${ slug } .md` ) ) ) ;
176+ const hashTwo = hashFileContents ( fs . readFileSync ( path . join ( fixturesDir , `/failure-docs/${ slugTwo } .md` ) ) ) ;
177+
178+ const getMocks = getNockWithVersionHeader ( version )
164179 . get ( `/api/v1/docs/${ slug } ` )
165180 . basicAuth ( { user : key } )
166181 . reply ( 404 , {
167182 error : 'DOC_NOTFOUND' ,
168183 message : `The doc with the slug '${ slug } ' couldn't be found` ,
169184 suggestion : '...a suggestion to resolve the issue...' ,
170185 help : 'If you need help, email support@readme.io and mention log "fake-metrics-uuid".' ,
171- } ) ;
172-
173- const getMockTwo = nock ( config . host , {
174- reqheaders : {
175- 'x-readme-version' : version ,
176- } ,
177- } )
186+ } )
178187 . get ( `/api/v1/docs/${ slugTwo } ` )
179188 . basicAuth ( { user : key } )
180189 . reply ( 404 , {
@@ -184,24 +193,14 @@ describe('rdme docs', () => {
184193 help : 'If you need help, email support@readme.io and mention log "fake-metrics-uuid".' ,
185194 } ) ;
186195
187- const postMock = nock ( config . host , {
188- reqheaders : {
189- 'x-readme-version' : version ,
190- } ,
191- } )
192- . post ( `/api/v1/docs` , { slug, body : doc . content , ...doc . data , lastUpdatedHash : hash } )
196+ const postMocks = getNockWithVersionHeader ( version )
197+ . post ( '/api/v1/docs' , { slug, body : doc . content , ...doc . data , lastUpdatedHash : hash } )
193198 . basicAuth ( { user : key } )
194199 . reply ( 400 , {
195200 error : 'DOC_INVALID' ,
196201 message : "We couldn't save this doc (Path `category` is required.)." ,
197- } ) ;
198-
199- const postMockTwo = nock ( config . host , {
200- reqheaders : {
201- 'x-readme-version' : version ,
202- } ,
203- } )
204- . post ( `/api/v1/docs` , { slug : slugTwo , body : docTwo . content , ...docTwo . data , lastUpdatedHash : hashTwo } )
202+ } )
203+ . post ( '/api/v1/docs' , { slug : slugTwo , body : docTwo . content , ...docTwo . data , lastUpdatedHash : hashTwo } )
205204 . basicAuth ( { user : key } )
206205 . reply ( 201 , {
207206 metadata : { image : [ ] , title : '' , description : '' } ,
@@ -210,14 +209,14 @@ describe('rdme docs', () => {
210209 url : '' ,
211210 auth : 'required' ,
212211 params : [ ] ,
213- apiSetting : '60ddf83e30681022753e27af' ,
212+ apiSetting,
214213 } ,
215214 title : 'This is the document title' ,
216215 updates : [ ] ,
217216 type : 'endpoint' ,
218217 slug : slugTwo ,
219218 body : 'Body' ,
220- category : '5ae122e10fdf4e39bb34db6f' ,
219+ category,
221220 } ) ;
222221
223222 return docs . run ( { folder : './__tests__/__fixtures__/failure-docs' , key, version } ) . then ( message => {
@@ -230,20 +229,20 @@ describe('rdme docs', () => {
230229 url : '' ,
231230 auth : 'required' ,
232231 params : [ ] ,
233- apiSetting : '60ddf83e30681022753e27af' ,
232+ apiSetting,
234233 } ,
235234 title : 'This is the document title' ,
236235 updates : [ ] ,
237236 type : 'endpoint' ,
238237 slug : slugTwo ,
239238 body : 'Body' ,
240- category : '5ae122e10fdf4e39bb34db6f' ,
239+ category,
241240 } ,
242241 ] ) ;
243- getMock . done ( ) ;
244- getMockTwo . done ( ) ;
245- postMock . done ( ) ;
246- postMockTwo . done ( ) ;
242+
243+ getMocks . done ( ) ;
244+ postMocks . done ( ) ;
245+
247246 console . log . mockRestore ( ) ;
248247 } ) ;
249248 } ) ;
@@ -279,22 +278,14 @@ describe('rdme docs:edit', () => {
279278 const body = 'abcdef' ;
280279 const edits = 'ghijkl' ;
281280
282- const getMock = nock ( config . host , {
283- reqheaders : {
284- 'x-readme-version' : version ,
285- } ,
286- } )
281+ const getMock = getNockWithVersionHeader ( version )
287282 . get ( `/api/v1/docs/${ slug } ` )
288283 . basicAuth ( { user : key } )
289- . reply ( 200 , { category : '5ae9ece93a685f47efb9a97c' , slug, body } ) ;
284+ . reply ( 200 , { category, slug, body } ) ;
290285
291- const putMock = nock ( config . host , {
292- reqheaders : {
293- 'x-readme-version' : version ,
294- } ,
295- } )
286+ const putMock = getNockWithVersionHeader ( version )
296287 . put ( `/api/v1/docs/${ slug } ` , {
297- category : '5ae9ece93a685f47efb9a97c' ,
288+ category,
298289 slug,
299290 body : `${ body } ${ edits } ` ,
300291 } )
0 commit comments