74 lines
2.7 KiB
TypeScript
74 lines
2.7 KiB
TypeScript
import { beforeEach, describe, expect, it, vi } from 'vitest';
|
|
import { readFileSync } from 'node:fs';
|
|
import { resolve } from 'node:path';
|
|
|
|
const { bootstrapDesktopShellMock, createDesktopTrayControllerMock, getTrayMock } = vi.hoisted(() => ({
|
|
bootstrapDesktopShellMock: vi.fn(),
|
|
createDesktopTrayControllerMock: vi.fn(() => ({ getTray: getTrayMock })),
|
|
getTrayMock: vi.fn(),
|
|
}));
|
|
|
|
vi.mock('react-native-gesture-handler', () => ({}));
|
|
vi.mock('react-native-url-polyfill/auto', () => ({}));
|
|
vi.mock('@telescope/desktop-shell', () => ({
|
|
bootstrapDesktopShell: bootstrapDesktopShellMock,
|
|
createDesktopTrayController: createDesktopTrayControllerMock,
|
|
}));
|
|
vi.mock('../src/App', () => ({
|
|
__esModule: true,
|
|
default: () => null,
|
|
}));
|
|
|
|
describe('main-web', () => {
|
|
beforeEach(() => {
|
|
vi.resetModules();
|
|
vi.clearAllMocks();
|
|
delete (globalThis as any).document;
|
|
});
|
|
|
|
it('存在 #root 时会初始化 logger、桌面壳层并启动应用', async () => {
|
|
const root = { id: 'root' };
|
|
(globalThis as any).document = {
|
|
getElementById: vi.fn((id: string) => (id === 'root' ? root : null)),
|
|
};
|
|
|
|
await import('../src/main-web');
|
|
|
|
const { initLogger } = await import('@telescope/logger');
|
|
const { AppRegistry } = await import('react-native');
|
|
|
|
expect(initLogger).toHaveBeenCalledTimes(1);
|
|
const [loggerConfig] = (initLogger as any).mock.calls[0] as [{ transports: unknown[] }];
|
|
expect(loggerConfig.transports).toHaveLength(2);
|
|
expect((loggerConfig.transports[1] as { constructor: { name: string } }).constructor.name).toBe('TauriLogTransport');
|
|
expect(createDesktopTrayControllerMock).toHaveBeenCalledTimes(1);
|
|
expect(bootstrapDesktopShellMock).toHaveBeenCalledWith({
|
|
enableWindowGuards: process.env.NODE_ENV === 'production',
|
|
getTray: getTrayMock,
|
|
});
|
|
expect(AppRegistry.registerComponent).toHaveBeenCalledWith('XingyunReactNative', expect.any(Function));
|
|
expect(AppRegistry.runApplication).toHaveBeenCalledWith('XingyunReactNative', { rootTag: root });
|
|
});
|
|
|
|
it('缺少 #root 时抛出错误', async () => {
|
|
(globalThis as any).document = {
|
|
getElementById: vi.fn(() => null),
|
|
};
|
|
|
|
await expect(import('../src/main-web')).rejects.toThrow('Root element #root not found');
|
|
|
|
const { AppRegistry } = await import('react-native');
|
|
expect(AppRegistry.runApplication).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it('桌面 capability 需要放行 desktop-shell set_tray_ready 命令', () => {
|
|
const source = readFileSync(
|
|
resolve(__dirname, '../src-tauri/capabilities/default.json'),
|
|
'utf8'
|
|
);
|
|
const capability = JSON.parse(source) as { permissions: string[] };
|
|
|
|
expect(capability.permissions).toContain('desktop-shell:allow-set-tray-ready');
|
|
});
|
|
});
|