|
| 1 | +import chokidar from 'chokidar'; |
| 2 | +import type { ITriggerFunctions } from 'n8n-workflow'; |
| 3 | + |
| 4 | +import { LocalFileTrigger } from '../LocalFileTrigger.node'; |
| 5 | + |
| 6 | +jest.mock('chokidar'); |
| 7 | + |
| 8 | +const mockWatcher = { |
| 9 | + on: jest.fn(), |
| 10 | + close: jest.fn().mockResolvedValue(undefined), |
| 11 | +}; |
| 12 | + |
| 13 | +(chokidar.watch as unknown as jest.Mock).mockReturnValue(mockWatcher); |
| 14 | + |
| 15 | +describe('LocalFileTrigger', () => { |
| 16 | + let node: LocalFileTrigger; |
| 17 | + let emitSpy: jest.Mock; |
| 18 | + let context: ITriggerFunctions; |
| 19 | + |
| 20 | + beforeEach(() => { |
| 21 | + node = new LocalFileTrigger(); |
| 22 | + emitSpy = jest.fn(); |
| 23 | + |
| 24 | + context = { |
| 25 | + getNodeParameter: jest.fn(), |
| 26 | + emit: emitSpy, |
| 27 | + helpers: { |
| 28 | + returnJsonArray: (data: unknown[]) => data, |
| 29 | + }, |
| 30 | + } as unknown as ITriggerFunctions; |
| 31 | + |
| 32 | + jest.clearAllMocks(); |
| 33 | + }); |
| 34 | + |
| 35 | + it('should set up chokidar with correct options for folder + regex ignore', async () => { |
| 36 | + (context.getNodeParameter as jest.Mock) |
| 37 | + .mockReturnValueOnce('folder') |
| 38 | + .mockReturnValueOnce('/some/folder') |
| 39 | + .mockReturnValueOnce({ |
| 40 | + ignored: '**/*.txt', |
| 41 | + ignoreMode: 'regex', |
| 42 | + ignoreInitial: true, |
| 43 | + followSymlinks: true, |
| 44 | + depth: 1, |
| 45 | + usePolling: false, |
| 46 | + awaitWriteFinish: false, |
| 47 | + }) |
| 48 | + .mockReturnValueOnce(['add']); |
| 49 | + |
| 50 | + await node.trigger.call(context); |
| 51 | + |
| 52 | + expect(chokidar.watch).toHaveBeenCalledWith( |
| 53 | + '/some/folder', |
| 54 | + expect.objectContaining({ |
| 55 | + ignored: '**/*.txt', |
| 56 | + ignoreInitial: true, |
| 57 | + depth: 1, |
| 58 | + followSymlinks: true, |
| 59 | + usePolling: false, |
| 60 | + awaitWriteFinish: false, |
| 61 | + }), |
| 62 | + ); |
| 63 | + |
| 64 | + expect(mockWatcher.on).toHaveBeenCalledWith('add', expect.any(Function)); |
| 65 | + }); |
| 66 | + |
| 67 | + it('should wrap ignored in function for ignoreMode=function', async () => { |
| 68 | + (context.getNodeParameter as jest.Mock) |
| 69 | + .mockReturnValueOnce('folder') |
| 70 | + .mockReturnValueOnce('/folder') |
| 71 | + .mockReturnValueOnce({ |
| 72 | + ignored: 'node_modules', |
| 73 | + ignoreMode: 'function', |
| 74 | + }) |
| 75 | + .mockReturnValueOnce(['change']); |
| 76 | + |
| 77 | + await node.trigger.call(context); |
| 78 | + |
| 79 | + const call = (chokidar.watch as jest.Mock).mock.calls[0][1]; |
| 80 | + expect(typeof call.ignored).toBe('function'); |
| 81 | + expect(call.ignored('folder/node_modules/stuff')).toBe(true); |
| 82 | + expect(call.ignored('folder/src/index.js')).toBe(false); |
| 83 | + }); |
| 84 | + |
| 85 | + it('should emit an event when a file changes', async () => { |
| 86 | + (context.getNodeParameter as jest.Mock) |
| 87 | + .mockReturnValueOnce('folder') |
| 88 | + .mockReturnValueOnce('/watched') |
| 89 | + .mockReturnValueOnce({}) |
| 90 | + .mockReturnValueOnce(['change']); |
| 91 | + |
| 92 | + await node.trigger.call(context); |
| 93 | + |
| 94 | + const callback = mockWatcher.on.mock.calls.find(([event]) => event === 'change')?.[1]; |
| 95 | + callback?.('/watched/file.txt'); |
| 96 | + |
| 97 | + expect(emitSpy).toHaveBeenCalledWith([[{ event: 'change', path: '/watched/file.txt' }]]); |
| 98 | + }); |
| 99 | + |
| 100 | + it('should use "change" as the only event if watching a specific file', async () => { |
| 101 | + (context.getNodeParameter as jest.Mock) |
| 102 | + .mockReturnValueOnce('file') |
| 103 | + .mockReturnValueOnce('/watched/file.txt') |
| 104 | + .mockReturnValueOnce({}); |
| 105 | + |
| 106 | + await node.trigger.call(context); |
| 107 | + |
| 108 | + expect(mockWatcher.on).toHaveBeenCalledWith('change', expect.any(Function)); |
| 109 | + }); |
| 110 | +}); |
0 commit comments