11import { createCollectionContent } from '../createCollectionContent'
22import { getConfig } from '../getConfig'
33import { writeFile } from 'fs/promises'
4- import { existsSync , readFileSync } from 'fs'
4+ import { existsSync } from 'fs'
55
66jest . mock ( '../getConfig' )
77jest . mock ( 'fs/promises' )
@@ -53,19 +53,18 @@ it('should call writeFile with the expected file location and content without th
5353 const mockContent = [
5454 { name : 'test' , base : 'src/docs' , pattern : '**/*.md' }
5555 ]
56- ; ( getConfig as jest . Mock ) . mockResolvedValue ( {
56+ ; ( getConfig as jest . Mock ) . mockResolvedValue ( {
5757 content : mockContent ,
5858 repoRoot : '.'
5959 } )
60- ; ( existsSync as jest . Mock ) . mockReturnValue ( false ) // No package.json
6160
6261 const mockConsoleError = jest . fn ( )
6362 jest . spyOn ( console , 'error' ) . mockImplementation ( mockConsoleError )
6463
6564 await createCollectionContent ( '/foo/' , '/config/dir/pf-docs.config.mjs' , false )
6665
6766 const expectedContent = [
68- { name : 'test' , base : '/config/dir/src/docs' , pattern : '**/*.md' , version : null }
67+ { name : 'test' , base : '/config/dir/src/docs' , pattern : '**/*.md' }
6968 ]
7069
7170 expect ( writeFile ) . toHaveBeenCalledWith (
@@ -79,11 +78,10 @@ it('should log error if writeFile throws an error', async () => {
7978 const mockContent = [
8079 { name : 'test' , base : 'src/docs' , pattern : '**/*.md' }
8180 ]
82- ; ( getConfig as jest . Mock ) . mockResolvedValue ( {
81+ ; ( getConfig as jest . Mock ) . mockResolvedValue ( {
8382 content : mockContent ,
8483 repoRoot : '.'
8584 } )
86- ; ( existsSync as jest . Mock ) . mockReturnValue ( false ) // No package.json
8785
8886 const mockConsoleError = jest . fn ( )
8987 jest . spyOn ( console , 'error' ) . mockImplementation ( mockConsoleError )
@@ -104,12 +102,11 @@ it('should log all verbose messages when run in verbose mode', async () => {
104102 { name : 'docs' , base : 'src/docs' , pattern : '**/*.md' } ,
105103 { name : 'components' , packageName : '@patternfly/react-core' , pattern : '**/*.md' }
106104 ]
107- ; ( getConfig as jest . Mock ) . mockResolvedValue ( {
105+ ; ( getConfig as jest . Mock ) . mockResolvedValue ( {
108106 content : mockContent ,
109107 repoRoot : '../'
110108 } )
111109 ; ( existsSync as jest . Mock ) . mockReturnValue ( true )
112- ; ( readFileSync as jest . Mock ) . mockReturnValue ( JSON . stringify ( { version : '6.2.2' } ) )
113110 ; ( writeFile as jest . Mock ) . mockResolvedValue ( undefined )
114111
115112 const mockConsoleLog = jest . fn ( )
@@ -121,19 +118,15 @@ it('should log all verbose messages when run in verbose mode', async () => {
121118 expect ( mockConsoleLog ) . toHaveBeenCalledWith ( 'configuration content entry: ' , mockContent , '\n' )
122119 expect ( mockConsoleLog ) . toHaveBeenCalledWith ( 'Creating content file' , '/foo/src/content.ts' , '\n' )
123120 expect ( mockConsoleLog ) . toHaveBeenCalledWith ( 'repoRootDir: ' , '/config' , '\n' )
124-
121+
125122 // For the base entry
126123 expect ( mockConsoleLog ) . toHaveBeenCalledWith ( 'relative path: ' , 'src/docs' )
127124 expect ( mockConsoleLog ) . toHaveBeenCalledWith ( 'absolute path: ' , '/config/dir/src/docs' , '\n' )
128-
125+
129126 // For the packageName entry
130127 expect ( mockConsoleLog ) . toHaveBeenCalledWith ( 'looking for package in ' , '/config/dir/node_modules' , '\n' )
131128 expect ( mockConsoleLog ) . toHaveBeenCalledWith ( 'found package at ' , '/config/dir/node_modules/@patternfly/react-core' , '\n' )
132-
133- // Version extraction logs
134- expect ( mockConsoleLog ) . toHaveBeenCalledWith ( 'Extracted version v6 from /config/dir/src/docs/package.json\n' )
135- expect ( mockConsoleLog ) . toHaveBeenCalledWith ( 'Extracted version v6 from /config/dir/node_modules/@patternfly/react-core/package.json\n' )
136-
129+
137130 // Final log
138131 expect ( mockConsoleLog ) . toHaveBeenCalledWith ( 'Content file created' )
139132} )
@@ -184,11 +177,10 @@ it('should not log to the console when not run in verbose mode', async () => {
184177 const mockContent = [
185178 { name : 'test' , base : 'src/docs' , pattern : '**/*.md' }
186179 ]
187- ; ( getConfig as jest . Mock ) . mockResolvedValue ( {
180+ ; ( getConfig as jest . Mock ) . mockResolvedValue ( {
188181 content : mockContent ,
189182 repoRoot : '.'
190183 } )
191- ; ( existsSync as jest . Mock ) . mockReturnValue ( false )
192184 ; ( writeFile as jest . Mock ) . mockResolvedValue ( undefined )
193185
194186 const mockConsoleLog = jest . fn ( )
@@ -203,12 +195,11 @@ it('should handle content with packageName by finding package in node_modules',
203195 const mockContent = [
204196 { name : 'test' , packageName : '@patternfly/react-core' , pattern : '**/*.md' }
205197 ]
206- ; ( getConfig as jest . Mock ) . mockResolvedValue ( {
198+ ; ( getConfig as jest . Mock ) . mockResolvedValue ( {
207199 content : mockContent ,
208200 repoRoot : '.'
209201 } )
210202 ; ( existsSync as jest . Mock ) . mockReturnValue ( true )
211- ; ( readFileSync as jest . Mock ) . mockReturnValue ( JSON . stringify ( { version : '6.2.2' } ) )
212203 ; ( writeFile as jest . Mock ) . mockResolvedValue ( undefined )
213204
214205 const mockConsoleError = jest . fn ( )
@@ -217,11 +208,10 @@ it('should handle content with packageName by finding package in node_modules',
217208 await createCollectionContent ( '/foo/' , '/config/dir/pf-docs.config.mjs' , false )
218209
219210 const expectedContent = [
220- {
211+ {
221212 base : '/config/dir/node_modules/@patternfly/react-core' ,
222- version : 'v6' ,
223- name : 'test' ,
224- packageName : '@patternfly/react-core' ,
213+ name : 'test' ,
214+ packageName : '@patternfly/react-core' ,
225215 pattern : '**/*.md'
226216 }
227217 ]
@@ -237,15 +227,13 @@ it('should handle content with packageName when package is not found locally but
237227 const mockContent = [
238228 { name : 'test' , packageName : '@patternfly/react-core' , pattern : '**/*.md' }
239229 ]
240- ; ( getConfig as jest . Mock ) . mockResolvedValue ( {
230+ ; ( getConfig as jest . Mock ) . mockResolvedValue ( {
241231 content : mockContent ,
242232 repoRoot : '../../'
243233 } )
244234 ; ( existsSync as jest . Mock )
245235 . mockReturnValueOnce ( false ) // not found in /config/dir/node_modules
246- . mockReturnValueOnce ( true ) // found in /config/node_modules/package.json
247- . mockReturnValueOnce ( true ) // package.json exists for version extraction
248- ; ( readFileSync as jest . Mock ) . mockReturnValue ( JSON . stringify ( { version : '5.1.0' } ) )
236+ . mockReturnValueOnce ( true ) // found in /config/node_modules
249237 ; ( writeFile as jest . Mock ) . mockResolvedValue ( undefined )
250238
251239 const mockConsoleError = jest . fn ( )
@@ -254,11 +242,10 @@ it('should handle content with packageName when package is not found locally but
254242 await createCollectionContent ( '/foo/' , '/config/dir/pf-docs.config.mjs' , false )
255243
256244 const expectedContent = [
257- {
245+ {
258246 base : '/config/node_modules/@patternfly/react-core' ,
259- version : 'v5' ,
260- name : 'test' ,
261- packageName : '@patternfly/react-core' ,
247+ name : 'test' ,
248+ packageName : '@patternfly/react-core' ,
262249 pattern : '**/*.md'
263250 }
264251 ]
@@ -274,7 +261,7 @@ it('should handle content with packageName when package is not found anywhere',
274261 const mockContent = [
275262 { name : 'test' , packageName : '@patternfly/react-core' , pattern : '**/*.md' }
276263 ]
277- ; ( getConfig as jest . Mock ) . mockResolvedValue ( {
264+ ; ( getConfig as jest . Mock ) . mockResolvedValue ( {
278265 content : mockContent ,
279266 repoRoot : '../'
280267 } )
@@ -287,11 +274,10 @@ it('should handle content with packageName when package is not found anywhere',
287274 await createCollectionContent ( '/foo/' , '/config/dir/pf-docs.config.mjs' , false )
288275
289276 const expectedContent = [
290- {
277+ {
291278 base : null ,
292- version : null ,
293- name : 'test' ,
294- packageName : '@patternfly/react-core' ,
279+ name : 'test' ,
280+ packageName : '@patternfly/react-core' ,
295281 pattern : '**/*.md'
296282 }
297283 ]
@@ -308,12 +294,11 @@ it('should handle mixed content with both base and packageName entries', async (
308294 { name : 'docs' , base : 'src/docs' , pattern : '**/*.md' } ,
309295 { name : 'components' , packageName : '@patternfly/react-core' , pattern : '**/*.md' }
310296 ]
311- ; ( getConfig as jest . Mock ) . mockResolvedValue ( {
297+ ; ( getConfig as jest . Mock ) . mockResolvedValue ( {
312298 content : mockContent ,
313299 repoRoot : '../'
314300 } )
315301 ; ( existsSync as jest . Mock ) . mockReturnValue ( true )
316- ; ( readFileSync as jest . Mock ) . mockReturnValue ( JSON . stringify ( { version : '6.2.2' } ) )
317302 ; ( writeFile as jest . Mock ) . mockResolvedValue ( undefined )
318303
319304 const mockConsoleError = jest . fn ( )
@@ -322,12 +307,11 @@ it('should handle mixed content with both base and packageName entries', async (
322307 await createCollectionContent ( '/foo/' , '/config/dir/pf-docs.config.mjs' , false )
323308
324309 const expectedContent = [
325- { name : 'docs' , base : '/config/dir/src/docs' , pattern : '**/*.md' , version : 'v6' } ,
326- {
310+ { name : 'docs' , base : '/config/dir/src/docs' , pattern : '**/*.md' } ,
311+ {
327312 base : '/config/dir/node_modules/@patternfly/react-core' ,
328- version : 'v6' ,
329- name : 'components' ,
330- packageName : '@patternfly/react-core' ,
313+ name : 'components' ,
314+ packageName : '@patternfly/react-core' ,
331315 pattern : '**/*.md'
332316 }
333317 ]
@@ -338,173 +322,3 @@ it('should handle mixed content with both base and packageName entries', async (
338322 )
339323 expect ( mockConsoleError ) . not . toHaveBeenCalled ( )
340324} )
341-
342- describe ( 'getPackageVersion function' , ( ) => {
343- it ( 'should extract major version from valid package.json with version 6.2.2' , async ( ) => {
344- const mockContent = [
345- { name : 'test' , base : 'src/docs' , pattern : '**/*.md' }
346- ]
347- ; ( getConfig as jest . Mock ) . mockResolvedValue ( {
348- content : mockContent ,
349- repoRoot : '.'
350- } )
351- ; ( existsSync as jest . Mock ) . mockReturnValue ( true )
352- ; ( readFileSync as jest . Mock ) . mockReturnValue ( JSON . stringify ( { version : '6.2.2' } ) )
353- ; ( writeFile as jest . Mock ) . mockResolvedValue ( undefined )
354-
355- await createCollectionContent ( '/foo/' , '/config/dir/pf-docs.config.mjs' , false )
356-
357- const expectedContent = [
358- { name : 'test' , base : '/config/dir/src/docs' , pattern : '**/*.md' , version : 'v6' }
359- ]
360-
361- expect ( writeFile ) . toHaveBeenCalledWith (
362- '/foo/src/content.ts' ,
363- `export const content = ${ JSON . stringify ( expectedContent ) } ` ,
364- )
365- } )
366-
367- it ( 'should extract major version from valid package.json with version 5.1.0' , async ( ) => {
368- const mockContent = [
369- { name : 'test' , base : 'src/docs' , pattern : '**/*.md' }
370- ]
371- ; ( getConfig as jest . Mock ) . mockResolvedValue ( {
372- content : mockContent ,
373- repoRoot : '.'
374- } )
375- ; ( existsSync as jest . Mock ) . mockReturnValue ( true )
376- ; ( readFileSync as jest . Mock ) . mockReturnValue ( JSON . stringify ( { version : '5.1.0' } ) )
377- ; ( writeFile as jest . Mock ) . mockResolvedValue ( undefined )
378-
379- await createCollectionContent ( '/foo/' , '/config/dir/pf-docs.config.mjs' , false )
380-
381- const expectedContent = [
382- { name : 'test' , base : '/config/dir/src/docs' , pattern : '**/*.md' , version : 'v5' }
383- ]
384-
385- expect ( writeFile ) . toHaveBeenCalledWith (
386- '/foo/src/content.ts' ,
387- `export const content = ${ JSON . stringify ( expectedContent ) } ` ,
388- )
389- } )
390-
391- it ( 'should return null version when package.json does not exist' , async ( ) => {
392- const mockContent = [
393- { name : 'test' , base : 'src/docs' , pattern : '**/*.md' }
394- ]
395- ; ( getConfig as jest . Mock ) . mockResolvedValue ( {
396- content : mockContent ,
397- repoRoot : '.'
398- } )
399- ; ( existsSync as jest . Mock ) . mockReturnValue ( false )
400- ; ( writeFile as jest . Mock ) . mockResolvedValue ( undefined )
401-
402- await createCollectionContent ( '/foo/' , '/config/dir/pf-docs.config.mjs' , false )
403-
404- const expectedContent = [
405- { name : 'test' , base : '/config/dir/src/docs' , pattern : '**/*.md' , version : null }
406- ]
407-
408- expect ( writeFile ) . toHaveBeenCalledWith (
409- '/foo/src/content.ts' ,
410- `export const content = ${ JSON . stringify ( expectedContent ) } ` ,
411- )
412- } )
413-
414- it ( 'should return null version when package.json has no version field' , async ( ) => {
415- const mockContent = [
416- { name : 'test' , base : 'src/docs' , pattern : '**/*.md' }
417- ]
418- ; ( getConfig as jest . Mock ) . mockResolvedValue ( {
419- content : mockContent ,
420- repoRoot : '.'
421- } )
422- ; ( existsSync as jest . Mock ) . mockReturnValue ( true )
423- ; ( readFileSync as jest . Mock ) . mockReturnValue ( JSON . stringify ( { name : 'test-package' } ) )
424- ; ( writeFile as jest . Mock ) . mockResolvedValue ( undefined )
425-
426- await createCollectionContent ( '/foo/' , '/config/dir/pf-docs.config.mjs' , false )
427-
428- const expectedContent = [
429- { name : 'test' , base : '/config/dir/src/docs' , pattern : '**/*.md' , version : null }
430- ]
431-
432- expect ( writeFile ) . toHaveBeenCalledWith (
433- '/foo/src/content.ts' ,
434- `export const content = ${ JSON . stringify ( expectedContent ) } ` ,
435- )
436- } )
437-
438- it ( 'should handle malformed package.json gracefully' , async ( ) => {
439- const mockContent = [
440- { name : 'test' , base : 'src/docs' , pattern : '**/*.md' }
441- ]
442- ; ( getConfig as jest . Mock ) . mockResolvedValue ( {
443- content : mockContent ,
444- repoRoot : '.'
445- } )
446- ; ( existsSync as jest . Mock ) . mockReturnValue ( true )
447- ; ( readFileSync as jest . Mock ) . mockReturnValue ( '{ invalid json }' )
448- ; ( writeFile as jest . Mock ) . mockResolvedValue ( undefined )
449-
450- const mockConsoleError = jest . fn ( )
451- jest . spyOn ( console , 'error' ) . mockImplementation ( mockConsoleError )
452-
453- await createCollectionContent ( '/foo/' , '/config/dir/pf-docs.config.mjs' , false )
454-
455- const expectedContent = [
456- { name : 'test' , base : '/config/dir/src/docs' , pattern : '**/*.md' , version : null }
457- ]
458-
459- expect ( writeFile ) . toHaveBeenCalledWith (
460- '/foo/src/content.ts' ,
461- `export const content = ${ JSON . stringify ( expectedContent ) } ` ,
462- )
463- } )
464-
465- it ( 'should handle version with pre-release tags (e.g., 6.0.0-beta.1)' , async ( ) => {
466- const mockContent = [
467- { name : 'test' , base : 'src/docs' , pattern : '**/*.md' }
468- ]
469- ; ( getConfig as jest . Mock ) . mockResolvedValue ( {
470- content : mockContent ,
471- repoRoot : '.'
472- } )
473- ; ( existsSync as jest . Mock ) . mockReturnValue ( true )
474- ; ( readFileSync as jest . Mock ) . mockReturnValue ( JSON . stringify ( { version : '6.0.0-beta.1' } ) )
475- ; ( writeFile as jest . Mock ) . mockResolvedValue ( undefined )
476-
477- await createCollectionContent ( '/foo/' , '/config/dir/pf-docs.config.mjs' , false )
478-
479- const expectedContent = [
480- { name : 'test' , base : '/config/dir/src/docs' , pattern : '**/*.md' , version : 'v6' }
481- ]
482-
483- expect ( writeFile ) . toHaveBeenCalledWith (
484- '/foo/src/content.ts' ,
485- `export const content = ${ JSON . stringify ( expectedContent ) } ` ,
486- )
487- } )
488-
489- it ( 'should log version extraction in verbose mode' , async ( ) => {
490- const mockContent = [
491- { name : 'test' , base : 'src/docs' , pattern : '**/*.md' }
492- ]
493- ; ( getConfig as jest . Mock ) . mockResolvedValue ( {
494- content : mockContent ,
495- repoRoot : '.'
496- } )
497- ; ( existsSync as jest . Mock ) . mockReturnValue ( true )
498- ; ( readFileSync as jest . Mock ) . mockReturnValue ( JSON . stringify ( { version : '6.2.2' } ) )
499- ; ( writeFile as jest . Mock ) . mockResolvedValue ( undefined )
500-
501- const mockConsoleLog = jest . fn ( )
502- jest . spyOn ( console , 'log' ) . mockImplementation ( mockConsoleLog )
503-
504- await createCollectionContent ( '/foo/' , '/config/dir/pf-docs.config.mjs' , true )
505-
506- expect ( mockConsoleLog ) . toHaveBeenCalledWith (
507- 'Extracted version v6 from /config/dir/src/docs/package.json\n'
508- )
509- } )
510- } )
0 commit comments