刘光辉
12 小时以前 0dfe84494048ce27ba8449831782128412d3eb13
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
import type { OfficeContentControl } from '../../src/onlyoffice/types';
 
import { readFileSync } from 'node:fs';
import { resolve } from 'node:path';
import process from 'node:process';
 
import { acceptHMRUpdate, createPinia, defineStore, setActivePinia } from 'pinia';
import { beforeEach, describe, expect, it, vi } from 'vitest';
 
import { useAnnotationStore } from '../../src/stores/annotation';
 
describe('annotation store', () => {
  beforeEach(() => {
    setActivePinia(createPinia());
  });
 
  it('使用初始化状态且 reset 可恢复初始值', () => {
    const store = useAnnotationStore();
    store.setStatus('ready');
    store.setControls([{ InternalId: 'control-1', Tag: 'eln.field.result' }]);
    store.setOperation({ kind: 'success', message: '已完成' });
 
    store.reset();
 
    expect(store.$state).toEqual({
      status: 'initializing',
      controls: [],
      operation: { kind: 'idle', message: '' },
    });
  });
 
  it('同步切换运行状态和操作状态', () => {
    const store = useAnnotationStore();
 
    store.setStatus('working');
    store.setOperation({ kind: 'working', message: '正在读取文档字段...' });
 
    expect(store.status).toBe('working');
    expect(store.operation).toEqual({ kind: 'working', message: '正在读取文档字段...' });
  });
 
  it('复制控件数组并隔离不同 Pinia 实例的状态', () => {
    const controls: OfficeContentControl[] = [{ InternalId: 'control-1', Tag: 'eln.field.result' }];
    const first = useAnnotationStore();
    first.setControls(controls);
    controls.push({ InternalId: 'control-2', Tag: 'eln.field.other' });
 
    setActivePinia(createPinia());
    const second = useAnnotationStore();
 
    expect(first.controls).toEqual([{ InternalId: 'control-1', Tag: 'eln.field.result' }]);
    expect(second.controls).toEqual([]);
  });
 
  it('仅在 import.meta.hot 存在时注册 Pinia HMR', () => {
    const source = readFileSync(resolve(process.cwd(), 'src/stores/annotation.ts'), 'utf8');
 
    expect(source).toContain("import { acceptHMRUpdate, defineStore } from 'pinia';");
    expect(source).toContain('const hot = import.meta.hot;');
    expect(source).toContain('hot.accept(acceptHMRUpdate(useAnnotationStore, hot));');
  });
 
  it('pinia HMR 更新 Store 定义时保留当前状态', () => {
    const pinia = createPinia();
    setActivePinia(pinia);
    const store = useAnnotationStore();
    store.setStatus('ready');
    store.setControls([{ InternalId: 'control-1', Tag: 'eln.field.result' }]);
    const hot = { data: {} as Record<string, unknown>, invalidate: vi.fn() };
    const updatedUseAnnotationStore = defineStore('annotation', {
      state: () => ({ controls: [], operation: { kind: 'idle', message: '' }, status: 'initializing' }),
      actions: {
        setUpdatedStatus() {
          this.status = 'working';
        },
      },
    });
 
    acceptHMRUpdate(useAnnotationStore, hot as never)({ useAnnotationStore: updatedUseAnnotationStore });
 
    expect(store.status).toBe('ready');
    expect(store.controls).toEqual([{ InternalId: 'control-1', Tag: 'eln.field.result' }]);
    expect(hot.invalidate).not.toHaveBeenCalled();
  });
});