mirror of
https://github.com/immich-app/immich.git
synced 2026-05-05 20:36:08 +03:00
refactor: library watching (#7071)
This commit is contained in:
@@ -1,24 +0,0 @@
|
||||
export const newFSWatcherMock = () => {
|
||||
return {
|
||||
options: {},
|
||||
on: jest.fn(),
|
||||
add: jest.fn(),
|
||||
unwatch: jest.fn(),
|
||||
getWatched: jest.fn(),
|
||||
close: jest.fn(),
|
||||
addListener: jest.fn(),
|
||||
removeListener: jest.fn(),
|
||||
removeAllListeners: jest.fn(),
|
||||
eventNames: jest.fn(),
|
||||
rawListeners: jest.fn(),
|
||||
listeners: jest.fn(),
|
||||
emit: jest.fn(),
|
||||
listenerCount: jest.fn(),
|
||||
off: jest.fn(),
|
||||
once: jest.fn(),
|
||||
prependListener: jest.fn(),
|
||||
prependOnceListener: jest.fn(),
|
||||
setMaxListeners: jest.fn(),
|
||||
getMaxListeners: jest.fn(),
|
||||
};
|
||||
};
|
||||
@@ -1 +0,0 @@
|
||||
export * from './fswatcher.mock';
|
||||
@@ -1,4 +1,36 @@
|
||||
import { IStorageRepository, StorageCore } from '@app/domain';
|
||||
import { IStorageRepository, StorageCore, WatchEvents } from '@app/domain';
|
||||
import { WatchOptions } from 'chokidar';
|
||||
|
||||
interface MockWatcherOptions {
|
||||
items?: Array<{ event: 'change' | 'add' | 'unlink' | 'error'; value: string }>;
|
||||
close?: () => 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));
|
||||
}
|
||||
}
|
||||
}
|
||||
return () => close?.();
|
||||
};
|
||||
|
||||
export const newStorageRepositoryMock = (reset = true): jest.Mocked<IStorageRepository> => {
|
||||
if (reset) {
|
||||
@@ -21,7 +53,7 @@ export const newStorageRepositoryMock = (reset = true): jest.Mocked<IStorageRepo
|
||||
crawl: jest.fn(),
|
||||
rename: jest.fn(),
|
||||
copyFile: jest.fn(),
|
||||
watch: jest.fn(),
|
||||
utimes: jest.fn(),
|
||||
watch: jest.fn().mockImplementation(makeMockWatcher({})),
|
||||
};
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user