Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions src/functions/DocumentationGenerator.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import FileParsing from "./FileParsing";
import OutputTitle from "./OutputTitle";
import OutputNameFilePDF from "./OutputNameFilePDF";
import WritePDF from "./WritePDF";
import OutputTitleMarkdown from "./OutputTitleMarkdown";
import OutputNameFileMarkdown from "./OutputNameFileMarkdown";
import WriteMarkdown from "./WriteMarkdown";
const FileParsing = require('./FileParsing');
const OutputTitle = require('./OutputTitle');
const OutputNameFilePDF = require('./OutputNameFilePDF');
const WritePDF = require('./WritePDF');
const OutputTitleMarkdown = require('./OutputTitleMarkdown');
const OutputNameFileMarkdown = require('./OutputNameFileMarkdown');
const WriteMarkdown = require('./WriteMarkdown');
/* Doc:
explication du generateur de documentation
qui est vraiment super genial
Expand Down
10 changes: 2 additions & 8 deletions src/functions/FilterFiles.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,9 @@ const FilterFiles = async (files, extensions) => {
let filesArrayResult = [];

for(const fileHandle of files) {
try {
const file = await fileHandle.getFile();
const file = await fileHandle.getFile();

filesArray.push(file.name);

} catch (error) {

console.error(`Erreur avec le fichier :`, error);
}
filesArray.push(file.name);
}

for(let i = 0; i < filesArray.length; i++) {
Expand Down
2 changes: 1 addition & 1 deletion src/functions/WriteMarkdown.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { saveAs } from 'file-saver';
const { saveAs } = require('file-saver');

const WriteMarkdown = (outputTitleMarkdown, outputNameFileMarkdown, filesParsed) => {
const star = "*";
Expand Down
5 changes: 0 additions & 5 deletions src/pages/components/TextBoxExtensions.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,6 @@ const TextBoxExtensions = ({ setRows, formData, setFormData }) => {

setRows(filesArray);
};

/* Doc:
ceci est une autre doc
entre la 3eme et la 4eme
*/

