intentBus.test.ts
1.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import { afterEach, describe, expect, it, vi } from 'vitest';
import {
dispatchIntent,
getAnalysisRerunVersion,
getCaseFocusVersion,
getHotspotFocusVersion,
getSpotlightOpenVersion,
resetIntentsForTests,
subscribeIntents,
} from './intentBus';
describe('地图意图通道(阶段 3:替代 store 的四个 seq)', () => {
afterEach(() => resetIntentsForTests());
it('四种意图各自独立计数,初始都是 0', () => {
expect(getHotspotFocusVersion()).toBe(0);
expect(getAnalysisRerunVersion()).toBe(0);
expect(getCaseFocusVersion()).toBe(0);
expect(getSpotlightOpenVersion()).toBe(0);
});
it('发意图只让对应类型 +1', () => {
dispatchIntent('hotspotFocus');
expect(getHotspotFocusVersion()).toBe(1);
expect(getAnalysisRerunVersion()).toBe(0);
expect(getCaseFocusVersion()).toBe(0);
expect(getSpotlightOpenVersion()).toBe(0);
});
it('重复点同一行也重新请求:同类连发两次版本 +2', () => {
dispatchIntent('caseFocus');
dispatchIntent('caseFocus');
expect(getCaseFocusVersion()).toBe(2);
});
it('订阅者每次发意图都收到通知,退订后不再收到', () => {
const listener = vi.fn();
const unsubscribe = subscribeIntents(listener);
dispatchIntent('analysisRerun');
dispatchIntent('spotlightOpen');
expect(listener).toHaveBeenCalledTimes(2);
unsubscribe();
dispatchIntent('analysisRerun');
expect(listener).toHaveBeenCalledTimes(2);
});
});