@@ -6,50 +6,48 @@ vi.mock('fs');
6
6
7
7
const filePath = path . join ( 'docs' , 'docs' , 'auto-docs' , 'test.md' ) ;
8
8
9
- // Mock file content with README.md links
10
- const mockFileContent = `
11
- This is a test file.
12
- [Some Link](./README.md)
13
- More content here.
14
- ` ;
15
-
16
- const expectedFileContent = `
17
- This is a test file.
18
- [Admin Docs](/)
19
- More content here.
20
- ` ;
9
+ // Move the function outside the test
10
+ const replaceLinks = ( filePath : string ) => {
11
+ let content = fs . readFileSync ( filePath , 'utf8' ) as string ;
12
+ content = content . replace ( / \[ .* ?\] \( ( .* ?) R E A D M E \. m d \) / g, ( ) => {
13
+ return '[Admin Docs](/)' ;
14
+ } ) ;
15
+ fs . writeFileSync ( filePath , content , 'utf8' ) ;
16
+ return content ;
17
+ } ;
21
18
22
19
describe ( 'fix-readme-links.js' , ( ) => {
23
20
beforeEach ( ( ) => {
24
- vi . restoreAllMocks ( ) ; // Reset mocks before each test
25
- vi . spyOn ( fs , 'readFileSync' ) . mockReturnValue ( mockFileContent ) ;
26
- vi . spyOn ( fs , 'writeFileSync' ) . mockImplementation ( ( ) => { } ) ; // Mock writeFileSync
21
+ vi . restoreAllMocks ( ) ;
22
+ } ) ;
23
+
24
+ it ( 'should replace README.md links with Admin Docs link' , ( ) => {
25
+ // Test case 1: Basic replacement
26
+ const mockContent1 = '[Some Link](./README.md)' ;
27
+ vi . spyOn ( fs , 'readFileSync' ) . mockReturnValue ( mockContent1 ) ;
28
+ vi . spyOn ( fs , 'writeFileSync' ) . mockImplementation ( ( ) => { } ) ;
29
+
30
+ const result = replaceLinks ( filePath ) ;
31
+ expect ( result ) . toBe ( '[Admin Docs](/)' ) ;
27
32
} ) ;
28
33
29
- it ( 'should read the file, replace README.md links, and write the modified content' , ( ) => {
30
- const replaceLinks = ( filePath : string ) => {
31
- // Read file
32
- let content = fs . readFileSync ( filePath , 'utf8' ) as string ;
33
- expect ( content ) . toBe ( mockFileContent ) ; // Ensures readFileSync returns expected content
34
-
35
- // Perform replacement
36
- content = content . replace ( / \[ .* ?\] \( ( .* ?) R E A D M E \. m d \) / g, ( ) => {
37
- return '[Admin Docs](/)' ;
38
- } ) ;
39
- expect ( content ) . toBe ( expectedFileContent ) ; // Ensures replacement occurred
40
-
41
- // Write file
42
- fs . writeFileSync ( filePath , content , 'utf8' ) ;
43
- } ;
44
-
45
- replaceLinks ( filePath ) ;
46
-
47
- // Assertions to confirm function execution
48
- expect ( fs . readFileSync ) . toHaveBeenCalledWith ( filePath , 'utf8' ) ;
49
- expect ( fs . writeFileSync ) . toHaveBeenCalledWith (
50
- filePath ,
51
- expectedFileContent ,
52
- 'utf8' ,
53
- ) ;
34
+ it ( 'should handle multiple README.md links' , ( ) => {
35
+ // Test case 2: Multiple replacements
36
+ const mockContent2 = '[Link1](README.md)[Link2](./docs/README.md)' ;
37
+ vi . spyOn ( fs , 'readFileSync' ) . mockReturnValue ( mockContent2 ) ;
38
+ vi . spyOn ( fs , 'writeFileSync' ) . mockImplementation ( ( ) => { } ) ;
39
+
40
+ const result = replaceLinks ( filePath ) ;
41
+ expect ( result ) . toBe ( '[Admin Docs](/)[Admin Docs](/)' ) ;
42
+ } ) ;
43
+
44
+ it ( 'should handle content with no README.md links' , ( ) => {
45
+ // Test case 3: No replacements needed
46
+ const mockContent3 = 'No links here' ;
47
+ vi . spyOn ( fs , 'readFileSync' ) . mockReturnValue ( mockContent3 ) ;
48
+ vi . spyOn ( fs , 'writeFileSync' ) . mockImplementation ( ( ) => { } ) ;
49
+
50
+ const result = replaceLinks ( filePath ) ;
51
+ expect ( result ) . toBe ( 'No links here' ) ;
54
52
} ) ;
55
53
} ) ;
0 commit comments