Skip to content

Commit c454fed

Browse files
authored
Merge pull request #20 from thom-cr/adding_more_tests
Creation of tests for the project coverage
2 parents a29ed78 + 9b9a018 commit c454fed

13 files changed

+579
-21
lines changed

src/functions/DocumentationGenerator.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
import FileParsing from "./FileParsing";
2-
import OutputTitle from "./OutputTitle";
3-
import OutputNameFilePDF from "./OutputNameFilePDF";
4-
import WritePDF from "./WritePDF";
5-
import OutputTitleMarkdown from "./OutputTitleMarkdown";
6-
import OutputNameFileMarkdown from "./OutputNameFileMarkdown";
7-
import WriteMarkdown from "./WriteMarkdown";
1+
const FileParsing = require('./FileParsing');
2+
const OutputTitle = require('./OutputTitle');
3+
const OutputNameFilePDF = require('./OutputNameFilePDF');
4+
const WritePDF = require('./WritePDF');
5+
const OutputTitleMarkdown = require('./OutputTitleMarkdown');
6+
const OutputNameFileMarkdown = require('./OutputNameFileMarkdown');
7+
const WriteMarkdown = require('./WriteMarkdown');
88
/* Doc:
99
explication du generateur de documentation
1010
qui est vraiment super genial

src/functions/FilterFiles.js

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,9 @@ const FilterFiles = async (files, extensions) => {
33
let filesArrayResult = [];
44

55
for(const fileHandle of files) {
6-
try {
7-
const file = await fileHandle.getFile();
6+
const file = await fileHandle.getFile();
87

9-
filesArray.push(file.name);
10-
11-
} catch (error) {
12-
13-
console.error(`Erreur avec le fichier :`, error);
14-
}
8+
filesArray.push(file.name);
159
}
1610

1711
for(let i = 0; i < filesArray.length; i++) {

src/functions/WriteMarkdown.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { saveAs } from 'file-saver';
1+
const { saveAs } = require('file-saver');
22

33
const WriteMarkdown = (outputTitleMarkdown, outputNameFileMarkdown, filesParsed) => {
44
const star = "*";

src/pages/components/TextBoxExtensions.jsx

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,6 @@ const TextBoxExtensions = ({ setRows, formData, setFormData }) => {
1616

1717
setRows(filesArray);
1818
};
19-
20-
/* Doc:
21-
ceci est une autre doc
22-
entre la 3eme et la 4eme
23-
*/
2419

