-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: add tests for missing crypto features
- Loading branch information
Showing
6 changed files
with
121 additions
and
0 deletions.
There are no files selected for viewing
19 changes: 19 additions & 0 deletions
19
packages/crypto/test/crypto-browser/btoa-undefined.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { ErrorCode, FuelError } from '@fuel-ts/errors'; | ||
import { expectToThrowFuelError } from '@fuel-ts/errors/test-utils'; | ||
|
||
/** | ||
* @group crypto | ||
*/ | ||
describe('throws when btoa is unavailable', () => { | ||
test('btoa is undefined', async () => { | ||
vi.stubGlobal('btoa', undefined); | ||
|
||
await expectToThrowFuelError( | ||
() => import('../../src/browser/crypto'), | ||
new FuelError( | ||
ErrorCode.ENV_DEPENDENCY_MISSING, | ||
`Could not find 'btoa' in current browser environment.` | ||
) | ||
); | ||
}); | ||
}); |
21 changes: 21 additions & 0 deletions
21
packages/crypto/test/crypto-browser/crypto-getRandomValues-undefined.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { ErrorCode, FuelError } from '@fuel-ts/errors'; | ||
import { expectToThrowFuelError } from '@fuel-ts/errors/test-utils'; | ||
|
||
import { CryptoMock } from './crypto-mock'; | ||
|
||
/** | ||
* @group crypto | ||
*/ | ||
describe('throws when crypto.getRandomValues is unavailable', () => { | ||
test('crypto.getRandomValues is undefined', async () => { | ||
vi.stubGlobal('crypto', new CryptoMock('getRandomValues')); | ||
|
||
await expectToThrowFuelError( | ||
() => import('../../src/browser/crypto'), | ||
new FuelError( | ||
ErrorCode.ENV_DEPENDENCY_MISSING, | ||
`Could not find 'crypto.getRandomValues' in current browser environment.` | ||
) | ||
); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import * as cr from 'crypto'; | ||
|
||
export class CryptoMock { | ||
/** | ||
* | ||
*/ | ||
constructor(private toUndefined: 'subtle' | 'randomUUID' | 'getRandomValues') {} | ||
|
||
get subtle() { | ||
return this.toUndefined === 'subtle' ? undefined : cr.subtle; | ||
} | ||
|
||
get randomUUID() { | ||
return this.toUndefined === 'randomUUID' ? undefined : cr.randomUUID; | ||
} | ||
|
||
get getRandomValues() { | ||
return this.toUndefined === 'getRandomValues' ? undefined : cr.getRandomValues; | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
packages/crypto/test/crypto-browser/crypto-randomUUID-undefined.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { ErrorCode, FuelError } from '@fuel-ts/errors'; | ||
import { expectToThrowFuelError } from '@fuel-ts/errors/test-utils'; | ||
|
||
import { CryptoMock } from './crypto-mock'; | ||
|
||
/** | ||
* @group crypto | ||
*/ | ||
describe('throws when crypto.randomUUID is unavailable', () => { | ||
test('crypto.randomUUID is undefined', async () => { | ||
vi.stubGlobal('crypto', new CryptoMock('randomUUID')); | ||
|
||
await expectToThrowFuelError( | ||
() => import('../../src/browser/crypto'), | ||
new FuelError( | ||
ErrorCode.ENV_DEPENDENCY_MISSING, | ||
`Could not find 'crypto.randomUUID' in current browser environment.` | ||
) | ||
); | ||
}); | ||
}); |
21 changes: 21 additions & 0 deletions
21
packages/crypto/test/crypto-browser/crypto-subtle-undefined.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { ErrorCode, FuelError } from '@fuel-ts/errors'; | ||
import { expectToThrowFuelError } from '@fuel-ts/errors/test-utils'; | ||
|
||
import { CryptoMock } from './crypto-mock'; | ||
|
||
/** | ||
* @group crypto | ||
*/ | ||
describe('throws when crypto.subtle is unavailable', () => { | ||
test('crypto.subtle is undefined', async () => { | ||
vi.stubGlobal('crypto', new CryptoMock('subtle')); | ||
|
||
await expectToThrowFuelError( | ||
() => import('../../src/browser/crypto'), | ||
new FuelError( | ||
ErrorCode.ENV_DEPENDENCY_MISSING, | ||
`Could not find 'crypto.subtle' in current browser environment.` | ||
) | ||
); | ||
}); | ||
}); |
19 changes: 19 additions & 0 deletions
19
packages/crypto/test/crypto-browser/crypto-undefined.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { ErrorCode, FuelError } from '@fuel-ts/errors'; | ||
import { expectToThrowFuelError } from '@fuel-ts/errors/test-utils'; | ||
|
||
/** | ||
* @group crypto | ||
*/ | ||
describe('throws when crypto is unavailable', () => { | ||
test('crypto is undefined', async () => { | ||
vi.stubGlobal('crypto', undefined); | ||
|
||
await expectToThrowFuelError( | ||
() => import('../../src/browser/crypto'), | ||
new FuelError( | ||
ErrorCode.ENV_DEPENDENCY_MISSING, | ||
`Could not find 'crypto' in current browser environment.` | ||
) | ||
); | ||
}); | ||
}); |