Update tests to use class

This commit is contained in:
Nick Playfair 2021-02-02 19:49:58 +00:00
parent 444e8a6b2a
commit 3600559d6c

View File

@ -1,25 +1,45 @@
/* eslint-disable */ /* eslint-disable */
const path = require('path'); const path = require('path');
const fs = require('fs-extra'); const fs = require('fs-extra');
const fileProc = require('../index.js'); const { ImageGenerator } = require('../index.js');
require('../index.js');
const testGerber = path.join(__dirname, 'Arduino-Pro-Mini.zip'); const testGerber = path.join(__dirname, 'Arduino-Pro-Mini.zip');
const testLayers = path.join(__dirname, 'layers'); const testLayers = path.join(__dirname, 'layers');
const emptyFolder = path.join(__dirname, 'layers', 'Empty'); const emptyFolder = path.join(__dirname, 'layers', 'Empty');
const tmpDir = path.join(__dirname, 'tmp'); // const tmpDir = path.join(__dirname, 'tmp');
const imgDir = path.join(__dirname, 'tmp'); // const imgDir = path.join(__dirname, 'tmp');
const nonWritableDir = fs.ensureDirSync(path.join(tmpDir, 'no-write'), 0o400); const folderConfig = {
tmpDir: path.join(__dirname, 'tmp'),
imgDir: path.join(__dirname, 'tmp'),
};
const noTempConfig = {
tmpDir: emptyFolder,
imgDir: path.join(__dirname, 'tmp'),
};
const noImageConfig = {
tmpDir: path.join(__dirname, 'tmp'),
imgDir: emptyFolder,
};
const nonWritableDir = fs.ensureDirSync(path.join(folderConfig.tmpDir, 'no-write'), 0o400);
const imgConfig = { const imgConfig = {
resizeWidth: 600, resizeWidth: 600,
density: 1000, density: 1000,
compLevel: 1, compLevel: 1,
}; };
const fileProc = new ImageGenerator(folderConfig, imgConfig);
const fileProcNoTemp = new ImageGenerator(noTempConfig, imgConfig);
const fileProcNoImage = new ImageGenerator(noImageConfig, imgConfig);
/**************
* Tests
***************/
// getLayers // getLayers
test('Promise of an array of layers from a given folder', () => { test('Promise of an array of layers from a given folder', () => {
expect.assertions(1); expect.assertions(1);
return fileProc.getLayers(testLayers).then((data) => { return ImageGenerator.getLayers(testLayers).then((data) => {
expect(data).toEqual( expect(data).toEqual(
expect.arrayContaining([ expect.arrayContaining([
expect.objectContaining({ expect.objectContaining({
@ -33,61 +53,63 @@ test('Promise of an array of layers from a given folder', () => {
test('Non-existent folder should reject promise with error', () => { test('Non-existent folder should reject promise with error', () => {
expect.assertions(1); expect.assertions(1);
return expect(fileProc.getLayers('./invalid_folder')).rejects.toThrow( return expect(ImageGenerator.getLayers('./invalid_folder')).rejects.toThrow(
'Layers folder does not exist.' new Error('Layers folder does not exist.')
); );
}); });
test('Folder with incorrect number of layers should reject promise with error', () => { test('Folder with incorrect number of layers should reject promise with error', () => {
expect.assertions(1); expect.assertions(1);
return expect(fileProc.getLayers(emptyFolder)).rejects.toThrow( return expect(ImageGenerator.getLayers(emptyFolder)).rejects.toThrow(
'Layer not found.' new Error('Layer not found.')
); );
}); });
// extractArchive // extractArchive
test('Non-existent archive should throw an error', () => { test('Non-existent archive should throw an error', () => {
expect(() => fileProc.extractArchive('invalid.zip', tmpDir).toThrow(Error)); expect(() =>
ImageGenerator.extractArchive('invalid.zip', folderConfig.tmpDir).toThrow(Error)
);
}); });
test('Temp dir not existing should throw an error', () => { test('Temp dir not existing should throw an error', () => {
expect(() => expect(() =>
fileProc.extractArchive(testGerber, './invalid_dir').toThrow(Error) ImageGenerator.extractArchive(testGerber, './invalid_dir').toThrow(Error)
); );
}); });
test('Should extract archive and resolve with the number of files extracted', () => { test('Should extract archive and resolve with the number of files extracted', () => {
expect(() => fileProc.extractArchive(testGerber, tmpDir).toBe(12)); expect(() => ImageGenerator.extractArchive(testGerber, folderConfig.tmpDir).toBe(12));
}); });
// gerberToImage // gerberToImage
test('Temp dir not existing should throw an error', () => { test('Temp dir not existing should throw an error', () => {
expect(() => expect(() =>
fileProc fileProcNoTemp
.gerberToImage(testGerber, imgConfig, './invalid_dir', imgDir) .gerberToImage(testGerber)
.toThrow('Temporary folder does not exist.') .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(() => expect(() =>
fileProc fileProcNoImage
.gerberToImage(testGerber, imgConfig, tmpDir, './invalid_dir') .gerberToImage(testGerber)
.toThrow('Output folder does not exist.') .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(() => expect(() =>
fileProc fileProc
.gerberToImage('invalid.zip', imgConfig, tmpDir, imgDir) .gerberToImage('invalid.zip')
.toThrow('Archive does not exist.') .toThrow(new Error('Archive does not exist.'))
); );
}); });
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); expect.assertions(1);
return expect( return expect(
fileProc.gerberToImage(testGerber, imgConfig, tmpDir, imgDir) fileProc.gerberToImage(testGerber)
).resolves.toEqual(expect.stringContaining('Arduino-Pro-Mini.png')); ).resolves.toEqual(expect.stringContaining('Arduino-Pro-Mini.png'));
}); });