Skip to content

Commit

Permalink
test: add tests for missing crypto features
Browse files Browse the repository at this point in the history
  • Loading branch information
nedsalk committed Jan 8, 2024
1 parent 1f47954 commit 7b18afc
Show file tree
Hide file tree
Showing 6 changed files with 121 additions and 0 deletions.
19 changes: 19 additions & 0 deletions packages/crypto/test/crypto-browser/btoa-undefined.test.ts
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.`
)
);
});
});
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.`
)
);
});
});
20 changes: 20 additions & 0 deletions packages/crypto/test/crypto-browser/crypto-mock.ts
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;
}
}
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.`
)
);
});
});
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 packages/crypto/test/crypto-browser/crypto-undefined.test.ts
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.`
)
);
});
});

0 comments on commit 7b18afc

Please sign in to comment.