import { Platform, Dimensions } from 'react-native'; import { getDeviceInfo } from '../hooks/useDeviceInfoCollector'; jest.mock('react-native', () => ({ Platform: { OS: 'ios', Version: '16.0', constants: {}, }, Dimensions: { get: jest.fn(), }, })); jest.mock('react-native-device-info', () => ({ getModel: () => 'iPhone 14', }), { virtual: true }); const mockDimensionsGet = Dimensions.get as jest.MockedFunction; describe('useDeviceInfoCollector', () => { beforeEach(() => { jest.clearAllMocks(); }); describe('getDeviceInfo', () => { it('should return complete device information', () => { mockDimensionsGet.mockImplementation((type) => { if (type === 'screen') { return { width: 390, height: 844, scale: 3, fontScale: 1 }; } return { width: 390, height: 844, scale: 3, fontScale: 1 }; }); const deviceInfo = getDeviceInfo(); expect(deviceInfo).toEqual({ platform: 'ios', osVersion: '16.0', screenWidth: 390, screenHeight: 844, windowWidth: 390, windowHeight: 844, scale: 3, fontScale: 1, deviceType: 'phone', model: 'iPhone 14', }); }); it('should identify device as tablet when dimensions are large', () => { mockDimensionsGet.mockImplementation((type) => { if (type === 'screen') { return { width: 768, height: 1024, scale: 2, fontScale: 1 }; } return { width: 768, height: 1024, scale: 2, fontScale: 1 }; }); const deviceInfo = getDeviceInfo(); expect(deviceInfo.deviceType).toBe('tablet'); }); it('should identify device as phone when dimensions are small', () => { mockDimensionsGet.mockImplementation((type) => { if (type === 'screen') { return { width: 375, height: 812, scale: 3, fontScale: 1 }; } return { width: 375, height: 812, scale: 3, fontScale: 1 }; }); const deviceInfo = getDeviceInfo(); expect(deviceInfo.deviceType).toBe('phone'); }); it('should return model from react-native-device-info', () => { mockDimensionsGet.mockImplementation(() => ({ width: 390, height: 844, scale: 3, fontScale: 1, })); const deviceInfo = getDeviceInfo(); expect(deviceInfo.model).toBe('iPhone 14'); }); }); describe('platform detection', () => { it('should detect Android platform', () => { (Platform.OS as any) = 'android'; (Platform.Version as any) = 33; mockDimensionsGet.mockImplementation(() => ({ width: 412, height: 915, scale: 2.625, fontScale: 1, })); const deviceInfo = getDeviceInfo(); expect(deviceInfo.platform).toBe('android'); expect(deviceInfo.osVersion).toBe('33'); }); }); });