return (
<TextField type="input" onChange={handleChange} value={formData?.extensionsName} label="Extensions" variant="outlined" placeholder="js, jsx, ..." fullWidth/>
Expand Down
104 changes: 104 additions & 0 deletions src/tests/DocumentationGenerator.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
const DocumentationGenerator = require('../functions/DocumentationGenerator');
const FileParsing = require('../functions/FileParsing');
const OutputTitle = require('../functions/OutputTitle');
const OutputNameFilePDF = require('../functions/OutputNameFilePDF');
const WritePDF = require('../functions/WritePDF');
const OutputTitleMarkdown = require('../functions/OutputTitleMarkdown');
const OutputNameFileMarkdown = require('../functions/OutputNameFileMarkdown');
const WriteMarkdown = require('../functions/WriteMarkdown');

jest.mock('../functions/FileParsing');
jest.mock('../functions/OutputTitle');
jest.mock('../functions/OutputNameFilePDF');
jest.mock('../functions/WritePDF');
jest.mock('../functions/OutputTitleMarkdown');
jest.mock('../functions/OutputNameFileMarkdown');
jest.mock('../functions/WriteMarkdown');

describe('DocumentationGenerator', () => {
it('should generate PDF documentation if format is pdf', async () => {
const formData = {
projectName: 'TestProject',
formats: { pdf: true, markdown: false }
};
const rows = [];
const filesParsed = [{ name: 'file1', functions: [] }];

FileParsing.mockResolvedValue(filesParsed);
OutputTitle.mockReturnValue('Test Project Documentation');
OutputNameFilePDF.mockReturnValue('TestProject.pdf');

await DocumentationGenerator(formData, rows);

expect(FileParsing).toHaveBeenCalledWith(formData, rows);
expect(OutputTitle).toHaveBeenCalledWith('TestProject');
expect(OutputNameFilePDF).toHaveBeenCalledWith('TestProject');
expect(WritePDF).toHaveBeenCalledWith('Test Project Documentation', 'TestProject.pdf', filesParsed);
});

it('should generate Markdown documentation if format is markdown', async () => {
const formData = {
projectName: 'TestProject',
formats: { pdf: false, markdown: true }
};
const rows = [];
const filesParsed = [{ name: 'file1', functions: [] }];

FileParsing.mockResolvedValue(filesParsed);
OutputTitleMarkdown.mockReturnValue('Test Project Documentation');
OutputNameFileMarkdown.mockReturnValue('TestProject.md');

await DocumentationGenerator(formData, rows);

expect(FileParsing).toHaveBeenCalledWith(formData, rows);
expect(OutputTitleMarkdown).toHaveBeenCalledWith('TestProject');
expect(OutputNameFileMarkdown).toHaveBeenCalledWith('TestProject');
expect(WriteMarkdown).toHaveBeenCalledWith('Test Project Documentation', 'TestProject.md', filesParsed);
});

it('should generate both PDF and Markdown documentation if both formats are specified', async () => {
const formData = {
projectName: 'TestProject',
formats: { pdf: true, markdown: true }
};
const rows = [];
const filesParsed = [{ name: 'file1', functions: [] }];

FileParsing.mockResolvedValue(filesParsed);
OutputTitle.mockReturnValue('Test Project Documentation');
OutputNameFilePDF.mockReturnValue('TestProject.pdf');
OutputTitleMarkdown.mockReturnValue('Test Project Documentation');
OutputNameFileMarkdown.mockReturnValue('TestProject.md');

await DocumentationGenerator(formData, rows);

expect(FileParsing).toHaveBeenCalledWith(formData, rows);
expect(OutputTitle).toHaveBeenCalledWith('TestProject');
expect(OutputNameFilePDF).toHaveBeenCalledWith('TestProject');
expect(WritePDF).toHaveBeenCalledWith('Test Project Documentation', 'TestProject.pdf', filesParsed);
expect(OutputTitleMarkdown).toHaveBeenCalledWith('TestProject');
expect(OutputNameFileMarkdown).toHaveBeenCalledWith('TestProject');
expect(WriteMarkdown).toHaveBeenCalledWith('Test Project Documentation', 'TestProject.md', filesParsed);
});

it('should handle empty formats gracefully', async () => {
const formData = {
projectName: 'TestProject',
formats: {}
};
const rows = [];
const filesParsed = [{ name: 'file1', functions: [] }];

FileParsing.mockResolvedValue(filesParsed);

await DocumentationGenerator(formData, rows);

expect(FileParsing).toHaveBeenCalledWith(formData, rows);
expect(OutputTitle).not.toHaveBeenCalled();
expect(OutputNameFilePDF).not.toHaveBeenCalled();
expect(WritePDF).not.toHaveBeenCalled();
expect(OutputTitleMarkdown).not.toHaveBeenCalled();
expect(OutputNameFileMarkdown).not.toHaveBeenCalled();
expect(WriteMarkdown).not.toHaveBeenCalled();
});
});
146 changes: 146 additions & 0 deletions src/tests/FileParsing.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
const FileParsing = require('../functions/FileParsing');

describe('FileParsing', () => {
it('should parse files and extract comments correctly', async () => {
const formData = {
files: [
{
name: 'testFile.js',
getFile: async () => ({
text: async () => `
//doc: This is a test comment
const testFunction = () => {};
/*doc: This is another test comment */
`
})
}
]
};

const rows = [
{ file: 'testFile' }
];

const expectedOutput = [
{
name: 'testFile',
functions: [
{
functionName: 'testFunction',
comments: [
'This is a test comment',
'This is another test comment'
]
}
]
}
];

const result = await FileParsing(formData, rows);
expect(result).toEqual(expectedOutput);
});

it('should return an empty array if no matching files are found', async () => {
const formData = {
files: [
{
name: 'nonMatchingFile.js',
getFile: async () => ({
text: async () => ``
})
}
]
};

const rows = [
{ file: 'testFile' }
];

const result = await FileParsing(formData, rows);
expect(result).toEqual([]);
});

it('should handle different comment formats correctly', async () => {
const formData = {
files: [
{
name: 'testFile.js',
getFile: async () => ({
text: async () => `
// Doc: Line comment with space
/*Doc: Block comment with space */
//doc: Line comment without space
/*doc: Block comment without space */
const anotherFunction = () => {};
`
})
}
]
};

const rows = [
{ file: 'testFile' }
];

const expectedOutput = [
{
name: 'testFile',
functions: [
{
functionName: 'anotherFunction',
comments: [
'Line comment with space',
'Block comment with space',
'Line comment without space',
'Block comment without space'
]
}
]
}
];

const result = await FileParsing(formData, rows);
expect(result).toEqual(expectedOutput);
});

it('should handle multiple functions in a single file', async () => {
const formData = {
files: [
{
name: 'multiFunctionFile.js',
getFile: async () => ({
text: async () => `
//doc: Comment for first function
const firstFunction = () => {};
//doc: Comment for second function
const secondFunction = () => {};
`
})
}
]
};

const rows = [
{ file: 'multiFunctionFile' }
];

const expectedOutput = [
{
name: 'multiFunctionFile',
functions: [
{
functionName: 'firstFunction',
comments: ['Comment for first function']
},
{
functionName: 'secondFunction',
comments: ['Comment for second function']
}
]
}
];

const result = await FileParsing(formData, rows);
expect(result).toEqual(expectedOutput);
});
});
44 changes: 44 additions & 0 deletions src/tests/FilesArraySelection.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
const FilesArraySelection = require('../functions/FilesArraySelection');

describe('FilesArraySelection', () => {
it('should correctly map files to rows with file names and extensions', () => {
const files = [
{ name: 'file1.txt' },
{ name: 'file2.js' },
{ name: 'file3' }
];

const expectedOutput = [
{ file: 'file1', extension: 'txt' },
{ file: 'file2', extension: 'js' },
{ file: 'file3', extension: '' }
];

const result = FilesArraySelection(files);
expect(result).toEqual(expectedOutput);
});

it('should handle files with multiple dots in the name', () => {
const files = [
{ name: 'file.name.with.dots.txt' },
{ name: 'another.file.name.js' }
];

const expectedOutput = [
{ file: 'file.name.with.dots', extension: 'txt' },
{ file: 'another.file.name', extension: 'js' }
];

const result = FilesArraySelection(files);
expect(result).toEqual(expectedOutput);
});

it('should return an empty array if no files are provided', () => {
const files = [];

const expectedOutput = [];

const result = FilesArraySelection(files);
expect(result).toEqual(expectedOutput);
});
});
22 changes: 22 additions & 0 deletions src/tests/FilesSelector.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
const FilesSelector = require('../functions/FilesSelector');

describe('FilesSelector', () => {
it('should return selected files', async () => {
const mockFilesHandle = [
{ name: 'file1.txt' },
{ name: 'file2.js' }
];

window.showOpenFilePicker = jest.fn().mockResolvedValue(mockFilesHandle);

const result = await FilesSelector();
expect(result).toEqual(mockFilesHandle);
});

it('should return an empty array if an error occurs', async () => {
window.showOpenFilePicker = jest.fn().mockRejectedValue(new Error('Test error'));

const result = await FilesSelector();
expect(result).toEqual([]);
});
});
Loading
Loading