import { canIUse } from '../can-i-use'; import { env } from '../get-system-info'; jest.mock('../get-system-info'); jest.mock('../../utils/can-i-use', () => ({ //eslint-disable-next-line @typescript-eslint/ban-ts-comment //@ts-ignore ...jest.requireActual('../../utils/can-i-use'), getCanIUseConfig: jest.fn() })); describe('canIUse', () => { const mockEnv = env as jest.MockedFunction; const mockGetCanIUseConfig = jest.requireMock('../../utils/can-i-use').getCanIUseConfig; beforeEach(() => { jest.clearAllMocks(); //eslint-disable-next-line @typescript-eslint/ban-ts-comment //@ts-ignore mockEnv.mockReturnValue({ data: { deviceInfo: { platform: 'h5' } } }); }); it('should return false when platform is not available', () => { //eslint-disable-next-line @typescript-eslint/ban-ts-comment //@ts-ignore mockEnv.mockReturnValue({ data: {} }); expect(canIUse('someApi')).toBe(false); }); it('should return false when API config is not found', () => { mockGetCanIUseConfig.mockReturnValue(null); expect(canIUse('unknownApi')).toBe(false); }); it('should return false when API version is invalid', () => { mockGetCanIUseConfig.mockReturnValue({ version: {} }); expect(canIUse('api')).toBe(false); }); it('should return true for supported API without properties', () => { mockGetCanIUseConfig.mockReturnValue({ version: '0.9.0' }); expect(canIUse('supportedApi')).toBe(false); }); it('should check H5 platform configuration', () => { //eslint-disable-next-line @typescript-eslint/ban-ts-comment //@ts-ignore mockEnv.mockReturnValue({ data: { deviceInfo: { platform: 'h5' } } }); mockGetCanIUseConfig.mockReturnValue({ version: '1.0.0' }); expect(canIUse('webApi')).toBe(false); expect(mockGetCanIUseConfig).toHaveBeenCalledWith('h5', 'webApi'); }); it('should handle nested property checks', () => { mockGetCanIUseConfig.mockReturnValue({ version: '1.0.0', properties: { feature: { subFeature: '1.0.0' } } }); expect(canIUse('api:feature:subFeature')).toBe(false); }); it('should return false for unsupported property version', () => { mockGetCanIUseConfig.mockReturnValue({ version: '0.9.0', properties: { feature: '2.0.0' } }); expect(canIUse('api:feature')).toBe(false); }); });