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();
|
});
|
});
|