Refactored tests

This commit is contained in:
Nick Playfair 2025-06-13 22:02:55 +01:00
parent 30d873048b
commit 809f1204b6

View File

@ -18,13 +18,21 @@ const noTempConfig = {
imgDir: path.join(__dirname, 'tmp'),
};
const tmpNotExist = {
tmpDir: path.join(__dirname, 'layers', 'InvalidFolderName'),
tmpDir: path.join(__dirname, 'InvalidFolderName'),
imgDir: path.join(__dirname, 'tmp'),
};
const imgNotExist = {
tmpDir: path.join(__dirname, 'tmp'),
imgDir: path.join(__dirname, 'InvalidFolderName'),
};
const tmpBadPerms = {
tmpDir: path.join(__dirname, 'badPerms'),
imgDir: path.join(__dirname, 'tmp'),
};
const imgBadPerms = {
tmpDir: path.join(__dirname, 'tmp'),
imgDir: path.join(__dirname, 'badPerms'),
};
const noImageConfig = {
tmpDir: path.join(__dirname, 'tmp'),
imgDir: emptyFolder,
@ -57,48 +65,53 @@ const fileProcNoImage = new ImageGenerator(
// Test constructor
describe('Creating an ImageGenerator object', () => {
test('should create a valid object when passed the correct files and configuration', () => {
const imgGen = new ImageGenerator(folderConfig, imgConfig);
test('should create a valid object when passed the correct files and configuration', () => {
expect(imgGen).toBeInstanceOf(ImageGenerator);
});
// Image processing configuration
test('image width should be 600', () => {
expect(imgGen.imgConfig.resizeWidth).toBe(600);
});
test('image density should be 1000', () => {
expect(imgGen.imgConfig.density).toBe(1000);
});
test('image compression level should be 1', () => {
expect(imgGen.imgConfig.compLevel).toBe(1);
// Folders
});
test('folders should be the ones specified in the folder config parameter', () => {
expect(imgGen.tmpDir).toBe(path.join(__dirname, 'tmp'));
expect(imgGen.imgDir).toBe(path.join(__dirname, 'tmp'));
});
});
// test('Create ImageGenerator object with the passed in config values', () => {
// const imgGen = new ImageGenerator(folderConfig, imgConfig);
// expect(imgGen).toBeInstanceOf(ImageGenerator);
// // Image processing configuration
// expect(imgGen.imgConfig.resizeWidth).toBe(600);
// expect(imgGen.imgConfig.density).toBe(1000);
// expect(imgGen.imgConfig.compLevel).toBe(1);
// // Folders
// expect(imgGen.tmpDir).toBe(path.join(__dirname, 'tmp'));
// expect(imgGen.imgDir).toBe(path.join(__dirname, 'tmp'));
// });
// Test invalid folders passed to constructor
test('Create ImageGenerator object with invalid tmp folder', () => {
// Testing folder config
describe('Passing in', () => {
test('a non-existent tmp folder should throw error', () => {
expect(() => {
new ImageGenerator(tmpNotExist, imgConfig);
}).toThrow();
});
//TODO invalid image
// Test invalid folder permissions
test('Try tmp folder with bad permissions', () => {
});
test('a tmp folder with invalid permissions should throw error', () => {
expect(() => {
new ImageGenerator(tmpBadPerms, imgConfig);
}).toThrow();
});
test('a non-existent img folder should throw error', () => {
expect(() => {
new ImageGenerator(imgNotExist, imgConfig);
}).toThrow();
});
test('an img folder with invalid permissions should throw error', () => {
expect(() => {
new ImageGenerator(imgBadPerms, imgConfig);
}).toThrow();
});
});
// getLayers
test('Promise of an array of layers from a given folder', () => {
// Testing static methods
describe('Getting layers', () => {
test('should return a promise of array layers', () => {
expect.assertions(1);
return ImageGenerator.getLayers(testLayers, layerNames).then((data) => {
expect(data).toEqual(
@ -110,83 +123,78 @@ test('Promise of an array of layers from a given folder', () => {
]),
);
});
});
test('Non-existent folder should reject promise with error', () => {
});
test('should reject promise with error if the layers folder is not valid', () => {
expect.assertions(1);
return expect(
ImageGenerator.getLayers('./invalid_folder', layerNames),
).rejects.toThrow(new Error('Layers folder does not exist.'));
});
test('Folder with incorrect number of layers should reject promise with error', () => {
});
test('should reject promise with error if there is not the correct number of layers', () => {
expect.assertions(1);
return expect(
ImageGenerator.getLayers(emptyFolder, layerNames),
).rejects.toThrow(new Error('Layer not found.'));
});
});
// extractArchive
test('Non-existent archive should throw an error', () => {
describe('When extracting an archive', () => {
test('a non-existent archive should throw an error', () => {
expect(() =>
ImageGenerator.extractArchive('invalid.zip', folderConfig.tmpDir).toThrow(
Error,
),
ImageGenerator.extractArchive(
'invalid.zip',
folderConfig.tmpDir,
).toThrow(),
);
});
test('Temp dir not existing should throw an error', () => {
});
test('if the temp dir does not exist it should throw an error', () => {
expect(() =>
ImageGenerator.extractArchive(testGerber, './invalid_dir').toThrow(Error),
);
});
test('Should extract archive and resolve with the number of files extracted', () => {
});
test('it should extract archive and resolve with the number of files extracted', () => {
expect(() =>
ImageGenerator.extractArchive(testGerber, folderConfig.tmpDir).toBe(12),
);
});
});
// gerberToImage
test('Temp dir not existing should throw an error', () => {
// Gerber methods
describe('Converting a gerber to an image', () => {
test('temp dir not existing should throw an error', () => {
expect(() =>
fileProcNoTemp
.gerberToImage(testGerber)
.toThrow(new Error('Temporary folder does not exist.')),
);
});
test('Output dir not existing should throw an error', () => {
});
test('output dir not existing should throw an error', () => {
expect(() =>
fileProcNoImage
.gerberToImage(testGerber)
.toThrow(new Error('Output folder does not exist.')),
);
});
test('Invalid archive file should throw an error', () => {
});
test('invalid archive file should throw an error', () => {
expect(() =>
fileProc
.gerberToImage('invalid.zip')
.toThrow(new Error('Archive does not exist.')),
);
});
test('Archive with incomplete set of layers should throw an error', () => {
});
test('an archive with incomplete set of layers should throw an error', () => {
expect(() => fileProc.gerberToImage(incompleteGerber).toThrow(Error));
});
test('Gerber archive should resolve promise and return a filename of an image', () => {
});
test('gerber archive should resolve promise and return a filename of an image', () => {
expect.assertions(1);
return expect(fileProc.gerberToImage(testGerber)).resolves.toEqual(
expect.stringContaining('Arduino-Pro-Mini.png'),
);
});
// gerberToStream
test('Gerber archive should resolve promise and return a png stream', () => {
});
test('Gerber archive should resolve promise and return a png stream', () => {
expect.assertions(1);
return expect(fileProc.gerberToStream(testGerber)).resolves.toBeInstanceOf(
Readable,
);
});
});