车间能级提升-智能设备管理系统
朱桂飞
2025-01-09 3e8f7f239bedae0b4f04a1ac6bd443ba6298f73c
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
import type { DrawerState } from '../drawer';
 
import { beforeEach, describe, expect, it, vi } from 'vitest';
 
import { DrawerApi } from '../drawer-api';
 
// 模拟 Store 类
vi.mock('@vben-core/shared/store', () => {
  return {
    isFunction: (fn: any) => typeof fn === 'function',
    Store: class {
      private _state: DrawerState;
      private options: any;
 
      constructor(initialState: DrawerState, options: any) {
        this._state = initialState;
        this.options = options;
      }
 
      batch(cb: () => void) {
        cb();
      }
 
      setState(fn: (prev: DrawerState) => DrawerState) {
        this._state = fn(this._state);
        this.options.onUpdate();
      }
 
      get state() {
        return this._state;
      }
    },
  };
});
 
describe('drawerApi', () => {
  let drawerApi: DrawerApi;
  let drawerState: DrawerState;
 
  beforeEach(() => {
    drawerApi = new DrawerApi();
    drawerState = drawerApi.store.state;
  });
 
  it('should initialize with default state', () => {
    expect(drawerState.isOpen).toBe(false);
    expect(drawerState.cancelText).toBe(undefined);
    expect(drawerState.confirmText).toBe(undefined);
  });
 
  it('should open the drawer', () => {
    drawerApi.open();
    expect(drawerApi.store.state.isOpen).toBe(true);
  });
 
  it('should close the drawer if onBeforeClose allows it', () => {
    drawerApi.open();
    drawerApi.close();
    expect(drawerApi.store.state.isOpen).toBe(false);
  });
 
  it('should not close the drawer if onBeforeClose returns false', () => {
    const onBeforeClose = vi.fn(() => false);
    const drawerApiWithHook = new DrawerApi({ onBeforeClose });
    drawerApiWithHook.open();
    drawerApiWithHook.close();
    expect(drawerApiWithHook.store.state.isOpen).toBe(true);
    expect(onBeforeClose).toHaveBeenCalled();
  });
 
  it('should trigger onCancel and keep drawer open if onCancel is provided', () => {
    const onCancel = vi.fn();
    const drawerApiWithHook = new DrawerApi({ onCancel });
    drawerApiWithHook.open();
    drawerApiWithHook.onCancel();
    expect(onCancel).toHaveBeenCalled();
    expect(drawerApiWithHook.store.state.isOpen).toBe(true); // 关闭逻辑不在 onCancel 内
  });
 
  it('should update shared data correctly', () => {
    const testData = { key: 'value' };
    drawerApi.setData(testData);
    expect(drawerApi.getData()).toEqual(testData);
  });
 
  it('should set state correctly using an object', () => {
    drawerApi.setState({ title: 'New Title' });
    expect(drawerApi.store.state.title).toBe('New Title');
  });
 
  it('should set state correctly using a function', () => {
    drawerApi.setState((prev) => ({ ...prev, confirmText: 'Yes' }));
    expect(drawerApi.store.state.confirmText).toBe('Yes');
  });
 
  it('should call onOpenChange when state changes', () => {
    const onOpenChange = vi.fn();
    const drawerApiWithHook = new DrawerApi({ onOpenChange });
    drawerApiWithHook.open();
    expect(onOpenChange).toHaveBeenCalledWith(true);
  });
 
  it('should batch state updates', () => {
    const batchSpy = vi.spyOn(drawerApi.store, 'batch');
    drawerApi.batchStore(() => {
      drawerApi.setState({ title: 'Batch Title' });
      drawerApi.setState({ confirmText: 'Batch Confirm' });
    });
    expect(batchSpy).toHaveBeenCalled();
    expect(drawerApi.store.state.title).toBe('Batch Title');
    expect(drawerApi.store.state.confirmText).toBe('Batch Confirm');
  });
});