2520
return (
2621
<TextField type="input" onChange={handleChange} value={formData?.extensionsName} label="Extensions" variant="outlined" placeholder="js, jsx, ..." fullWidth/>
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
const DocumentationGenerator = require('../functions/DocumentationGenerator');
2+
const FileParsing = require('../functions/FileParsing');
3+
const OutputTitle = require('../functions/OutputTitle');
4+
const OutputNameFilePDF = require('../functions/OutputNameFilePDF');
5+
const WritePDF = require('../functions/WritePDF');
6+
const OutputTitleMarkdown = require('../functions/OutputTitleMarkdown');
7+
const OutputNameFileMarkdown = require('../functions/OutputNameFileMarkdown');
8+
const WriteMarkdown = require('../functions/WriteMarkdown');
9+
10+
jest.mock('../functions/FileParsing');
11+
jest.mock('../functions/OutputTitle');
12+
jest.mock('../functions/OutputNameFilePDF');
13+
jest.mock('../functions/WritePDF');
14+
jest.mock('../functions/OutputTitleMarkdown');
15+
jest.mock('../functions/OutputNameFileMarkdown');
16+
jest.mock('../functions/WriteMarkdown');
17+
18+
describe('DocumentationGenerator', () => {
19+
it('should generate PDF documentation if format is pdf', async () => {
20+
const formData = {
21+
projectName: 'TestProject',
22+
formats: { pdf: true, markdown: false }
23+
};
24+
const rows = [];
25+
const filesParsed = [{ name: 'file1', functions: [] }];
26+
27+
FileParsing.mockResolvedValue(filesParsed);
28+
OutputTitle.mockReturnValue('Test Project Documentation');
29+
OutputNameFilePDF.mockReturnValue('TestProject.pdf');
30+
31+
await DocumentationGenerator(formData, rows);
32+
33+
expect(FileParsing).toHaveBeenCalledWith(formData, rows);
34+
expect(OutputTitle).toHaveBeenCalledWith('TestProject');
35+
expect(OutputNameFilePDF).toHaveBeenCalledWith('TestProject');
36+
expect(WritePDF).toHaveBeenCalledWith('Test Project Documentation', 'TestProject.pdf', filesParsed);
37+
});
38+
39+
it('should generate Markdown documentation if format is markdown', async () => {
40+
const formData = {
41+
projectName: 'TestProject',
42+
formats: { pdf: false, markdown: true }
43+
};
44+
const rows = [];
45+
const filesParsed = [{ name: 'file1', functions: [] }];
46+
47+
FileParsing.mockResolvedValue(filesParsed);
48+
OutputTitleMarkdown.mockReturnValue('Test Project Documentation');
49+
OutputNameFileMarkdown.mockReturnValue('TestProject.md');
50+
51+
await DocumentationGenerator(formData, rows);
52+
53+
expect(FileParsing).toHaveBeenCalledWith(formData, rows);
54+
expect(OutputTitleMarkdown).toHaveBeenCalledWith('TestProject');
55+
expect(OutputNameFileMarkdown).toHaveBeenCalledWith('TestProject');
56+
expect(WriteMarkdown).toHaveBeenCalledWith('Test Project Documentation', 'TestProject.md', filesParsed);
57+
});
58+
59+
it('should generate both PDF and Markdown documentation if both formats are specified', async () => {
60+
const formData = {
61+
projectName: 'TestProject',
62+
formats: { pdf: true, markdown: true }
63+
};
64+
const rows = [];
65+
const filesParsed = [{ name: 'file1', functions: [] }];
66+
67+
FileParsing.mockResolvedValue(filesParsed);
68+
OutputTitle.mockReturnValue('Test Project Documentation');
69+
OutputNameFilePDF.mockReturnValue('TestProject.pdf');
70+
OutputTitleMarkdown.mockReturnValue('Test Project Documentation');
71+
OutputNameFileMarkdown.mockReturnValue('TestProject.md');
72+
73+
await DocumentationGenerator(formData, rows);
74+
75+
expect(FileParsing).toHaveBeenCalledWith(formData, rows);
76+
expect(OutputTitle).toHaveBeenCalledWith('TestProject');
77+
expect(OutputNameFilePDF).toHaveBeenCalledWith('TestProject');
78+
expect(WritePDF).toHaveBeenCalledWith('Test Project Documentation', 'TestProject.pdf', filesParsed);
79+
expect(OutputTitleMarkdown).toHaveBeenCalledWith('TestProject');
80+
expect(OutputNameFileMarkdown).toHaveBeenCalledWith('TestProject');
81+
expect(WriteMarkdown).toHaveBeenCalledWith('Test Project Documentation', 'TestProject.md', filesParsed);
82+
});
83+
84+
it('should handle empty formats gracefully', async () => {
85+
const formData = {
86+
projectName: 'TestProject',
87+
formats: {}
88+
};
89+
const rows = [];
90+
const filesParsed = [{ name: 'file1', functions: [] }];
91+
92+
FileParsing.mockResolvedValue(filesParsed);
93+
94+
await DocumentationGenerator(formData, rows);
95+
96+
expect(FileParsing).toHaveBeenCalledWith(formData, rows);
97+
expect(OutputTitle).not.toHaveBeenCalled();
98+
expect(OutputNameFilePDF).not.toHaveBeenCalled();
99+
expect(WritePDF).not.toHaveBeenCalled();
100+
expect(OutputTitleMarkdown).not.toHaveBeenCalled();
101+
expect(OutputNameFileMarkdown).not.toHaveBeenCalled();
102+
expect(WriteMarkdown).not.toHaveBeenCalled();
103+
});
104+
});

src/tests/FileParsing.test.js

Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
const FileParsing = require('../functions/FileParsing');
2+
3+
describe('FileParsing', () => {
4+
it('should parse files and extract comments correctly', async () => {
5+
const formData = {
6+
files: [
7+
{
8+
name: 'testFile.js',
9+
getFile: async () => ({
10+
text: async () => `
11+
//doc: This is a test comment
12+
const testFunction = () => {};
13+
/*doc: This is another test comment */
14+
`
15+
})
16+
}
17+
]
18+
};
19+
20+
const rows = [
21+
{ file: 'testFile' }
22+
];
23+
24+
const expectedOutput = [
25+
{
26+
name: 'testFile',
27+
functions: [
28+
{
29+
functionName: 'testFunction',
30+
comments: [
31+
'This is a test comment',
32+
'This is another test comment'
33+
]
34+
}
35+
]
36+
}
37+
];
38+
39+
const result = await FileParsing(formData, rows);
40+
expect(result).toEqual(expectedOutput);
41+
});
42+
43+
it('should return an empty array if no matching files are found', async () => {
44+
const formData = {
45+
files: [
46+
{
47+
name: 'nonMatchingFile.js',
48+
getFile: async () => ({
49+
text: async () => ``
50+
})
51+
}
52+
]
53+
};
54+
55+
const rows = [
56+
{ file: 'testFile' }
57+
];
58+
59+
const result = await FileParsing(formData, rows);
60+
expect(result).toEqual([]);
61+
});
62+
63+
it('should handle different comment formats correctly', async () => {
64+
const formData = {
65+
files: [
66+
{
67+
name: 'testFile.js',
68+
getFile: async () => ({
69+
text: async () => `
70+
// Doc: Line comment with space
71+
/*Doc: Block comment with space */
72+
//doc: Line comment without space
73+
/*doc: Block comment without space */
74+
const anotherFunction = () => {};
75+
`
76+
})
77+
}
78+
]
79+
};
80+
81+
const rows = [
82+
{ file: 'testFile' }
83+
];
84+
85+
const expectedOutput = [
86+
{
87+
name: 'testFile',
88+
functions: [
89+
{
90+
functionName: 'anotherFunction',
91+
comments: [
92+
'Line comment with space',
93+
'Block comment with space',
94+
'Line comment without space',
95+
'Block comment without space'
96+
]
97+
}
98+
]
99+
}
100+
];
101+
102+
const result = await FileParsing(formData, rows);
103+
expect(result).toEqual(expectedOutput);
104+
});
105+
106+
it('should handle multiple functions in a single file', async () => {
107+
const formData = {
108+
files: [
109+
{
110+
name: 'multiFunctionFile.js',
111+
getFile: async () => ({
112+
text: async () => `
113+
//doc: Comment for first function
114+
const firstFunction = () => {};
115+
//doc: Comment for second function
116+
const secondFunction = () => {};
117+
`
118+
})
119+
}
120+
]
121+
};
122+
123+
const rows = [
124+
{ file: 'multiFunctionFile' }
125+
];
126+
127+
const expectedOutput = [
128+
{
129+
name: 'multiFunctionFile',
130+
functions: [
131+
{
132+
functionName: 'firstFunction',
133+
comments: ['Comment for first function']
134+
},
135+
{
136+
functionName: 'secondFunction',
137+
comments: ['Comment for second function']
138+
}
139+
]
140+
}
141+
];
142+
143+
const result = await FileParsing(formData, rows);
144+
expect(result).toEqual(expectedOutput);
145+
});
146+
});
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
const FilesArraySelection = require('../functions/FilesArraySelection');
2+
3+
describe('FilesArraySelection', () => {
4+
it('should correctly map files to rows with file names and extensions', () => {
5+
const files = [
6+
{ name: 'file1.txt' },
7+
{ name: 'file2.js' },
8+
{ name: 'file3' }
9+
];
10+
11+
const expectedOutput = [
12+
{ file: 'file1', extension: 'txt' },
13+
{ file: 'file2', extension: 'js' },
14+
{ file: 'file3', extension: '' }
15+
];
16+
17+
const result = FilesArraySelection(files);
18+
expect(result).toEqual(expectedOutput);
19+
});
20+
21+
it('should handle files with multiple dots in the name', () => {
22+
const files = [
23+
{ name: 'file.name.with.dots.txt' },
24+
{ name: 'another.file.name.js' }
25+
];
26+
27+
const expectedOutput = [
28+
{ file: 'file.name.with.dots', extension: 'txt' },
29+
{ file: 'another.file.name', extension: 'js' }
30+
];
31+
32+
const result = FilesArraySelection(files);
33+
expect(result).toEqual(expectedOutput);
34+
});
35+
36+
it('should return an empty array if no files are provided', () => {
37+
const files = [];
38+
39+
const expectedOutput = [];
40+
41+
const result = FilesArraySelection(files);
42+
expect(result).toEqual(expectedOutput);
43+
});
44+
});

src/tests/FilesSelector.test.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
const FilesSelector = require('../functions/FilesSelector');
2+
3+
describe('FilesSelector', () => {
4+
it('should return selected files', async () => {
5+
const mockFilesHandle = [
6+
{ name: 'file1.txt' },
7+
{ name: 'file2.js' }
8+
];
9+
10+
window.showOpenFilePicker = jest.fn().mockResolvedValue(mockFilesHandle);
11+
12+
const result = await FilesSelector();
13+
expect(result).toEqual(mockFilesHandle);
14+
});
15+
16+
it('should return an empty array if an error occurs', async () => {
17+
window.showOpenFilePicker = jest.fn().mockRejectedValue(new Error('Test error'));
18+
19+
const result = await FilesSelector();
20+
expect(result).toEqual([]);
21+
});
22+
});

0 commit comments

Comments
 (0)