immich/server/test/repositories/storage.repository.mock.ts
Jonathan Jogenfors 5d377e5b0f
chore(server): eslint await-thenable (#7545)
* await-thenable

* fix library watchers

* moar eslint

* fix test

* fix typo

* try to remove check void return

* fix checksVoidReturn

* move to domain utils

* remove eslint ignores

* chore: cleanup types

* chore: use logger

* fix: e2e

---------

Co-authored-by: Ben McCann <322311+benmccann@users.noreply.github.com>
Co-authored-by: Jason Rasmussen <jrasm91@gmail.com>
2024-03-05 17:23:06 -05:00

65 lines
1.6 KiB
TypeScript

import { IStorageRepository, StorageCore, WatchEvents } from '@app/domain';
import { WatchOptions } from 'chokidar';
interface MockWatcherOptions {
items?: Array<{ event: 'change' | 'add' | 'unlink' | 'error'; value: string }>;
close?: () => Promise<void>;
}
export const makeMockWatcher =
({ items, close }: MockWatcherOptions) =>
(paths: string[], options: WatchOptions, events: Partial<WatchEvents>) => {
events.onReady?.();
for (const item of items || []) {
switch (item.event) {
case 'add': {
events.onAdd?.(item.value);
break;
}
case 'change': {
events.onChange?.(item.value);
break;
}
case 'unlink': {
events.onUnlink?.(item.value);
break;
}
case 'error': {
events.onError?.(new Error(item.value));
}
}
}
if (close) {
return () => close();
}
return () => Promise.resolve();
};
export const newStorageRepositoryMock = (reset = true): jest.Mocked<IStorageRepository> => {
if (reset) {
StorageCore.reset();
}
return {
createZipStream: jest.fn(),
createReadStream: jest.fn(),
readFile: jest.fn(),
writeFile: jest.fn(),
unlink: jest.fn(),
unlinkDir: jest.fn().mockResolvedValue(true),
removeEmptyDirs: jest.fn(),
checkFileExists: jest.fn(),
mkdirSync: jest.fn(),
checkDiskUsage: jest.fn(),
readdir: jest.fn(),
stat: jest.fn(),
crawl: jest.fn(),
rename: jest.fn(),
copyFile: jest.fn(),
utimes: jest.fn(),
watch: jest.fn().mockImplementation(makeMockWatcher({})),
};
};