|
| 1 | +import fs from 'fs'; |
| 2 | +import path from 'path'; |
| 3 | +import { toast } from 'react-toastify'; |
| 4 | +import { v4 as uuidv4 } from 'uuid'; |
| 5 | +import { getEntities, setEntities, getActiveLayerId, getActiveLineColor, getActiveLineWidth, setActiveLayerId, setActiveLineColor, setActiveLineWidth } from '../../state'; |
| 6 | +import { EntityType, type LineEntity, type CircleEntity } from '../../App.types'; |
| 7 | +import { importEntitiesFromDxfFile } from './import-entities-from-dxf'; |
| 8 | + |
| 9 | +// Mock react-toastify |
| 10 | +jest.mock('react-toastify', () => ({ |
| 11 | + toast: { |
| 12 | + success: jest.fn(), |
| 13 | + warn: jest.fn(), |
| 14 | + error: jest.fn(), |
| 15 | + info: jest.fn(), |
| 16 | + }, |
| 17 | +})); |
| 18 | + |
| 19 | +// Mock uuid to return predictable IDs |
| 20 | +let mockUuidCounter = 0; |
| 21 | +jest.mock('uuid', () => ({ |
| 22 | + v4: jest.fn(() => `mock-uuid-${mockUuidCounter++}`), |
| 23 | +})); |
| 24 | + |
| 25 | + |
| 26 | +describe('importEntitiesFromDxfFile', () => { |
| 27 | + const mockFilesBasePath = path.join(__dirname, '../../../../test/mocks/dxf'); |
| 28 | + let consoleLogSpy: jest.SpyInstance; |
| 29 | + let consoleWarnSpy: jest.SpyInstance; |
| 30 | + let consoleErrorSpy: jest.SpyInstance; |
| 31 | + |
| 32 | + const defaultLayerId = 'layer-dxf-default'; |
| 33 | + const defaultColor = '#FF00FF'; |
| 34 | + const defaultWidth = 3; |
| 35 | + |
| 36 | + beforeEach(() => { |
| 37 | + setEntities([]); |
| 38 | + mockUuidCounter = 0; |
| 39 | + (toast.success as jest.Mock).mockClear(); |
| 40 | + (toast.warn as jest.Mock).mockClear(); |
| 41 | + (toast.error as jest.Mock).mockClear(); |
| 42 | + (toast.info as jest.Mock).mockClear(); |
| 43 | + (uuidv4 as jest.Mock).mockClear(); // Clear mock usage counts |
| 44 | + |
| 45 | + // Set default active states for predictability in tests |
| 46 | + setActiveLayerId(defaultLayerId); |
| 47 | + setActiveLineColor(defaultColor); |
| 48 | + setActiveLineWidth(defaultWidth); |
| 49 | + |
| 50 | + // Spy on console methods |
| 51 | + consoleLogSpy = jest.spyOn(console, 'log').mockImplementation(() => {}); // Suppress log output during tests |
| 52 | + consoleWarnSpy = jest.spyOn(console, 'warn').mockImplementation(() => {}); |
| 53 | + consoleErrorSpy = jest.spyOn(console, 'error').mockImplementation(() => {}); |
| 54 | + }); |
| 55 | + |
| 56 | + afterEach(() => { |
| 57 | + // Restore console spies |
| 58 | + consoleLogSpy.mockRestore(); |
| 59 | + consoleWarnSpy.mockRestore(); |
| 60 | + consoleErrorSpy.mockRestore(); |
| 61 | + }); |
| 62 | + |
| 63 | + const loadMockDxfFile = (fileName: string): File => { |
| 64 | + const filePath = path.join(mockFilesBasePath, fileName); |
| 65 | + const fileContent = fs.readFileSync(filePath, 'utf-8'); |
| 66 | + return new File([fileContent], fileName, { type: 'application/dxf' }); |
| 67 | + }; |
| 68 | + |
| 69 | + it('should warn if no file is provided', async () => { |
| 70 | + await importEntitiesFromDxfFile(undefined); |
| 71 | + expect(getEntities()).toHaveLength(0); |
| 72 | + expect(toast.warn).toHaveBeenCalledWith('No DXF file selected.'); |
| 73 | + expect(toast.success).not.toHaveBeenCalled(); |
| 74 | + }); |
| 75 | + |
| 76 | + it('should import a single LINE entity from line.dxf', async () => { |
| 77 | + const file = loadMockDxfFile('line.dxf'); |
| 78 | + await importEntitiesFromDxfFile(file); |
| 79 | + |
| 80 | + const entities = getEntities(); |
| 81 | + expect(entities).toHaveLength(1); |
| 82 | + expect(toast.success).toHaveBeenCalledWith('1 entities imported successfully from DXF!'); |
| 83 | + |
| 84 | + const line = entities[0] as LineEntity; |
| 85 | + expect(line.type).toBe(EntityType.LINE); |
| 86 | + expect(line.id).toBe('mock-uuid-0'); |
| 87 | + expect(line.layerId).toBe(defaultLayerId); |
| 88 | + expect(line.start).toEqual({ x: 10.0, y: 10.0 }); |
| 89 | + expect(line.end).toEqual({ x: 20.0, y: 20.0 }); |
| 90 | + expect(line.color).toBe(defaultColor); // DXF color 256 (ByLayer) should use default |
| 91 | + expect(line.width).toBe(defaultWidth); |
| 92 | + }); |
| 93 | + |
| 94 | + it('should import a single CIRCLE entity from circle.dxf', async () => { |
| 95 | + const file = loadMockDxfFile('circle.dxf'); |
| 96 | + await importEntitiesFromDxfFile(file); |
| 97 | + |
| 98 | + const entities = getEntities(); |
| 99 | + expect(entities).toHaveLength(1); |
| 100 | + expect(toast.success).toHaveBeenCalledWith('1 entities imported successfully from DXF!'); |
| 101 | + |
| 102 | + const circle = entities[0] as CircleEntity; |
| 103 | + expect(circle.type).toBe(EntityType.CIRCLE); |
| 104 | + expect(circle.id).toBe('mock-uuid-0'); |
| 105 | + expect(circle.layerId).toBe(defaultLayerId); |
| 106 | + expect(circle.center).toEqual({ x: 30.0, y: 30.0 }); |
| 107 | + expect(circle.radius).toBe(5.0); |
| 108 | + expect(circle.color).toBe(defaultColor); |
| 109 | + expect(circle.width).toBe(defaultWidth); |
| 110 | + }); |
| 111 | + |
| 112 | + it('should import one LINE and one CIRCLE entity from line-and-circle.dxf', async () => { |
| 113 | + const file = loadMockDxfFile('line-and-circle.dxf'); |
| 114 | + await importEntitiesFromDxfFile(file); |
| 115 | + |
| 116 | + const entities = getEntities(); |
| 117 | + expect(entities).toHaveLength(2); |
| 118 | + expect(toast.success).toHaveBeenCalledWith('2 entities imported successfully from DXF!'); |
| 119 | + |
| 120 | + const line = entities.find(e => e.type === EntityType.LINE) as LineEntity; |
| 121 | + expect(line).toBeDefined(); |
| 122 | + expect(line.id).toBe('mock-uuid-0'); // First entity parsed |
| 123 | + expect(line.layerId).toBe(defaultLayerId); |
| 124 | + expect(line.start).toEqual({ x: 10.0, y: 10.0 }); |
| 125 | + expect(line.end).toEqual({ x: 20.0, y: 20.0 }); |
| 126 | + |
| 127 | + const circle = entities.find(e => e.type === EntityType.CIRCLE) as CircleEntity; |
| 128 | + expect(circle).toBeDefined(); |
| 129 | + expect(circle.id).toBe('mock-uuid-1'); // Second entity parsed |
| 130 | + expect(circle.layerId).toBe(defaultLayerId); |
| 131 | + expect(circle.center).toEqual({ x: 30.0, y: 30.0 }); |
| 132 | + expect(circle.radius).toBe(5.0); |
| 133 | + }); |
| 134 | + |
| 135 | + it('should import no entities from empty.dxf and show info toast', async () => { |
| 136 | + const file = loadMockDxfFile('empty.dxf'); |
| 137 | + await importEntitiesFromDxfFile(file); |
| 138 | + |
| 139 | + const entities = getEntities(); |
| 140 | + expect(entities).toHaveLength(0); |
| 141 | + expect(toast.info).toHaveBeenCalledWith('No supported entities found in the DXF file.'); |
| 142 | + expect(toast.success).not.toHaveBeenCalled(); |
| 143 | + }); |
| 144 | + |
| 145 | + it('should log a message for unsupported entities in unsupported.dxf', async () => { |
| 146 | + const file = loadMockDxfFile('unsupported.dxf'); |
| 147 | + await importEntitiesFromDxfFile(file); |
| 148 | + |
| 149 | + const entities = getEntities(); |
| 150 | + expect(entities).toHaveLength(0); // ARC is not supported |
| 151 | + expect(toast.info).toHaveBeenCalledWith('No supported entities found in the DXF file.'); |
| 152 | + // Check console.log because that's what the implementation uses for unsupported types |
| 153 | + expect(consoleLogSpy).toHaveBeenCalledWith('Unsupported DXF entity type: ARC. Skipping.'); |
| 154 | + }); |
| 155 | + |
| 156 | + it('should handle invalid DXF content and show error toast', async () => { |
| 157 | + const invalidFileContent = "this is not a dxf file"; |
| 158 | + const file = new File([invalidFileContent], 'invalid.dxf', { type: 'application/dxf' }); |
| 159 | + await importEntitiesFromDxfFile(file); |
| 160 | + |
| 161 | + const entities = getEntities(); |
| 162 | + expect(entities).toHaveLength(0); |
| 163 | + expect(toast.error).toHaveBeenCalledWith('An error occurred while parsing the DXF file. See console for details.'); |
| 164 | + expect(consoleErrorSpy).toHaveBeenCalled(); // Check if console.error was called for the parsing error |
| 165 | + }); |
| 166 | + |
| 167 | + // TODO: Add a test case for DXF files with specific color numbers to check color mapping, |
| 168 | + // once the color mapping logic is more sophisticated than just defaultColor. |
| 169 | + // For example, a LINE with color index 1 (red) should be mapped to '#FF0000'. |
| 170 | +}); |
0 commit comments