Skip to content

Commit 7b18afc

Browse files
committed
test: add tests for missing crypto features
1 parent 1f47954 commit 7b18afc

File tree

6 files changed

+121
-0
lines changed

6 files changed

+121
-0
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { ErrorCode, FuelError } from '@fuel-ts/errors';
2+
import { expectToThrowFuelError } from '@fuel-ts/errors/test-utils';
3+
4+
/**
5+
* @group crypto
6+
*/
7+
describe('throws when btoa is unavailable', () => {
8+
test('btoa is undefined', async () => {
9+
vi.stubGlobal('btoa', undefined);
10+
11+
await expectToThrowFuelError(
12+
() => import('../../src/browser/crypto'),
13+
new FuelError(
14+
ErrorCode.ENV_DEPENDENCY_MISSING,
15+
`Could not find 'btoa' in current browser environment.`
16+
)
17+
);
18+
});
19+
});
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import { ErrorCode, FuelError } from '@fuel-ts/errors';
2+
import { expectToThrowFuelError } from '@fuel-ts/errors/test-utils';
3+
4+
import { CryptoMock } from './crypto-mock';
5+
6+
/**
7+
* @group crypto
8+
*/
9+
describe('throws when crypto.getRandomValues is unavailable', () => {
10+
test('crypto.getRandomValues is undefined', async () => {
11+
vi.stubGlobal('crypto', new CryptoMock('getRandomValues'));
12+
13+
await expectToThrowFuelError(
14+
() => import('../../src/browser/crypto'),
15+
new FuelError(
16+
ErrorCode.ENV_DEPENDENCY_MISSING,
17+
`Could not find 'crypto.getRandomValues' in current browser environment.`
18+
)
19+
);
20+
});
21+
});
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import * as cr from 'crypto';
2+
3+
export class CryptoMock {
4+
/**
5+
*
6+
*/
7+
constructor(private toUndefined: 'subtle' | 'randomUUID' | 'getRandomValues') {}
8+
9+
get subtle() {
10+
return this.toUndefined === 'subtle' ? undefined : cr.subtle;
11+
}
12+
13+
get randomUUID() {
14+
return this.toUndefined === 'randomUUID' ? undefined : cr.randomUUID;
15+
}
16+
17+
get getRandomValues() {
18+
return this.toUndefined === 'getRandomValues' ? undefined : cr.getRandomValues;
19+
}
20+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import { ErrorCode, FuelError } from '@fuel-ts/errors';
2+
import { expectToThrowFuelError } from '@fuel-ts/errors/test-utils';
3+
4+
import { CryptoMock } from './crypto-mock';
5+
6+
/**
7+
* @group crypto
8+
*/
9+
describe('throws when crypto.randomUUID is unavailable', () => {
10+
test('crypto.randomUUID is undefined', async () => {
11+
vi.stubGlobal('crypto', new CryptoMock('randomUUID'));
12+
13+
await expectToThrowFuelError(
14+
() => import('../../src/browser/crypto'),
15+
new FuelError(
16+
ErrorCode.ENV_DEPENDENCY_MISSING,
17+
`Could not find 'crypto.randomUUID' in current browser environment.`
18+
)
19+
);
20+
});
21+
});
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import { ErrorCode, FuelError } from '@fuel-ts/errors';
2+
import { expectToThrowFuelError } from '@fuel-ts/errors/test-utils';
3+
4+
import { CryptoMock } from './crypto-mock';
5+
6+
/**
7+
* @group crypto
8+
*/
9+
describe('throws when crypto.subtle is unavailable', () => {
10+
test('crypto.subtle is undefined', async () => {
11+
vi.stubGlobal('crypto', new CryptoMock('subtle'));
12+
13+
await expectToThrowFuelError(
14+
() => import('../../src/browser/crypto'),
15+
new FuelError(
16+
ErrorCode.ENV_DEPENDENCY_MISSING,
17+
`Could not find 'crypto.subtle' in current browser environment.`
18+
)
19+
);
20+
});
21+
});
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { ErrorCode, FuelError } from '@fuel-ts/errors';
2+
import { expectToThrowFuelError } from '@fuel-ts/errors/test-utils';
3+
4+
/**
5+
* @group crypto
6+
*/
7+
describe('throws when crypto is unavailable', () => {
8+
test('crypto is undefined', async () => {
9+
vi.stubGlobal('crypto', undefined);
10+
11+
await expectToThrowFuelError(
12+
() => import('../../src/browser/crypto'),
13+
new FuelError(
14+
ErrorCode.ENV_DEPENDENCY_MISSING,
15+
`Could not find 'crypto' in current browser environment.`
16+
)
17+
);
18+
});
19+
});

0 commit comments

Comments
 (0)