Files
Genarrative/apps/mobile-shell/src/host-bridge/appearance.test.ts
T
kdletters 6bd24cc7f8 补齐移动壳外观单测
移动壳 appearance helper 增加独立单测覆盖

原生壳结构门禁登记移动外观测试

宿主壳文档和共享决策同步外观测试边界
2026-06-20 08:04:46 +08:00

74 lines
2.0 KiB
TypeScript

import { Appearance } from 'react-native';
import { beforeEach, describe, expect, test, vi } from 'vitest';
import {
HOST_BRIDGE_PROTOCOL,
HOST_BRIDGE_VERSION,
type HostBridgeRequest,
} from '../../../../packages/shared/src/contracts/hostBridge';
import {
getMobileAppearanceColorScheme,
getMobileHostBridgeAppearanceColorScheme,
} from './appearance';
vi.mock('react-native', () => ({
Appearance: {
getColorScheme: vi.fn(),
},
}));
function request(): HostBridgeRequest {
return {
bridge: HOST_BRIDGE_PROTOCOL,
version: HOST_BRIDGE_VERSION,
id: 'appearance-request',
method: 'appearance.getColorScheme',
};
}
beforeEach(() => {
vi.mocked(Appearance.getColorScheme).mockReset();
});
describe('mobile appearance helpers', () => {
test('reads the current light or dark system color scheme', () => {
vi.mocked(Appearance.getColorScheme).mockReturnValue('light');
expect(getMobileAppearanceColorScheme()).toEqual({
colorScheme: 'light',
});
vi.mocked(Appearance.getColorScheme).mockReturnValue('dark');
expect(getMobileAppearanceColorScheme()).toEqual({
colorScheme: 'dark',
});
});
test('normalizes missing or unknown native color schemes to unknown', () => {
vi.mocked(Appearance.getColorScheme).mockReturnValue(null);
expect(getMobileAppearanceColorScheme()).toEqual({
colorScheme: 'unknown',
});
vi.mocked(Appearance.getColorScheme).mockReturnValue(
'unspecified' as ReturnType<typeof Appearance.getColorScheme>,
);
expect(getMobileAppearanceColorScheme()).toEqual({
colorScheme: 'unknown',
});
});
test('wraps the system color scheme in the HostBridge response shape', () => {
vi.mocked(Appearance.getColorScheme).mockReturnValue('dark');
expect(getMobileHostBridgeAppearanceColorScheme(request())).toEqual({
bridge: HOST_BRIDGE_PROTOCOL,
version: HOST_BRIDGE_VERSION,
id: 'appearance-request',
ok: true,
result: {
colorScheme: 'dark',
},
});
});
});