/
helpers.spec.ts
49 lines (37 loc) · 1.42 KB
/
helpers.spec.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import directoryExists from '../src/helpers/directoryExists';
import dirSize from '../src/helpers/space';
import fs from 'fs';
import transformDirSizeThreshold from '../src/helpers/sizeThreshold';
import transformSegmentTime from '../src/helpers/segmentTime';
jest.mock('fs');
jest.mock('path');
describe('directoryExists', () => {
test('exists', () => {
jest.mocked(fs).lstatSync.mockReturnValue({isDirectory: () => true});
expect(directoryExists('path')).toBeTruthy();
});
test('does not exist', () => {
jest.mocked(fs).lstatSync.mockImplementation(() => {
throw new Error('no such file or directory');
});
expect(directoryExists('path')).toBeFalsy();
});
test('not a directory', () => {
jest.mocked(fs).lstatSync.mockReturnValue({isDirectory: () => false});
expect(() => directoryExists('path')).toThrowError('path exists but it is not a directory.');
});
});
test('transformDirSizeThreshold', () => {
expect(transformDirSizeThreshold(1)).toEqual(1);
expect(transformDirSizeThreshold('1G')).toEqual(Math.pow(1024, 3));
});
test('transformSegmentTime', () => {
expect(transformSegmentTime(1)).toEqual(1);
expect(transformSegmentTime('1m')).toEqual(60);
});
test('should return directory size in bytes', () => {
jest.mocked(fs).readdirSync.mockReturnValue(new Array(3).fill(0));
jest.mocked(fs).statSync.mockReturnValue({isDirectory: () => false, size: 3});
const size = dirSize('');
expect(size).toEqual(9);
});