@@ -4,7 +4,7 @@ import { generateStructure } from '../structure';
44import { isRootYamlObject , isYamlObject } from '../types' ;
55
66const mockExistsSync = vi . spyOn ( fs , 'existsSync' ) ;
7- vi . spyOn ( fs , 'statSync' ) . mockReturnValue ( {
7+ const mockStatSync = vi . spyOn ( fs , 'statSync' ) . mockReturnValue ( {
88 isDirectory : ( ) => true
99} as any ) ;
1010
@@ -55,32 +55,71 @@ describe('Test structure generation', () => {
5555
5656describe ( 'Test tree validation' , ( ) => {
5757 const validPaths = [
58- '/dir' ,
59- '/dir/real_item0'
58+ '/' ,
59+ '/real_item0' ,
60+ '/real_item1' ,
61+ '/real_item1/real_item2' ,
6062 ] ;
6163
6264 mockExistsSync . mockImplementation ( path => validPaths . includes ( path as string ) ) ;
6365
6466 test ( 'Successful validation' , ( ) => {
6567 const [ , , warnings ] = generateStructure ( {
66- path : '/dir ' ,
68+ path : '. ' ,
6769 name : 'root' ,
6870 children : [
69- 'real_item0'
71+ 'real_item0' ,
72+ {
73+ name : 'real_item1' ,
74+ children : [ 'real_item2' ]
75+ }
7076 ]
7177 } , '/' ) ;
7278 expect ( warnings . length ) . toEqual ( 0 ) ;
73- expect ( fs . existsSync ) . toHaveBeenCalledTimes ( 2 ) ;
79+ expect ( fs . existsSync ) . toHaveBeenCalledTimes ( 4 ) ;
7480 } ) ;
7581
76- test ( 'Unsuccessful validation' , ( ) => {
82+ test ( 'Unsuccessful validation when child item doesn\'t exist ' , ( ) => {
7783 const [ , , warnings ] = generateStructure ( {
78- path : './dir ' ,
84+ path : '.' ,
7985 name : 'root' ,
8086 children : [ 'fake_item0' ]
8187 } , '/' ) ;
8288
8389 expect ( warnings . length ) . toEqual ( 1 ) ;
8490 expect ( fs . existsSync ) . toHaveBeenCalledTimes ( 2 ) ;
8591 } ) ;
92+
93+ test ( 'Unsuccessful validation when item with children is not a folder' , ( ) => {
94+ mockStatSync . mockReturnValueOnce ( {
95+ isDirectory : ( ) => false
96+ } as any ) ;
97+
98+ const [ , , warnings ] = generateStructure ( {
99+ path : '.' ,
100+ name : 'root' ,
101+ children : [ {
102+ name : 'real_item0' ,
103+ children : [ 'real_item2' ]
104+ } ]
105+ } , '/' ) ;
106+
107+ expect ( warnings . length ) . toEqual ( 1 ) ;
108+ expect ( fs . existsSync ) . toHaveBeenCalledTimes ( 3 ) ;
109+ } ) ;
110+
111+ test ( 'Not providing a path means no validation is run' , ( ) => {
112+ const [ , , warnings ] = generateStructure ( {
113+ path : '.' ,
114+ name : 'root' ,
115+ children : [ {
116+ name : 'real_item1' ,
117+ children : [ 'real_item2' ]
118+ } ]
119+ } ) ;
120+
121+ expect ( warnings . length ) . toEqual ( 0 ) ;
122+ expect ( fs . existsSync ) . not . toHaveBeenCalled ( ) ;
123+ expect ( fs . statSync ) . not . toHaveBeenCalled ( ) ;
124+ } ) ;
86125} ) ;
0 commit comments