xref: /expo/apps/test-suite/tests/Random.js (revision dae28413)
1import * as Random from 'expo-random';
2
3export const name = 'Random';
4
5export async function test({ describe, it, expect }) {
6  describe('Random', async () => {
7    it('gets random bytes asynchronously', async () => {
8      const length = 4;
9      const bytes = await Random.getRandomBytesAsync(length);
10
11      expect(bytes instanceof Uint8Array).toBe(true);
12      expect(bytes.length).toBe(length);
13    });
14
15    it('gets random bytes synchronously', () => {
16      const length = 12;
17      const bytes = Random.getRandomBytes(length);
18
19      expect(bytes instanceof Uint8Array).toBe(true);
20      expect(bytes.length).toBe(length);
21    });
22
23    // Verify with high likelihood that we get different values each time
24    it('is random', () => {
25      const length = 8;
26      const firstDraw = Random.getRandomBytes(length);
27      const secondDraw = Random.getRandomBytes(length);
28
29      expect([...firstDraw]).not.toEqual([...secondDraw]);
30    });
31  });
32}
33