3 changed files with 59 additions and 4 deletions
@ -1,5 +1,50 @@
|
||||
import { create_images_directory } from '../src/logic'; |
||||
|
||||
beforeEach(jest.restoreAllMocks); |
||||
|
||||
const fs = require('fs').promises; |
||||
const filesystem = require('fs'); |
||||
|
||||
describe('create_images_directory', () => { |
||||
it('', () => {}); |
||||
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'); |
||||
}); |
||||
}), |
||||
); |
||||
}); |
||||
|
||||
Loading…
Reference in new issue