|
| 1 | +const { afterEach, beforeEach, describe, expect, it, jest: jestGlobals } = require('@jest/globals'); |
| 2 | +const { join } = require('node:path'); |
| 3 | + |
| 4 | +describe('fsWatcher', () => { |
| 5 | + beforeEach(() => { |
| 6 | + jestGlobals.resetModules(); |
| 7 | + jestGlobals.useFakeTimers(); |
| 8 | + }); |
| 9 | + |
| 10 | + afterEach(() => { |
| 11 | + jestGlobals.useRealTimers(); |
| 12 | + jestGlobals.restoreAllMocks(); |
| 13 | + }); |
| 14 | + |
| 15 | + it('should debounce watch events and emit the latest change details', () => { |
| 16 | + let onWatchEvent; |
| 17 | + const watcher = { |
| 18 | + close: jestGlobals.fn(), |
| 19 | + on: jestGlobals.fn().mockReturnThis(), |
| 20 | + }; |
| 21 | + const watch = jestGlobals.fn((_directory, callback) => { |
| 22 | + onWatchEvent = callback; |
| 23 | + return watcher; |
| 24 | + }); |
| 25 | + |
| 26 | + jestGlobals.doMock('node:fs', () => ({ watch })); |
| 27 | + const fsWatcher = require('../scripts/fsWatcher.js'); |
| 28 | + const callback = jestGlobals.fn(); |
| 29 | + |
| 30 | + fsWatcher.makeFsWatchFilter('debounceWatcher', '/data', 200, callback); |
| 31 | + |
| 32 | + onWatchEvent('rename', 'geoip-city.dat'); |
| 33 | + onWatchEvent('change', 'geoip-country.dat'); |
| 34 | + |
| 35 | + jestGlobals.advanceTimersByTime(199); |
| 36 | + expect(callback).not.toHaveBeenCalled(); |
| 37 | + |
| 38 | + jestGlobals.advanceTimersByTime(1); |
| 39 | + expect(callback).toHaveBeenCalledTimes(1); |
| 40 | + expect(callback).toHaveBeenCalledWith({ |
| 41 | + event: 'change', |
| 42 | + file: 'geoip-country.dat', |
| 43 | + path: join('/data', 'geoip-country.dat'), |
| 44 | + }); |
| 45 | + }); |
| 46 | + |
| 47 | + it('should filter events to requested files', () => { |
| 48 | + let onWatchEvent; |
| 49 | + const watcher = { |
| 50 | + close: jestGlobals.fn(), |
| 51 | + on: jestGlobals.fn().mockReturnThis(), |
| 52 | + }; |
| 53 | + const watch = jestGlobals.fn((_directory, callback) => { |
| 54 | + onWatchEvent = callback; |
| 55 | + return watcher; |
| 56 | + }); |
| 57 | + |
| 58 | + jestGlobals.doMock('node:fs', () => ({ watch })); |
| 59 | + const fsWatcher = require('../scripts/fsWatcher.js'); |
| 60 | + const callback = jestGlobals.fn(); |
| 61 | + |
| 62 | + fsWatcher.makeFsWatchFilter('fileFilterWatcher', '/data', ['geoip-city.dat'], 100, callback); |
| 63 | + |
| 64 | + onWatchEvent('rename', 'geoip-country.dat'); |
| 65 | + jestGlobals.advanceTimersByTime(100); |
| 66 | + expect(callback).not.toHaveBeenCalled(); |
| 67 | + |
| 68 | + onWatchEvent('rename', 'geoip-city.dat'); |
| 69 | + jestGlobals.advanceTimersByTime(100); |
| 70 | + expect(callback).toHaveBeenCalledTimes(1); |
| 71 | + }); |
| 72 | + |
| 73 | + it('should close previous watcher when re-registering the same watcher name', () => { |
| 74 | + let firstHandler; |
| 75 | + let secondHandler; |
| 76 | + const firstWatcher = { |
| 77 | + close: jestGlobals.fn(), |
| 78 | + on: jestGlobals.fn().mockReturnThis(), |
| 79 | + }; |
| 80 | + const secondWatcher = { |
| 81 | + close: jestGlobals.fn(), |
| 82 | + on: jestGlobals.fn().mockReturnThis(), |
| 83 | + }; |
| 84 | + let call = 0; |
| 85 | + const watch = jestGlobals.fn((_directory, callback) => { |
| 86 | + call++; |
| 87 | + if (call === 1) { |
| 88 | + firstHandler = callback; |
| 89 | + return firstWatcher; |
| 90 | + } |
| 91 | + secondHandler = callback; |
| 92 | + return secondWatcher; |
| 93 | + }); |
| 94 | + |
| 95 | + jestGlobals.doMock('node:fs', () => ({ watch })); |
| 96 | + const fsWatcher = require('../scripts/fsWatcher.js'); |
| 97 | + const callback = jestGlobals.fn(); |
| 98 | + |
| 99 | + fsWatcher.makeFsWatchFilter('sameNameWatcher', '/data', 100, callback); |
| 100 | + fsWatcher.makeFsWatchFilter('sameNameWatcher', '/data', 100, callback); |
| 101 | + |
| 102 | + expect(firstWatcher.close).toHaveBeenCalledTimes(1); |
| 103 | + expect(secondWatcher.close).not.toHaveBeenCalled(); |
| 104 | + expect(typeof firstHandler).toBe('function'); |
| 105 | + expect(typeof secondHandler).toBe('function'); |
| 106 | + }); |
| 107 | +}); |
0 commit comments