File tree Expand file tree Collapse file tree 6 files changed +121
-0
lines changed
packages/crypto/test/crypto-browser Expand file tree Collapse file tree 6 files changed +121
-0
lines changed Original file line number Diff line number Diff line change 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+ } ) ;
Original file line number Diff line number Diff line change 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+ } ) ;
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 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+ } ) ;
Original file line number Diff line number Diff line change 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+ } ) ;
Original file line number Diff line number Diff line change 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+ } ) ;
You can’t perform that action at this time.
0 commit comments