Skip to content

Commit f3b368d

Browse files
author
Ian Tan
committed
test(node): add test case for node.createTransaction
1 parent 13d7c9f commit f3b368d

File tree

3 files changed

+49
-4
lines changed

3 files changed

+49
-4
lines changed

jest-setup.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,5 @@
77
// another public or private blockchain network. This source code is provided ‘as is’ and no
88
// warranties are given as to title or non-infringement, merchantability or fitness for purpose
99
// and, to the extent permitted by law, all liability for your use of the code is disclaimed.
10-
const crypto = require('@trust/webcrypto');
11-
window.crypto = crypto;
10+
window.crypto = require('@trust/webcrypto');
11+
window.fetch = require('jest-fetch-mock');

src/__tests__/node.spec.ts

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import elliptic from 'elliptic';
2+
import BN from 'bn.js';
3+
import {pairs} from './keypairs.fixtures';
4+
import Znode from '../node';
5+
import jestFetch from 'jest-fetch-mock';
6+
7+
const secp256k1 = elliptic.ec('secp256k1');
8+
const node = new Znode({url: 'http://localhost:4201'});
9+
const fetchMock = fetch as typeof jestFetch;
10+
11+
describe('node', () => {
12+
beforeEach(() => {
13+
fetchMock.resetMocks();
14+
});
15+
16+
it('should be able to broadcast a well-formed transaction', (done) => {
17+
const privateKey = pairs[1].private;
18+
const publicKey = secp256k1
19+
.keyFromPrivate(privateKey, 'hex')
20+
.getPublic(true, 'hex');
21+
22+
const tx = {
23+
version: 8,
24+
nonce: 8,
25+
to: pairs[0].digest.slice(0, 40),
26+
from: pairs[1].digest.slice(0, 40),
27+
pubKey: publicKey,
28+
amount: new BN('888', 10),
29+
gasPrice: 8,
30+
gasLimit: 88,
31+
code: '',
32+
data: '',
33+
};
34+
35+
// setup mock response here
36+
const response = JSON.stringify({some: 'random_data'});
37+
fetchMock.mockResponseOnce(response);
38+
39+
node.createTransaction(tx, (err, res) => {
40+
expect(err).toBeFalsy();
41+
expect(JSON.stringify(res)).toEqual(response);
42+
done();
43+
});
44+
});
45+
});

src/node.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
// another public or private blockchain network. This source code is provided ‘as is’ and no
88
// warranties are given as to title or non-infringement, merchantability or fitness for purpose
99
// and, to the extent permitted by law, all liability for your use of the code is disclaimed.
10-
import fetch from 'cross-fetch';
10+
import BN from 'bn.js';
1111
import { validateArgs } from './util';
1212
import * as util from './util';
1313

@@ -61,7 +61,7 @@ export default class ZNode {
6161
validateArgs(args, {
6262
to: [util.isAddress],
6363
pubKey: [util.isPubkey],
64-
amount: [util.isNumber],
64+
amount: [BN.isBN],
6565
gasPrice: [util.isNumber],
6666
gasLimit: [util.isNumber],
6767
});

0 commit comments

Comments
 (0)