Skip to content
This repository has been archived by the owner on Oct 7, 2024. It is now read-only.

Commit

Permalink
100% test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
owencraston committed Sep 26, 2023
1 parent 6c54ace commit 1160b98
Showing 1 changed file with 38 additions and 10 deletions.
48 changes: 38 additions & 10 deletions src/SnapKeyring.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -167,19 +167,11 @@ describe('SnapKeyring', () => {
});

it('removes an account', async () => {
<<<<<<< HEAD
mockSnapController.handleRequest.mockResolvedValue(null);
mockCallbacks.removeAccount.mockImplementation(async (address) => {
mockCallbacks.removeAccount.mockImplementation(async (address, handleUserInput) => {
await handleUserInput(true);
await keyring.removeAccount(address);
});
=======
mockCallbacks.removeAccount.mockImplementation(
async (_address, _snapId, handleUserInput) => {
await handleUserInput(true);
},
);
>>>>>>> 0cf64cf (test: mock `addAccount` and `removeAccount` callbacks in the unit tests)

await keyring.handleKeyringSnapMessage(snapId, {
method: KeyringEvent.AccountDeleted,
params: { id: accounts[0].id },
Expand Down Expand Up @@ -213,6 +205,21 @@ describe('SnapKeyring', () => {
).toBeNull();
});

it('throws an error if the removeAccount callback fails', async () => {
mockCallbacks.removeAccount.mockImplementation(
async (_address, _snapId, _handleUserInput) => {
throw new Error('Some error occurred while removing account');
},
);

await expect(
keyring.handleKeyringSnapMessage(snapId, {
method: KeyringEvent.AccountDeleted,
params: { id: accounts[0].id },
}),
).rejects.toThrow('Some error occurred while removing account');
});

it('fails when the method is not supported', async () => {
await expect(
keyring.handleKeyringSnapMessage(snapId, {
Expand Down Expand Up @@ -379,6 +386,27 @@ describe('SnapKeyring', () => {
await expect(responsePromise).rejects.toThrow(
"Request 'b59b5449-5517-4622-99f2-82670cc7f3f3' not found",
);
it('returns null after successfully updating an account', async () => {
const result = await keyring.handleKeyringSnapMessage(snapId, {
method: KeyringEvent.AccountUpdated,
});
expect(mockCallbacks.saveState).toHaveBeenCalledTimes(1);
expect(result).toBeNull();
});

it('throws an error if updating account fails', async () => {
mockCallbacks.saveState.mockImplementation(
async (_address, _snapId, _handleUserInput) => {
throw new Error('Some error occurred while updating account');
},
);

await expect(
keyring.handleKeyringSnapMessage(snapId, {
method: KeyringEvent.AccountUpdated,
params: { id: accounts[0].id },
}),
).rejects.toThrow('Some error occurred while updating account');
});
});

Expand Down

0 comments on commit 1160b98

Please sign in to comment.