|
1 | 1 | import * as assert from 'assert';
|
2 |
| -import test, { describe } from 'node:test'; |
| 2 | +import test, { describe, mock } from 'node:test'; |
3 | 3 | import v4 from '../v4.js';
|
| 4 | +import native from '../native.js'; |
4 | 5 |
|
5 | 6 | const randomBytesFixture = Uint8Array.of(
|
6 | 7 | 0x10,
|
@@ -48,6 +49,26 @@ describe('v4', () => {
|
48 | 49 | assert.ok(id1 !== id2);
|
49 | 50 | });
|
50 | 51 |
|
| 52 | + test('should uses native randomUUID() if no option is passed', () => { |
| 53 | + mock.method(native, 'randomUUID'); |
| 54 | + |
| 55 | + assert.equal((native.randomUUID as any).mock.callCount(), 0); |
| 56 | + v4(); |
| 57 | + assert.equal((native.randomUUID as any).mock.callCount(), 1); |
| 58 | + |
| 59 | + mock.restoreAll(); |
| 60 | + }); |
| 61 | + |
| 62 | + test('should not use native randomUUID() if an option is passed', () => { |
| 63 | + mock.method(native, 'randomUUID'); |
| 64 | + |
| 65 | + assert.equal((native.randomUUID as any).mock.callCount(), 0); |
| 66 | + v4({}); |
| 67 | + assert.equal((native.randomUUID as any).mock.callCount(), 0); |
| 68 | + |
| 69 | + mock.restoreAll(); |
| 70 | + }); |
| 71 | + |
51 | 72 | test('explicit options.random produces expected result', () => {
|
52 | 73 | const id = v4({ random: randomBytesFixture });
|
53 | 74 | assert.strictEqual(id, '109156be-c4fb-41ea-b1b4-efe1671c5836');
|
|
0 commit comments