1 |
|
2 |
|
3 | const { mockBlock, mockInfo } = require('./helpers/fetch');
|
4 | const { constructOrejs, mockGetAccount, mockGetInfo, mockGetBlock, mockGetBlockError, mockGetTransaction } = require('./helpers/orejs');
|
5 |
|
6 | describe('eos', () => {
|
7 | let orejs;
|
8 |
|
9 | beforeAll(() => {
|
10 | orejs = constructOrejs();
|
11 | });
|
12 |
|
13 | describe('awaitTransaction', () => {
|
14 | let transaction;
|
15 | let info;
|
16 | let block;
|
17 | let spyInfo;
|
18 | let spyBlock;
|
19 |
|
20 | beforeAll(() => {
|
21 | transaction = mockGetTransaction(orejs);
|
22 | info = mockGetInfo(orejs);
|
23 | block = mockGetBlock(orejs, { block_num: info.head_block_num, transactions: [{ trx: { id: transaction.transaction_id } }] });
|
24 | spyInfo = jest.spyOn(orejs.eos.rpc, 'get_info');
|
25 | spyBlock = jest.spyOn(orejs.eos.rpc, 'get_block');
|
26 | });
|
27 |
|
28 | it('returns the transaction', async () => {
|
29 | await orejs.awaitTransaction(async () => {
|
30 | await setTimeout(() => true, 10);
|
31 | return transaction;
|
32 | }, 10, 10);
|
33 | expect(spyInfo).toHaveBeenCalledWith({});
|
34 | expect(spyBlock).toHaveBeenCalledWith(block.block_num + 1);
|
35 | });
|
36 |
|
37 | describe('when the transaction is not found', () => {
|
38 | beforeAll(() => {
|
39 | jest.clearAllMocks();
|
40 | transaction = mockGetTransaction(orejs);
|
41 | info = mockGetInfo(orejs);
|
42 | block = mockGetBlock(orejs, { block_num: info.head_block_num, transactions: [{ trx: { id: transaction.transaction_id + 1 } }] });
|
43 | });
|
44 |
|
45 | it('throws an error with the block number', async () => {
|
46 | const result = orejs.awaitTransaction(async () => {
|
47 | await setTimeout(() => true, 10);
|
48 | return transaction;
|
49 | }, 2, 10);
|
50 | await expect(result).rejects.toThrow(/Await Transaction Timeout/);
|
51 | });
|
52 | });
|
53 |
|
54 | describe('when the block is not found', () => {
|
55 | beforeAll(() => {
|
56 | jest.clearAllMocks();
|
57 | transaction = mockGetTransaction(orejs);
|
58 | info = mockGetInfo(orejs);
|
59 | block = mockGetBlockError(orejs);
|
60 | });
|
61 |
|
62 | it('throws an error with the block number', async () => {
|
63 | const result = orejs.awaitTransaction(async () => {
|
64 | await setTimeout(() => true, 10);
|
65 | return transaction;
|
66 | }, 10, 10);
|
67 | await expect(result).rejects.toThrow(/Await Transaction Failure/);
|
68 | });
|
69 | });
|
70 | });
|
71 |
|
72 | describe('hasTransaction', () => {
|
73 | let block;
|
74 | let transactionId;
|
75 | let transaction;
|
76 |
|
77 | beforeAll(() => {
|
78 | transactionId = 'asdf';
|
79 | transaction = {
|
80 | trx: {
|
81 | id: transactionId,
|
82 | },
|
83 | };
|
84 | });
|
85 |
|
86 | describe('when the block includes the transaction', () => {
|
87 | beforeAll(() => {
|
88 | block = {
|
89 | transactions: [transaction],
|
90 | };
|
91 | });
|
92 |
|
93 | it('returns true', () => {
|
94 | const hasTransaction = orejs.hasTransaction(block, transactionId);
|
95 | expect(hasTransaction).toEqual(true);
|
96 | });
|
97 | });
|
98 |
|
99 | describe('when the block does not include the transaction', () => {
|
100 | beforeAll(() => {
|
101 | block = {
|
102 | transactions: [],
|
103 | };
|
104 | });
|
105 |
|
106 | it('returns false', () => {
|
107 | const hasTransaction = orejs.hasTransaction(block, transactionId);
|
108 | expect(hasTransaction).toEqual(false);
|
109 | });
|
110 | });
|
111 | });
|
112 | });
|