import { describe, expect, it, vi } from 'vitest' import { OperationButton } from '../../../script/operationButton' describe('OperationButton', () => { const instance = new OperationButton() it('onLine 配置 show/disabled/loading/method 正常工作', () => { const fn = vi.fn() const loading = vi.fn().mockReturnValue(true) const disabled = vi.fn().mockReturnValue(false) const button = instance.onLine(fn, loading, disabled) expect(button.label).toBe('online') expect(button.code).toBe('online') expect(button.text).toBe(true) expect(button.plain).toBe(true) expect(button.show({ status: 'SAVED' } as any)).toBe(true) expect(button.show({ status: 'ONLINE' } as any)).toBe(false) expect(button.show({ status: 'OFFLINE_PENDING' } as any)).toBe(false) expect(button.show({ status: 'ONLINE_PENDING' } as any)).toBe(false) expect(button.show({ status: 'OFFLINE_REJECTED' } as any)).toBe(false) expect(button.disabled({ id: 1 } as any)).toBe(false) expect(button.loading(2)).toBe(true) button.method(3, { id: 10 } as any) expect(disabled).toHaveBeenCalledWith({ id: 1 }) expect(loading).toHaveBeenCalledWith(2) expect(fn).toHaveBeenCalledWith(3, { id: 10 }) }) it('offLine 仅在允许状态下展示,并透传 disabled/loading/method', () => { const fn = vi.fn() const loading = vi.fn().mockReturnValue('loading') const disabled = vi.fn().mockReturnValue(true) const button = instance.offLine(fn, loading, disabled) expect(button.label).toBe('offline') expect(button.code).toBe('offline') expect(button.show({ status: 'ONLINE' } as any)).toBe(true) expect(button.show({ status: 'NOCONF' } as any)).toBe(false) expect(button.show({ status: 'OFFLINE' } as any)).toBe(false) expect(button.show({ status: 'OFFLINE_PENDING' } as any)).toBe(false) expect(button.show({ status: 'ONLINE_PENDING' } as any)).toBe(false) expect(button.show({ status: 'ONLINE_REJECTED' } as any)).toBe(false) expect(button.show({ status: 'SAVED' } as any)).toBe(false) expect(button.disabled({ id: 2 } as any)).toBe(true) expect(button.loading(5)).toBe('loading') button.method(6, { id: 20 } as any) expect(fn).toHaveBeenCalledWith(6, { id: 20 }) }) it('withdraw 仅在待上线/待下线状态展示', () => { const fn = vi.fn() const loading = vi.fn().mockReturnValue(false) const disabled = vi.fn().mockReturnValue(true) const button = instance.withdraw(fn, loading, disabled) expect(button.label).toBe('withdraw') expect(button.code).toBe('withdraw') expect(button.show({ status: 'OFFLINE_PENDING' } as any)).toBe(true) expect(button.show({ status: 'ONLINE_PENDING' } as any)).toBe(true) expect(button.show({ status: 'ONLINE' } as any)).toBe(false) expect(button.disabled({ id: 3 } as any)).toBe(true) expect(button.loading(1)).toBe(false) button.method(7, { id: 30 } as any) expect(fn).toHaveBeenCalledWith(7, { id: 30 }) }) it('delete 默认 show=true,且默认按状态控制 disabled', () => { const fn = vi.fn() const button = instance.delete(fn) expect(button.label).toBe('delete') expect(button.code).toBe('del') expect(button.show({ status: 'ONLINE' } as any, 0)).toBe(true) expect(button.disabled({ status: 'ONLINE' } as any)).toBe(true) expect(button.disabled({ status: 'OFFLINE_PENDING' } as any)).toBe(true) expect(button.disabled({ status: 'ONLINE_PENDING' } as any)).toBe(true) expect(button.disabled({ status: 'OFFLINE_REJECTED' } as any)).toBe(true) expect(button.disabled({ status: 'SAVED' } as any)).toBe(false) expect(button.loading(1)).toBeUndefined() button.method(8, { id: 40 } as any) expect(fn).toHaveBeenCalledWith(8, { id: 40 }) }) it('delete 支持自定义 show、disabled 和 loading', () => { const fn = vi.fn() const loading = vi.fn().mockReturnValue('pending') const disabled = vi.fn().mockReturnValue(false) const show = vi.fn().mockReturnValue(false) const button = instance.delete(fn, loading, disabled, show) expect(button.show({ id: 1 } as any, 9)).toBe(false) expect(button.disabled({ id: 1 } as any)).toBe(false) expect(button.loading(3)).toBe('pending') button.method(9, { id: 50 } as any) expect(show).toHaveBeenCalledWith({ id: 1 }, 9) expect(disabled).toHaveBeenCalledWith({ id: 1 }) expect(loading).toHaveBeenCalledWith(3) expect(fn).toHaveBeenCalledWith(9, { id: 50 }) }) it('editor 默认按状态控制 disabled,并透传 loading/method', () => { const fn = vi.fn() const loading = vi.fn().mockReturnValue(true) const button = instance.editor(fn, loading) expect(button.label).toBe('edit') expect(button.code).toBe('edit') expect(button.disabled({ status: 'ONLINE' } as any)).toBe(true) expect(button.disabled({ status: 'ONLINE_PENDING' } as any)).toBe(true) expect(button.disabled({ status: 'OFFLINE_PENDING' } as any)).toBe(true) expect(button.disabled({ status: 'OFFLINE_REJECTED' } as any)).toBe(true) expect(button.disabled({ status: 'SAVED' } as any)).toBe(false) expect(button.loading(4)).toBe(true) button.method(10, { id: 60 } as any) expect(fn).toHaveBeenCalledWith(10, { id: 60 }) }) it('editor 支持自定义 disabled', () => { const disabled = vi.fn().mockReturnValue(false) const button = instance.editor(undefined as any, undefined, disabled) expect(button.disabled({ status: 'ONLINE' } as any)).toBe(false) expect(disabled).toHaveBeenCalledWith({ status: 'ONLINE' }) }) it('stop 默认 show=true,且仅 status=1 时可用', () => { const fn = vi.fn() const button = instance.stop(fn) expect(button.label).toBe('stop') expect(button.code).toBe('enable') expect(button.show({ status: 1 } as any, 0)).toBe(true) expect(button.disabled({ status: 1 } as any)).toBe(false) expect(button.disabled({ status: 3 } as any)).toBe(true) expect(button.disabled({ status: 2 } as any)).toBe(true) button.method(11, { id: 70 } as any) expect(fn).toHaveBeenCalledWith(11, { id: 70 }) }) it('stop 支持自定义 show、disabled 和 loading', () => { const fn = vi.fn() const loading = vi.fn().mockReturnValue('loading') const disabled = vi.fn().mockReturnValue(true) const show = vi.fn().mockReturnValue(false) const button = instance.stop(fn, loading, disabled, show) expect(button.show({ id: 1 } as any, 2)).toBe(false) expect(button.disabled({ id: 1 } as any)).toBe(true) expect(button.loading(6)).toBe('loading') button.method(12, { id: 80 } as any) expect(show).toHaveBeenCalledWith({ id: 1 }, 2) expect(disabled).toHaveBeenCalledWith({ id: 1 }) expect(loading).toHaveBeenCalledWith(6) expect(fn).toHaveBeenCalledWith(12, { id: 80 }) }) it('enable 默认 show=true,且仅 status 不为 1/3 时可用', () => { const fn = vi.fn() const button = instance.enable(fn) expect(button.label).toBe('enable') expect(button.code).toBe('enable') expect(button.show({ status: 2 } as any, 0)).toBe(true) expect(button.disabled({ status: 2 } as any)).toBe(false) expect(button.disabled({ status: 1 } as any)).toBe(true) expect(button.disabled({ status: 3 } as any)).toBe(true) button.method(13, { id: 90 } as any) expect(fn).toHaveBeenCalledWith(13, { id: 90 }) }) it('enable 支持自定义 show、disabled 和 loading', () => { const fn = vi.fn() const loading = vi.fn().mockReturnValue(false) const disabled = vi.fn().mockReturnValue(true) const show = vi.fn().mockReturnValue(false) const button = instance.enable(fn, loading, disabled, show) expect(button.show({ id: 1 } as any, 7)).toBe(false) expect(button.disabled({ id: 1 } as any)).toBe(true) expect(button.loading(8)).toBe(false) button.method(14, { id: 100 } as any) expect(show).toHaveBeenCalledWith({ id: 1 }, 7) expect(disabled).toHaveBeenCalledWith({ id: 1 }) expect(loading).toHaveBeenCalledWith(8) expect(fn).toHaveBeenCalledWith(14, { id: 100 }) }) it('未传回调时 method/disabled/loading 返回 undefined 且不报错', () => { const onlineButton = instance.onLine(undefined as any) const withdrawButton = instance.withdraw(undefined as any) expect(() => onlineButton.method(1, { id: 1 } as any)).not.toThrow() expect(() => withdrawButton.method(2, { id: 2 } as any)).not.toThrow() expect(onlineButton.disabled({ id: 1 } as any)).toBeUndefined() expect(onlineButton.loading(1)).toBeUndefined() expect(withdrawButton.disabled({ id: 2 } as any)).toBeUndefined() expect(withdrawButton.loading(2)).toBeUndefined() }) })