Skip to content

Commit 3bb3d37

Browse files
committed
fix(test): adding unit tests for new random func
1 parent db31483 commit 3bb3d37

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import { getUUID } from './random';
2+
3+
describe('Random (crypto.randomUuid()) fallback unit tests', () => {
4+
let originalRandomUuid = crypto.randomUUID;
5+
6+
beforeAll(() => {
7+
crypto.randomUUID = null; // Mock crypto.randomUUID to simulate a non-secure context
8+
});
9+
10+
it('should generate a valid UUID', () => {
11+
const uuid = getUUID();
12+
expect(uuid).toMatch(/^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/);
13+
});
14+
15+
it('should generate unique UUIDs', () => {
16+
const uuids = new Set();
17+
for (let i = 0; i < 100; i++) {
18+
uuids.add(getUUID());
19+
}
20+
expect(uuids.size).toBe(100); // All UUIDs should be unique
21+
});
22+
23+
afterAll(() => {
24+
crypto.randomUUID = originalRandomUuid; // Restore the original function
25+
});
26+
});

0 commit comments

Comments
 (0)