Skip to content

Commit

Permalink
test: add unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
danroc committed Oct 19, 2024
1 parent b96a9a7 commit ffb1c3a
Show file tree
Hide file tree
Showing 2 changed files with 222 additions and 0 deletions.
178 changes: 178 additions & 0 deletions packages/keyring-api/src/KeyringClient.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,184 @@ describe('KeyringClient', () => {
});
});

describe('listAccountTransactions', () => {
it('returns an empty list of transactions', async () => {
const id = '49116980-0712-4fa5-b045-e4294f1d440e';
const expectedResponse = {
data: [],
next: null,
};

mockSender.send.mockResolvedValue(expectedResponse);
const transactions = await keyring.listAccountTransactions(id, {
limit: 10,
});

expect(mockSender.send).toHaveBeenCalledWith({
jsonrpc: '2.0',
id: expect.any(String),
method: 'keyring_listAccountTransactions',
params: { id, pagination: { limit: 10 } },
});

expect(transactions).toStrictEqual(expectedResponse);
});

it('returns a single page of transactions', async () => {
const id = '7bd967bd-9c4a-47cc-9725-75f0d2d8df9d';
const expectedResponse = {
data: [
{
id: 'c3d2e7a5-7c3c-4c1b-8d2e-7a57c3c41b8d',
account: '03a16e94-df42-46e6-affc-789bd58cf478',
chain: 'eip155:1',
type: 'send',
status: 'confirmed',
timestamp: 1716367781,
from: [],
to: [],
fee: {
amount: '0.001',
asset: {
fungible: true,
type: 'eip155:1/slip44:60',
unit: 'ETH',
},
},
},
{
id: '774a9423-9dd4-4b63-81a0-26884be90a35',
account: '03a16e94-df42-46e6-affc-789bd58cf478',
chain: 'eip155:1',
type: 'receive',
status: 'submitted',
timestamp: null,
from: [],
to: [],
fee: {
amount: '0',
asset: {
fungible: true,
type: 'eip155:1/slip44:60',
unit: 'ETH',
},
},
},
],
next: null,
};

mockSender.send.mockResolvedValue(expectedResponse);
const transactions = await keyring.listAccountTransactions(id, {
limit: 2,
});

expect(mockSender.send).toHaveBeenCalledWith({
jsonrpc: '2.0',
id: expect.any(String),
method: 'keyring_listAccountTransactions',
params: { id, pagination: { limit: 2 } },
});

expect(transactions).toStrictEqual(expectedResponse);
});

it('returns a page of transactions with next', async () => {
const id = '7bd967bd-9c4a-47cc-9725-75f0d2d8df9d';
const expectedResponse = {
data: [
{
id: 'c3d2e7a5-7c3c-4c1b-8d2e-7a57c3c41b8d',
account: '03a16e94-df42-46e6-affc-789bd58cf478',
chain: 'eip155:1',
type: 'send',
status: 'confirmed',
timestamp: 1716367781,
from: [],
to: [],
fee: {
amount: '0.001',
asset: {
fungible: true,
type: 'eip155:1/slip44:60',
unit: 'ETH',
},
},
},
{
id: '774a9423-9dd4-4b63-81a0-26884be90a35',
account: '03a16e94-df42-46e6-affc-789bd58cf478',
chain: 'eip155:1',
type: 'receive',
status: 'submitted',
timestamp: null,
from: [],
to: [],
fee: {
amount: '0',
asset: {
fungible: true,
type: 'eip155:1/slip44:60',
unit: 'ETH',
},
},
},
],
next: 'some-cursor',
};

mockSender.send.mockResolvedValue(expectedResponse);
const transactions = await keyring.listAccountTransactions(id, {
limit: 2,
});

expect(mockSender.send).toHaveBeenCalledWith({
jsonrpc: '2.0',
id: expect.any(String),
method: 'keyring_listAccountTransactions',
params: { id, pagination: { limit: 2 } },
});

expect(transactions).toStrictEqual(expectedResponse);
});

it('throwns an error when the fee has an invalid amount', async () => {
const id = '7bd967bd-9c4a-47cc-9725-75f0d2d8df9d';
const expectedResponse = {
data: [
{
id: 'c3d2e7a5-7c3c-4c1b-8d2e-7a57c3c41b8d',
account: '03a16e94-df42-46e6-affc-789bd58cf478',
chain: 'eip155:1',
type: 'send',
status: 'confirmed',
timestamp: 1716367781,
from: [],
to: [],
fee: {
amount: 'invalid-amount', // Should be a numeric string
asset: {
fungible: true,
type: 'eip155:1/slip44:60',
unit: 'ETH',
},
},
},
],
next: null,
};

mockSender.send.mockResolvedValue(expectedResponse);
await expect(
keyring.listAccountTransactions(id, {
limit: 2,
}),
).rejects.toThrow(
'At path: data.0.fee.amount -- Expected a value of type `StringNumber`, but received: `"invalid-amount"`',
);
});
});

describe('getAccountBalances', () => {
it('returns a valid response', async () => {
const assets = ['bip122:000000000019d6689c085ae165831e93/slip44:0'];
Expand Down
44 changes: 44 additions & 0 deletions packages/keyring-api/src/rpc-handler.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ describe('handleKeyringRequest', () => {
listAccounts: jest.fn(),
getAccount: jest.fn(),
createAccount: jest.fn(),
listAccountTransactions: jest.fn(),
getAccountBalances: jest.fn(),
filterAccountChains: jest.fn(),
updateAccount: jest.fn(),
Expand Down Expand Up @@ -110,6 +111,49 @@ describe('handleKeyringRequest', () => {
expect(result).toBe('CreateAccount result');
});

it('calls keyring_listAccountTransactions', async () => {
const accountId = '5767f284-5273-44c1-9556-31959b2afd10';
const pagination = { limit: 10, next: 'cursor-token' };
const request: JsonRpcRequest = {
jsonrpc: '2.0',
id: '9f014e5b-262b-4fb9-99b7-642d5afa21ee',
method: KeyringRpcMethod.ListAccountTransactions,
params: {
id: accountId,
pagination,
},
};

const dummyResponse = 'ListAccountTransactions result';
keyring.listAccountTransactions.mockResolvedValue(dummyResponse);
const result = await handleKeyringRequest(keyring, request);

expect(keyring.listAccountTransactions).toHaveBeenCalledWith(
accountId,
pagination,
);
expect(result).toBe(dummyResponse);
});

it('throws an error if `keyring_listAccountTransactions` is not implemented', async () => {
const request: JsonRpcRequest = {
jsonrpc: '2.0',
id: '57854955-7c1f-4603-92a8-69de8e6c678a',
method: KeyringRpcMethod.ListAccountTransactions,
params: {
id: '46e59db9-8e05-46a9-a73e-f514c55e7894',
pagination: { limit: 10, next: 'cursor-token' },
},
};

const partialKeyring: Keyring = { ...keyring };
delete partialKeyring.listAccountTransactions;

await expect(handleKeyringRequest(partialKeyring, request)).rejects.toThrow(
'Method not supported: keyring_listAccountTransactions',
);
});

it('calls `keyring_filterAccountChains`', async () => {
const request: JsonRpcRequest = {
jsonrpc: '2.0',
Expand Down

0 comments on commit ffb1c3a

Please sign in to comment.