Skip to content

Commit cfc75ca

Browse files
committed
test: add a basic test for file change queue
1 parent 8e699b1 commit cfc75ca

File tree

1 file changed

+70
-0
lines changed

1 file changed

+70
-0
lines changed

src/createFileChangeQueue.test.ts

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
import { createFileChangeQueue } from './createFileChangeQueue';
2+
import { type Subscription } from './types';
3+
import { mkdir, rmdir, writeFile } from 'node:fs/promises';
4+
import { join } from 'node:path';
5+
import { setTimeout } from 'node:timers/promises';
6+
import * as sinon from 'sinon';
7+
import { beforeEach, expect, test } from 'vitest';
8+
9+
const FIXTURES_DIRECTORY = join(__dirname, '.createFileChangeQueueFixtures');
10+
11+
beforeEach(async () => {
12+
try {
13+
await rmdir(FIXTURES_DIRECTORY, {
14+
recursive: true,
15+
});
16+
} catch {
17+
//
18+
}
19+
20+
await mkdir(FIXTURES_DIRECTORY);
21+
});
22+
23+
test('deduplicates triggers', async () => {
24+
const fooFile = join(FIXTURES_DIRECTORY, 'foo');
25+
26+
await writeFile(fooFile, 'foo');
27+
28+
const abortController = new AbortController();
29+
30+
const trigger = sinon.stub().resolves(null);
31+
32+
const subscription: Subscription = {
33+
activeTask: null,
34+
expression: ['match', '*'],
35+
initialRun: false,
36+
persistent: false,
37+
teardown: async () => {},
38+
trigger,
39+
};
40+
41+
const fileChangeQueue = createFileChangeQueue({
42+
abortSignal: abortController.signal,
43+
project: FIXTURES_DIRECTORY,
44+
subscriptions: [subscription],
45+
userDebounce: {
46+
wait: 100,
47+
},
48+
});
49+
50+
fileChangeQueue.trigger({
51+
filename: fooFile,
52+
hash: 'bar',
53+
});
54+
55+
fileChangeQueue.trigger({
56+
filename: fooFile,
57+
hash: 'baz',
58+
});
59+
60+
await setTimeout(200);
61+
62+
expect(trigger.callCount).toBe(1);
63+
64+
expect(trigger.firstCall.args[0]).toEqual([
65+
{
66+
filename: fooFile,
67+
hash: 'baz',
68+
},
69+
]);
70+
});

0 commit comments

Comments
 (0)