Add a couple of negative tests

This commit is contained in:
Michael Vines 2018-08-24 11:12:48 -07:00
parent 9da3188a2d
commit 0aa5d2c71a
1 changed files with 46 additions and 16 deletions

View File

@ -14,6 +14,14 @@ if (process.env.DOITLIVE) {
jest.mock('node-fetch'); jest.mock('node-fetch');
} }
const errorMessage = 'Invalid request';
const errorResponse = {
error: {
message: errorMessage,
},
result: undefined,
};
test('get balance', async () => { test('get balance', async () => {
const account = new Account(); const account = new Account();
const connection = new Connection(url); const connection = new Connection(url);
@ -34,11 +42,27 @@ test('get balance', async () => {
expect(balance).toBeGreaterThanOrEqual(0); expect(balance).toBeGreaterThanOrEqual(0);
}); });
test('throws on bad transaction confirmation', () => { test('get balance - error', () => {
const connection = new Connection(url);
const invalidPublicKey = 'not a public key';
mockRpc.push([
url,
{
method: 'getBalance',
params: [invalidPublicKey],
},
errorResponse,
]);
expect(connection.getBalance(invalidPublicKey))
.rejects.toThrow(errorMessage);
});
test('confirm transaction - error', () => {
const connection = new Connection(url); const connection = new Connection(url);
const badTransactionSignature = 'bad transaction signature'; const badTransactionSignature = 'bad transaction signature';
const errorMessage = 'Invalid request';
mockRpc.push([ mockRpc.push([
url, url,
@ -46,12 +70,7 @@ test('throws on bad transaction confirmation', () => {
method: 'confirmTransaction', method: 'confirmTransaction',
params: [badTransactionSignature], params: [badTransactionSignature],
}, },
{ errorResponse,
error: {
message: errorMessage,
},
result: undefined,
}
] ]
); );
@ -164,7 +183,24 @@ test('request airdrop', async () => {
expect(balance).toBe(42); expect(balance).toBe(42);
}); });
test('throws on bad transaction', () => { test('request airdrop - error', () => {
const invalidPublicKey = 'not a public key';
const connection = new Connection(url);
mockRpc.push([
url,
{
method: 'requestAirdrop',
params: [invalidPublicKey, 1],
},
errorResponse
]);
expect(connection.requestAirdrop(invalidPublicKey, 1))
.rejects.toThrow(errorMessage);
});
test('send transaction - error', () => {
const secretKey = Buffer.from([ const secretKey = Buffer.from([
153, 218, 149, 89, 225, 94, 145, 62, 233, 171, 46, 83, 227, 153, 218, 149, 89, 225, 94, 145, 62, 233, 171, 46, 83, 227,
223, 173, 87, 93, 163, 59, 73, 190, 17, 37, 187, 146, 46, 51, 223, 173, 87, 93, 163, 59, 73, 190, 17, 37, 187, 146, 46, 51,
@ -175,7 +211,6 @@ test('throws on bad transaction', () => {
const account = new Account(secretKey); const account = new Account(secretKey);
const connection = new Connection(url); const connection = new Connection(url);
const errorMessage = 'Invalid request';
mockRpc.push([ mockRpc.push([
url, url,
{ {
@ -188,12 +223,7 @@ test('throws on bad transaction', () => {
134, 216, 209, 8, 211, 209, 44, 1 134, 216, 209, 8, 211, 209, 44, 1
]], ]],
}, },
{ errorResponse,
error: {
message: errorMessage,
},
result: undefined,
}
]); ]);