You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

50 lines
1.6 KiB

import { create_images_directory } from '../src/logic';
beforeEach(jest.restoreAllMocks);
const fs = require('fs').promises;
const filesystem = require('fs');
describe('create_images_directory', () => {
it('creates images directory', () => {
const mock = jest
.spyOn(fs, 'mkdir')
.mockImplementation(() => Promise.resolve());
jest.spyOn(filesystem, 'existsSync').mockImplementation(() => false);
const dir = 'new directory';
expect.assertions(2);
return create_images_directory(dir).then(() => {
expect(mock).toHaveBeenCalledTimes(1);
expect(mock).toHaveBeenCalledWith(dir, { recursive: true });
});
});
it('it skips creating images directory', () => {
const fs_mock = jest
.spyOn(fs, 'mkdir')
.mockImplementation(() => Promise.resolve());
const exist_mock = jest
.spyOn(filesystem, 'existsSync')
.mockImplementation(() => true);
const dir = 'new directory';
expect.assertions(3);
return create_images_directory(dir).then(() => {
expect(fs_mock).toHaveBeenCalledTimes(0);
expect(exist_mock).toHaveBeenCalledTimes(1);
expect(exist_mock).toHaveBeenCalledWith(dir);
});
});
[undefined, null].forEach((dir) =>
it('throws if directory is not set', () => {
const mock = jest
.spyOn(filesystem, 'existsSync')
.mockImplementation(() => true);
expect.assertions(2);
return create_images_directory(dir).catch((error) => {
expect(mock).toHaveBeenCalledTimes(0);
expect(error).toEqual('Image path was not set');
});
}),
);
});