1import ExpoRandom from '../ExpoRandom'; 2import * as Random from '../Random'; 3 4jest.mock('../ExpoRandom', () => ({ 5 getRandomBytesAsync: jest.fn(async () => 0), 6 getRandomBase64StringAsync: jest.fn(async () => 0), 7})); 8 9jest.mock('base64-js', () => ({ toByteArray: jest.fn(() => {}) })); 10 11it(`accepts valid byte counts`, async () => { 12 for (const value of [0, 1024, 512.5]) { 13 await expect(Random.getRandomBytesAsync(value)); 14 expect(ExpoRandom.getRandomBytesAsync).lastCalledWith(Math.floor(value)); 15 } 16}); 17 18it(`falls back to an alternative native method when getRandomBytesAsync is not available`, async () => { 19 ExpoRandom.getRandomBytesAsync = null; 20 await expect(Random.getRandomBytesAsync(1024)); 21 expect(ExpoRandom.getRandomBase64StringAsync).toHaveBeenCalled(); 22}); 23 24it(`asserts invalid byte count errors`, async () => { 25 await expect(Random.getRandomBytesAsync(-1)).rejects.toThrowError(TypeError); 26 await expect(Random.getRandomBytesAsync(1025)).rejects.toThrowError(TypeError); 27 await expect(Random.getRandomBytesAsync('invalid' as any)).rejects.toThrowError(TypeError); 28 await expect(Random.getRandomBytesAsync(null as any)).rejects.toThrowError(TypeError); 29 await expect(Random.getRandomBytesAsync({} as any)).rejects.toThrowError(TypeError); 30 await expect(Random.getRandomBytesAsync(NaN)).rejects.toThrowError(TypeError); 31}); 32