1 |
|
2 |
|
3 |
|
4 |
|
5 | const { expectFetch, mock, mockInfo } = require('../helpers/fetch');
|
6 | const { mockAction, mockOptions } = require('../helpers/eos');
|
7 | const { constructOrejs, mockGetBlock, mockGetInfo, mockGetTransaction } = require('../helpers/orejs');
|
8 |
|
9 | describe('cpu', () => {
|
10 | let orejs;
|
11 |
|
12 | beforeAll(() => {
|
13 | orejs = constructOrejs();
|
14 | });
|
15 |
|
16 | describe('getCpuBalance', () => {
|
17 | let cpuBalance;
|
18 |
|
19 | beforeEach(() => {
|
20 | cpuBalance = 30;
|
21 |
|
22 | fetch.resetMocks();
|
23 | fetch.mockResponses(mock([`${cpuBalance}.0000 CPU`]));
|
24 | orejs = constructOrejs({ fetch });
|
25 | });
|
26 |
|
27 | it('returns the cpu balance', async () => {
|
28 | cpuBalance = await orejs.getCpuBalance(ORE_TESTA_ACCOUNT_NAME);
|
29 | expectFetch(`${ORE_NETWORK_URI}/v1/chain/get_currency_balance`);
|
30 | expect(cpuBalance).toEqual(cpuBalance);
|
31 | });
|
32 | });
|
33 |
|
34 | describe('approveCpu', () => {
|
35 | let cpuBalance;
|
36 | let memo;
|
37 | let spyTransaction;
|
38 | let transaction;
|
39 |
|
40 | beforeEach(() => {
|
41 | memo = 'approve CPU transfer';
|
42 | cpuBalance = 10;
|
43 | fetch.resetMocks();
|
44 | fetch.mockResponses(mock([`${cpuBalance}.0000 CPU`]));
|
45 | orejs = constructOrejs({ fetch });
|
46 | transaction = mockGetTransaction(orejs);
|
47 | spyTransaction = jest.spyOn(orejs.eos, 'transact');
|
48 | });
|
49 |
|
50 | describe('when authorized', () => {
|
51 | it('returns', async () => {
|
52 | mockGetInfo(orejs);
|
53 | mockGetBlock(orejs);
|
54 | const result = await orejs.approveCpu(ORE_OWNER_ACCOUNT_NAME, ORE_TESTA_ACCOUNT_NAME, cpuBalance, memo);
|
55 | expect(spyTransaction).toHaveBeenCalledWith({ actions: [mockAction({ account: 'token.ore', name: 'approve' })] }, mockOptions());
|
56 | });
|
57 | });
|
58 |
|
59 | describe('when unauthorized', () => {
|
60 | xit('throws', () => {
|
61 |
|
62 | const result = orejs.approveCpu(ORE_TESTA_ACCOUNT_NAME, ORE_TESTA_ACCOUNT_NAME, cpuBalance);
|
63 | expect(result).rejects.toThrow(/unauthorized/);
|
64 | });
|
65 | });
|
66 | });
|
67 |
|
68 | describe('getApprovedCpuBalance', () => {
|
69 | let cpuBalance;
|
70 | let memo;
|
71 | let spyTransaction;
|
72 | let transaction;
|
73 |
|
74 | beforeEach(() => {
|
75 | cpuBalance = 10;
|
76 | memo = 'approve CPU transfer';
|
77 | fetch.resetMocks();
|
78 | });
|
79 |
|
80 | describe('when approved', () => {
|
81 | beforeEach(() => {
|
82 | fetch.mockResponses(mock([`${cpuBalance}.0000 CPU`]), mock({
|
83 | rows: [{
|
84 | to: ORE_TESTA_ACCOUNT_NAME,
|
85 | quantity: '10.0000 CPU',
|
86 | }],
|
87 | }));
|
88 | orejs = constructOrejs({ fetch });
|
89 | transaction = mockGetTransaction(orejs);
|
90 | });
|
91 |
|
92 | it('returns', async () => {
|
93 | mockGetInfo(orejs);
|
94 | mockGetBlock(orejs);
|
95 | await orejs.approveCpu(ORE_OWNER_ACCOUNT_NAME, ORE_TESTA_ACCOUNT_NAME, cpuBalance, memo);
|
96 | const approveAmount = await orejs.getApprovedCpuBalance(ORE_OWNER_ACCOUNT_NAME, ORE_TESTA_ACCOUNT_NAME);
|
97 | expect(approveAmount).toEqual(`${cpuBalance}.0000 CPU`);
|
98 | });
|
99 | });
|
100 |
|
101 | describe('when not approved', () => {
|
102 | beforeEach(() => {
|
103 | fetch.mockResponses(mock({
|
104 | rows: [{
|
105 | to: ORE_TESTB_ACCOUNT_NAME,
|
106 | quantity: '0.0000 CPU',
|
107 | }],
|
108 | }));
|
109 | orejs = constructOrejs({ fetch });
|
110 | });
|
111 |
|
112 | it('returns', async () => {
|
113 | const approveAmount = await orejs.getApprovedCpuBalance(ORE_OWNER_ACCOUNT_NAME, ORE_TESTB_ACCOUNT_NAME);
|
114 | expect(approveAmount).toEqual('0.0000 CPU');
|
115 | });
|
116 | });
|
117 | });
|
118 |
|
119 | describe('transferCpu', () => {
|
120 | let cpuBalance;
|
121 | let spyTransaction;
|
122 | let transaction;
|
123 |
|
124 | beforeEach(() => {
|
125 | cpuBalance = 10;
|
126 | transaction = mockGetTransaction(orejs);
|
127 | spyTransaction = jest.spyOn(orejs.eos, 'transact');
|
128 | });
|
129 |
|
130 | describe('when authorized', () => {
|
131 | it('returns', async () => {
|
132 | const result = await orejs.transferCpu(ORE_OWNER_ACCOUNT_NAME, ORE_TESTA_ACCOUNT_NAME, cpuBalance);
|
133 | expect(spyTransaction).toHaveBeenCalledWith({ actions: [mockAction({ account: 'token.ore', name: 'transfer' })] }, mockOptions());
|
134 | });
|
135 | });
|
136 |
|
137 | describe('when unauthorized', () => {
|
138 | xit('throws', () => {
|
139 | contract.approve.mockImplementationOnce(() => Promise.reject(new Error('unauthorized')));
|
140 |
|
141 | const result = orejs.transferCpu(ORE_TESTA_ACCOUNT_NAME, ORE_TESTA_ACCOUNT_NAME, cpuBalance);
|
142 | expect(result).rejects.toThrow(/unauthorized/);
|
143 | });
|
144 | });
|
145 | });
|
146 | });
|