Skip to content

Commit

Permalink
Add more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Mrtenz committed Jan 8, 2025
1 parent 1d5415c commit 1a95f6b
Show file tree
Hide file tree
Showing 5 changed files with 120 additions and 7 deletions.
6 changes: 3 additions & 3 deletions packages/snaps-controllers/coverage.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"branches": 93.06,
"branches": 92.41,
"functions": 96.54,
"lines": 98.02,
"statements": 97.74
"lines": 97.99,
"statements": 97.71
}
71 changes: 71 additions & 0 deletions packages/snaps-controllers/src/snaps/SnapController.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ import {
AssertionError,
base64ToBytes,
stringToBytes,
createDeferredPromise,
} from '@metamask/utils';
import { File } from 'buffer';
import { webcrypto } from 'crypto';
Expand All @@ -78,6 +79,7 @@ import {
getNodeEESMessenger,
getPersistedSnapsState,
getSnapController,
getSnapControllerEncryptor,
getSnapControllerMessenger,
getSnapControllerOptions,
getSnapControllerWithEES,
Expand Down Expand Up @@ -9164,6 +9166,40 @@ describe('SnapController', () => {

snapController.destroy();
});

it('logs an error message if the state fails to persist', async () => {
const messenger = getSnapControllerMessenger();

const errorValue = new Error('Failed to persist state.');
const snapController = getSnapController(
getSnapControllerOptions({
messenger,
state: {
snaps: getPersistedSnapsState(),
},
// @ts-expect-error - Missing required properties.
encryptor: {
...getSnapControllerEncryptor(),
encryptWithKey: jest.fn().mockRejectedValue(errorValue),
},
}),
);

const { promise, resolve } = createDeferredPromise();
const error = jest.spyOn(console, 'error').mockImplementation(resolve);

await messenger.call(
'SnapController:updateSnapState',
MOCK_SNAP_ID,
{ foo: 'bar' },
true,
);

await promise;
expect(error).toHaveBeenCalledWith(errorValue);

snapController.destroy();
});
});

describe('SnapController:clearSnapState', () => {
Expand Down Expand Up @@ -9222,6 +9258,41 @@ describe('SnapController', () => {

snapController.destroy();
});

it('logs an error message if the state fails to persist', async () => {
const messenger = getSnapControllerMessenger();

const errorValue = new Error('Failed to persist state.');
const snapController = getSnapController(
getSnapControllerOptions({
messenger,
state: {
snaps: getPersistedSnapsState(),
},
// @ts-expect-error - Missing required properties.
encryptor: {
...getSnapControllerEncryptor(),
encryptWithKey: jest.fn().mockRejectedValue(errorValue),
},
}),
);

const { promise, resolve } = createDeferredPromise();
const error = jest.spyOn(console, 'error').mockImplementation(resolve);

// @ts-expect-error - Property `update` is protected.
// eslint-disable-next-line jest/prefer-spy-on
snapController.update = jest.fn().mockImplementation(() => {
throw errorValue;
});

await messenger.call('SnapController:clearSnapState', MOCK_SNAP_ID, true);

await promise;
expect(error).toHaveBeenCalledWith(errorValue);

snapController.destroy();
});
});

describe('SnapController:updateBlockedSnaps', () => {
Expand Down
6 changes: 5 additions & 1 deletion packages/snaps-controllers/src/test-utils/controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import {
exportKey,
generateSalt,
isVaultUpdated,
encrypt,
decrypt,
} from '@metamask/browser-passworder';
import type {
PermissionConstraint,
Expand Down Expand Up @@ -540,8 +542,10 @@ export const DEFAULT_ENCRYPTION_KEY_DERIVATION_OPTIONS = {
},
};

const getSnapControllerEncryptor = () => {
export const getSnapControllerEncryptor = () => {
return {
encrypt,
decrypt,
encryptWithKey,
decryptWithKey,
keyFromPassword: async (
Expand Down
6 changes: 3 additions & 3 deletions packages/snaps-utils/coverage.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"branches": 99.74,
"functions": 98.93,
"lines": 99.46,
"statements": 96.31
"functions": 98.95,
"lines": 99.47,
"statements": 96.33
}
38 changes: 38 additions & 0 deletions packages/snaps-utils/src/mutex.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { createDeferredPromise } from '@metamask/utils';

import { withMutex } from './mutex';

describe('withMutex', () => {
it('runs the function with a mutex', async () => {
jest.useFakeTimers();

const { promise, resolve: resolveDeferred } = createDeferredPromise();

const fn = jest.fn().mockImplementation(async () => {
return await new Promise<void>((resolve) => {
resolveDeferred();
setTimeout(() => {
resolve();
}, 1000);
});
});

const wrappedFn = withMutex(fn);

const first = wrappedFn();
const second = wrappedFn();

await promise;
jest.advanceTimersByTime(1000);

expect(fn).toHaveBeenCalledTimes(1);

await first;

jest.advanceTimersByTime(1000);

await second;

expect(fn).toHaveBeenCalledTimes(2);
});
});

0 comments on commit 1a95f6b

Please sign in to comment.