diff --git a/src/functions/DocumentationGenerator.js b/src/functions/DocumentationGenerator.js index 41488a7..dc85bde 100644 --- a/src/functions/DocumentationGenerator.js +++ b/src/functions/DocumentationGenerator.js @@ -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 diff --git a/src/functions/FilterFiles.js b/src/functions/FilterFiles.js index 6026b6b..3b62317 100644 --- a/src/functions/FilterFiles.js +++ b/src/functions/FilterFiles.js @@ -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++) { diff --git a/src/functions/WriteMarkdown.js b/src/functions/WriteMarkdown.js index 6b3caa0..62dbe5b 100644 --- a/src/functions/WriteMarkdown.js +++ b/src/functions/WriteMarkdown.js @@ -1,4 +1,4 @@ -import { saveAs } from 'file-saver'; +const { saveAs } = require('file-saver'); const WriteMarkdown = (outputTitleMarkdown, outputNameFileMarkdown, filesParsed) => { const star = "*"; diff --git a/src/pages/components/TextBoxExtensions.jsx b/src/pages/components/TextBoxExtensions.jsx index b43d47e..b5f6e46 100644 --- a/src/pages/components/TextBoxExtensions.jsx +++ b/src/pages/components/TextBoxExtensions.jsx @@ -16,11 +16,6 @@ const TextBoxExtensions = ({ setRows, formData, setFormData }) => { setRows(filesArray); }; - - /* Doc: - ceci est une autre doc - entre la 3eme et la 4eme - */ return ( diff --git a/src/tests/DocumentationGenerator.test.js b/src/tests/DocumentationGenerator.test.js new file mode 100644 index 0000000..6c5f373 --- /dev/null +++ b/src/tests/DocumentationGenerator.test.js @@ -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(); + }); +}); \ No newline at end of file diff --git a/src/tests/FileParsing.test.js b/src/tests/FileParsing.test.js new file mode 100644 index 0000000..138fe20 --- /dev/null +++ b/src/tests/FileParsing.test.js @@ -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); + }); +}); \ No newline at end of file diff --git a/src/tests/FilesArraySelection.test.js b/src/tests/FilesArraySelection.test.js new file mode 100644 index 0000000..125b049 --- /dev/null +++ b/src/tests/FilesArraySelection.test.js @@ -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); + }); +}); \ No newline at end of file diff --git a/src/tests/FilesSelector.test.js b/src/tests/FilesSelector.test.js new file mode 100644 index 0000000..9217fce --- /dev/null +++ b/src/tests/FilesSelector.test.js @@ -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([]); + }); +}); \ No newline at end of file diff --git a/src/tests/FilterFiles.test.js b/src/tests/FilterFiles.test.js new file mode 100644 index 0000000..13ca4ee --- /dev/null +++ b/src/tests/FilterFiles.test.js @@ -0,0 +1,70 @@ +const FilterFiles = require('../functions/FilterFiles'); + +describe('FilterFiles', () => { + it('should filter files based on given extensions', async () => { + const files = [ + { + name: 'file1.txt', + getFile: async () => ({ name: 'file1.txt' }) + }, + { + name: 'file2.js', + getFile: async () => ({ name: 'file2.js' }) + }, + { + name: 'file3.md', + getFile: async () => ({ name: 'file3.md' }) + } + ]; + + const extensions = ['txt', 'js']; + + const expectedOutput = [ + { file: 'file1', extension: 'txt' }, + { file: 'file2', extension: 'js' } + ]; + + const result = await FilterFiles(files, extensions); + expect(result).toEqual(expectedOutput); + }); + + it('should return an empty array if no files match the given extensions', async () => { + const files = [ + { + name: 'file1.txt', + getFile: async () => ({ name: 'file1.txt' }) + }, + { + name: 'file2.js', + getFile: async () => ({ name: 'file2.js' }) + } + ]; + + const extensions = ['md']; + + const result = await FilterFiles(files, extensions); + expect(result).toEqual([]); + }); + + it('should handle errors gracefully', async () => { + const files = [ + { + name: 'file1.txt', + getFile: async () => { throw new Error('Test error'); } + }, + { + name: 'file2.js', + getFile: async () => ({ name: 'file2.js' }) + } + ]; + + const extensions = ['js']; + + const expectedOutput = [ + { file: 'file2', extension: 'js' } + ]; + + const result = await FilterFiles(files, extensions); + expect(result).toEqual(expectedOutput); + }); +}); \ No newline at end of file diff --git a/src/tests/FolderSelector.test.js b/src/tests/FolderSelector.test.js new file mode 100644 index 0000000..6e546bb --- /dev/null +++ b/src/tests/FolderSelector.test.js @@ -0,0 +1,24 @@ +const FolderSelector = require('../functions/FolderSelector'); +const GetAllFiles = require('../functions/GetAllFiles'); + +jest.mock('../functions/GetAllFiles'); + +describe('FolderSelector', () => { + it('should return all files from the selected directory', async () => { + const mockFiles = [{ kind: 'file', name: 'file1.txt' }]; + const mockDirectoryHandle = { kind: 'directory' }; + + window.showDirectoryPicker = jest.fn().mockResolvedValue(mockDirectoryHandle); + GetAllFiles.mockResolvedValue(mockFiles); + + const result = await FolderSelector(); + expect(result).toEqual(mockFiles); + }); + + it('should return an empty array if an error occurs', async () => { + window.showDirectoryPicker = jest.fn().mockRejectedValue(new Error('Test error')); + + const result = await FolderSelector(); + expect(result).toEqual([]); + }); +}); \ No newline at end of file diff --git a/src/tests/GetAllFiles.test.js b/src/tests/GetAllFiles.test.js new file mode 100644 index 0000000..958d59d --- /dev/null +++ b/src/tests/GetAllFiles.test.js @@ -0,0 +1,44 @@ +/* const GetAllFiles = require('../functions/GetAllFiles'); + +describe('GetAllFiles', () => { + it('should retrieve all files from a directory', async () => { + const mockFile = { kind: 'file', name: 'file1.txt' }; + const mockDirectory = { + kind: 'directory', + values: jest.fn().mockResolvedValue([ + mockFile, + { + kind: 'directory', + values: jest.fn().mockResolvedValue([mockFile]) + } + ]) + }; + + const files = []; + const result = await GetAllFiles(mockDirectory, files); + + expect(result).toEqual([mockFile, mockFile]); + }); + + it('should handle an empty directory', async () => { + const mockDirectory = { + kind: 'directory', + values: jest.fn().mockResolvedValue([]) + }; + + const files = []; + const result = await GetAllFiles(mockDirectory, files); + + expect(result).toEqual([]); + }); + + it('should handle errors gracefully', async () => { + const mockDirectory = { + kind: 'directory', + values: jest.fn().mockRejectedValue('Test error') + }; + + const files = []; + await expect(GetAllFiles(mockDirectory, files)).rejects.toThrow('Test error'); + }); +}); */ \ No newline at end of file diff --git a/src/tests/WriteMarkdown.test.js b/src/tests/WriteMarkdown.test.js new file mode 100644 index 0000000..5056cf8 --- /dev/null +++ b/src/tests/WriteMarkdown.test.js @@ -0,0 +1,55 @@ +const WriteMarkdown = require('../functions/WriteMarkdown'); +const { saveAs } = require('file-saver'); + +jest.mock('file-saver', () => ({ + saveAs: jest.fn() +})); + +describe('WriteMarkdown', () => { + it('should generate markdown content and save it as a file', () => { + const outputTitleMarkdown = '# Documentation'; + const outputNameFileMarkdown = 'output.md'; + const filesParsed = [ + { + name: 'file1', + functions: [ + { + functionName: 'function1', + comments: ['Comment 1', 'Comment 2'] + } + ] + }, + { + name: 'file2', + functions: [ + { + functionName: 'function2', + comments: ['Comment 3'] + } + ] + } + ]; + + WriteMarkdown(outputTitleMarkdown, outputNameFileMarkdown, filesParsed); + + const expectedContent = `# Documentation +* __file1__ : + * **Fonction function1** : + * Comment 1,Comment 2 +* __file2__ : + * **Fonction function2** : + * Comment 3`; + + expect(saveAs).toHaveBeenCalledWith( + expect.any(Blob), + outputNameFileMarkdown + ); + + const blob = saveAs.mock.calls[0][0]; + const reader = new FileReader(); + reader.onload = () => { + expect(reader.result).toBe(expectedContent); + }; + reader.readAsText(blob); + }); +}); \ No newline at end of file diff --git a/src/tests/WritePDF.test.js b/src/tests/WritePDF.test.js new file mode 100644 index 0000000..050d97f --- /dev/null +++ b/src/tests/WritePDF.test.js @@ -0,0 +1,60 @@ +const WritePDF = require('../functions/WritePDF'); +const { jsPDF } = require('jspdf'); + +jest.mock('jspdf', () => { + const jsPDFMock = jest.fn().mockImplementation(() => ({ + setFontSize: jest.fn(), + setFont: jest.fn(), + text: jest.fn(), + addPage: jest.fn(), + splitTextToSize: jest.fn().mockReturnValue([]), + save: jest.fn() + })); + return { jsPDF: jsPDFMock }; +}); + +describe('WritePDF', () => { + it('should generate a PDF with the correct content', () => { + const outputTitle = 'Documentation'; + const outputNameFilePDF = 'output.pdf'; + const filesParsed = [ + { + name: 'file1', + functions: [ + { + functionName: 'function1', + comments: ['Comment 1', 'Comment 2'] + } + ] + }, + { + name: 'file2', + functions: [ + { + functionName: 'function2', + comments: ['Comment 3'] + } + ] + } + ]; + + WritePDF(outputTitle, outputNameFilePDF, filesParsed); + + const doc = jsPDF.mock.instances[0]; + + expect(doc.setFontSize).toHaveBeenCalledWith(22); + expect(doc.setFont).toHaveBeenCalledWith('Helvetica', 'Bold'); + expect(doc.text).toHaveBeenCalledWith(outputTitle, 10, 20); + expect(doc.setFontSize).toHaveBeenCalledWith(16); + + expect(doc.text).toHaveBeenCalledWith('- file1 :', 20, 40); + expect(doc.text).toHaveBeenCalledWith('Fonction function1 : ', 30, 50); + expect(doc.splitTextToSize).toHaveBeenCalledWith(['Comment 1', 'Comment 2'], 160); + + expect(doc.text).toHaveBeenCalledWith('- file2 :', 20, expect.any(Number)); + expect(doc.text).toHaveBeenCalledWith('Fonction function2 : ', 30, expect.any(Number)); + expect(doc.splitTextToSize).toHaveBeenCalledWith(['Comment 3'], 160); + + expect(doc.save).toHaveBeenCalledWith(outputNameFilePDF); + }); +}); \ No newline at end of file