Skip to content

Commit 4aaa4c9

Browse files
committed
refactor: getNanoContractHistory
1 parent 1371eca commit 4aaa4c9

File tree

6 files changed

+149
-146
lines changed

6 files changed

+149
-146
lines changed

__tests__/sagas/nanoContracts/fixtures.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,10 +119,26 @@ export const fixtures = {
119119
addressNotMine: {
120120
isReady: () => true,
121121
isAddressMine: jest.fn().mockReturnValue(false),
122+
storage: {
123+
isNanoContractRegistered: jest.fn(),
124+
registerNanoContract: jest.fn(),
125+
},
122126
},
123127
readyAndMine: {
124128
isReady: () => true,
125129
isAddressMine: jest.fn().mockReturnValue(true),
130+
storage: {
131+
isNanoContractRegistered: jest.fn(),
132+
registerNanoContract: jest.fn(),
133+
getNanoContract: jest.fn(),
134+
},
135+
},
136+
},
137+
store: {
138+
nanoContractAddressAlreadyRegistered: {
139+
ncId: '3cb032600bdf7db784800e4ea911b10676fa2f67591f82bb62628c234e771595',
140+
blueprintName: 'Bet',
141+
addresses: new Set(['HTeZeYTCv7cZ8u7pBGHkWsPwhZAuoq5j3V']),
126142
},
127143
},
128144
};
Lines changed: 60 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
import { put } from 'redux-saga/effects';
2-
import { ncApi } from '@hathor/wallet-lib';
2+
import { ncApi, addressUtils, transactionUtils } from '@hathor/wallet-lib';
33
import { jest, test, expect, beforeEach, describe } from '@jest/globals';
44
import {
5-
formatNanoContractRegistryEntry,
65
failureMessage,
76
requestHistoryNanoContract,
87
fetchHistory
@@ -14,7 +13,6 @@ import {
1413
onExceptionCaptured
1514
} from '../../../src/actions';
1615
import { STORE } from '../../../src/store';
17-
import { nanoContractKey } from '../../../src/constants';
1816
import { fixtures } from './fixtures';
1917

2018
jest.mock('@hathor/wallet-lib');
@@ -24,24 +22,32 @@ beforeEach(() => {
2422
STORE.clearItems();
2523
});
2624

27-
function addNanoContractEntry(address, ncId) {
28-
// add an entry to registeredContracts
29-
const ncEntry = formatNanoContractRegistryEntry(address, ncId);
30-
STORE.setItem(nanoContractKey.registeredContracts, { [ncEntry]: {} });
31-
return ncEntry;
32-
}
33-
3425
describe('sagas/nanoContract/fetchHistory', () => {
3526
test('success', async () => {
27+
// arrange wallet mock
28+
const mockedWallet = {
29+
getNetworkObject: jest.fn(),
30+
storage: {
31+
isAddressMine: jest.fn(),
32+
},
33+
};
3634
// arrange ncApi mock
3735
const mockedNcApi = jest.mocked(ncApi);
3836
mockedNcApi.getNanoContractHistory
3937
.mockReturnValue(fixtures.ncApi.getNanoContractHistory.successResponse);
38+
// arrange addressUtils mock
39+
const mockedAddressUtils = jest.mocked(addressUtils);
40+
mockedAddressUtils.getAddressFromPubkey
41+
.mockResolvedValue('123');
42+
// arrange transactionUtils
43+
const mockedTransactionUtils = jest.mocked(transactionUtils);
44+
mockedTransactionUtils.getTxBalance
45+
.mockResolvedValue({});
4046

4147
// call fetchHistory
4248
const count = 1;
4349
const after = null;
44-
const result = await fetchHistory(fixtures.ncId, count, after);
50+
const result = await fetchHistory(fixtures.ncId, count, after, mockedWallet);
4551

4652
// assert result is defined
4753
expect(result.history).toBeDefined();
@@ -72,58 +78,73 @@ describe('sagas/nanoContract/requestHistoryNanoContract', () => {
7278

7379
// call effect to request history
7480
const gen = requestHistoryNanoContract(nanoContractHistoryRequest({ address, ncId }));
81+
// select wallet
82+
gen.next();
83+
// feed back wallet
84+
gen.next(fixtures.wallet.readyAndMine);
7585

7686
// expect failure
77-
expect(gen.next().value)
87+
// feed back isNanoContractRegistered
88+
expect(gen.next(false).value)
7889
.toStrictEqual(put(nanoContractHistoryFailure(failureMessage.notRegistered)));
7990
});
8091

8192
test('fetch history fails', () => {
8293
// arrange Nano Contract registration inputs
8394
const { address, ncId } = fixtures;
84-
addNanoContractEntry(address, ncId);
95+
const storage = STORE.getStorage();
96+
storage.registerNanoContract(ncId, { ncId });
8597

8698
// call effect to request history
8799
const gen = requestHistoryNanoContract(nanoContractHistoryRequest({ address, ncId }));
88-
// advances to fetchHistory
89-
const fetchHistoryCall = gen.next();
90-
// advances to failure
91-
const failureCall = gen.throw(new Error('history'));
92-
const onErrorCall = gen.next();
100+
// select wallet
101+
gen.next();
102+
// feed back wallet
103+
gen.next(fixtures.wallet.readyAndMine);
104+
// feed back isNanoContractRegistered
105+
const fetchHistoryCall = gen.next(true).value;
106+
107+
// throws on fetchHistory call
108+
const failureCall = gen.throw(new Error('history')).value;
109+
const onErrorCall = gen.next().value;
93110

94111
// assert failure
95-
expect(fetchHistoryCall.value.payload.fn).toBe(fetchHistory);
96-
expect(failureCall.value).toStrictEqual(put(nanoContractHistoryFailure(failureMessage.nanoContractHistoryFailure, new Error('history'))));
97-
expect(onErrorCall.value).toStrictEqual(put(onExceptionCaptured(new Error('history'), false)));
112+
expect(fetchHistoryCall.payload.fn).toBe(fetchHistory);
113+
expect(failureCall).toStrictEqual(put(nanoContractHistoryFailure(failureMessage.nanoContractHistoryFailure, new Error('history'))));
114+
expect(onErrorCall).toStrictEqual(put(onExceptionCaptured(new Error('history'), false)));
98115
});
99116

100117
test('history with success', () => {
101118
// arrange Nano Contract registration inputs
102119
const { address, ncId } = fixtures;
103-
const ncEntry = addNanoContractEntry(address, ncId);
104120

105121
// call effect to request history
106122
const gen = requestHistoryNanoContract(nanoContractHistoryRequest({ address, ncId }));
107-
// advances to fetchHistory
108-
const fetchHistoryCall = gen.next();
109-
// feed back fetchHistory and advances to history load
110-
const historyLoadCall = gen.next(fixtures.ncSaga.fetchHistory.successResponse);
111-
// advances to success
112-
const sucessCall = gen.next();
123+
// select wallet
124+
gen.next();
125+
// feed back wallet
126+
gen.next(fixtures.wallet.readyAndMine);
127+
// feed back isNanoContractRegistered
128+
const fetchHistoryCall = gen.next(true).value;
129+
// feed back fetchHistory
130+
gen.next(fixtures.ncSaga.fetchHistory.successResponse);
131+
// feed back getNanoContract
132+
gen.next({ ncId });
133+
// call registerNanoContract and yield put nanoContractHistoryLoad
134+
const historyLoadCall = gen.next().value;
135+
136+
const sucessCall = gen.next().value;
113137

114138
// assert success
115-
expect(fetchHistoryCall.value.payload.fn).toBe(fetchHistory);
116-
expect(historyLoadCall.value.payload).toHaveProperty('action.payload.history');
117-
expect(historyLoadCall.value.payload).toHaveProperty('action.payload.ncEntry');
118-
expect(sucessCall.value).toStrictEqual(put(nanoContractHistorySuccess()));
139+
expect(fetchHistoryCall.payload.fn).toBe(fetchHistory);
140+
expect(historyLoadCall.payload).toHaveProperty('action.payload.ncId');
141+
expect(historyLoadCall.payload).toHaveProperty('action.payload.history');
142+
expect(historyLoadCall.payload.action.payload.ncId).toStrictEqual(ncId);
143+
expect(historyLoadCall.payload.action.payload.history).toStrictEqual(
144+
fixtures.ncSaga.fetchHistory.successResponse.history
145+
);
146+
expect(sucessCall).toStrictEqual(put(nanoContractHistorySuccess()));
119147
// assert termination
120148
expect(gen.next().value).toBeUndefined();
121-
122-
// assert nano contract history persistence
123-
const registeredContracts = STORE.getItem(nanoContractKey.registeredContracts);
124-
expect(registeredContracts).toBeDefined();
125-
expect(registeredContracts[ncEntry]).toHaveProperty('history');
126-
expect(registeredContracts[ncEntry].history)
127-
.toStrictEqual(fixtures.ncSaga.fetchHistory.successResponse.history);
128149
});
129150
});

__tests__/sagas/nanoContracts/registerNanoContract.test.js

Lines changed: 0 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -3,70 +3,8 @@ import { jest, test, expect, beforeEach, describe } from '@jest/globals';
33
import { registerNanoContract, failureMessage } from '../../../src/sagas/nanoContract';
44
import { nanoContractRegisterFailure, nanoContractRegisterRequest, onExceptionCaptured, types } from '../../../src/actions';
55
import { STORE } from '../../../src/store';
6-
import { nanoContractKey } from '../../../src/constants';
76
import { fixtures } from './fixtures';
87

9-
const fixtures = {
10-
address: 'HTeZeYTCv7cZ8u7pBGHkWsPwhZAuoq5j3V',
11-
ncId: '3cb032600bdf7db784800e4ea911b10676fa2f67591f82bb62628c234e771595',
12-
ncApi: {
13-
getNanoContractState: {
14-
successResponse: {
15-
success: true,
16-
nc_id: '3cb032600bdf7db784800e4ea911b10676fa2f67591f82bb62628c234e771595',
17-
blueprint_name: 'Bet',
18-
fields: {
19-
token_uid: { value: '00' },
20-
total: { value: 300 },
21-
final_result: { value: '1x0' },
22-
oracle_script: { value: '76a91441c431ff7ad5d6ce5565991e3dcd5d9106cfd1e288ac' },
23-
'withdrawals.a\'Wi8zvxdXHjaUVAoCJf52t3WovTZYcU9aX6\'': { value: 300 },
24-
'address_details.a\'Wi8zvxdXHjaUVAoCJf52t3WovTZYcU9aX6\'': { value: { '1x0': 100 } },
25-
}
26-
}
27-
}
28-
},
29-
ncSaga: {
30-
getNanoContractState: {
31-
errorResponse: new Error('API call error'),
32-
successResponse: {
33-
success: true,
34-
nc_id: '3cb032600bdf7db784800e4ea911b10676fa2f67591f82bb62628c234e771595',
35-
blueprint_name: 'Bet',
36-
fields: {
37-
token_uid: { value: '00' },
38-
total: { value: 300 },
39-
final_result: { value: '1x0' },
40-
oracle_script: { value: '76a91441c431ff7ad5d6ce5565991e3dcd5d9106cfd1e288ac' },
41-
'withdrawals.a\'Wi8zvxdXHjaUVAoCJf52t3WovTZYcU9aX6\'': { value: 300 },
42-
'address_details.a\'Wi8zvxdXHjaUVAoCJf52t3WovTZYcU9aX6\'': { value: { '1x0': 100 } },
43-
}
44-
}
45-
},
46-
},
47-
wallet: {
48-
notReady: {
49-
isReady: () => false,
50-
},
51-
addressNotMine: {
52-
isReady: () => true,
53-
isAddressMine: jest.fn().mockReturnValue(false),
54-
storage: {
55-
isNanoContractRegistered: jest.fn(),
56-
registerNanoContract: jest.fn(),
57-
},
58-
},
59-
readyAndMine: {
60-
isReady: () => true,
61-
isAddressMine: jest.fn().mockReturnValue(true),
62-
storage: {
63-
isNanoContractRegistered: jest.fn(),
64-
registerNanoContract: jest.fn(),
65-
},
66-
},
67-
},
68-
};
69-
708
beforeEach(() => {
719
jest.clearAllMocks();
7210
STORE.clearItems();

src/actions.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1039,7 +1039,7 @@ export const nanoContractHistoryRequest = (ncEntry) => ({
10391039
/**
10401040
* Nano Contract history's chunk to load.
10411041
* @param {{
1042-
* ncEntry: string;
1042+
* ncId: string;
10431043
* history: Object;
10441044
* }} chunkToLoad History chunk to load into store and redux.
10451045
*/

src/reducers/reducer.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1299,7 +1299,7 @@ export const onNanoContractRegisterSuccess = (state, { payload }) => ({
12991299
* @param {Object} state
13001300
* @param {{
13011301
* payload: {
1302-
* entryKey: string;
1302+
* ncId: string;
13031303
* history: {
13041304
* txId: string;
13051305
* timestamp: number;
@@ -1317,8 +1317,8 @@ export const onNanoContractHistoryLoad = (state, { payload }) => ({
13171317
...state.nanoContract,
13181318
contractsHistory: {
13191319
...state.nanoContract.contractsHistory,
1320-
[payload.entryKey]: [
1321-
...(state.nanoContract.contractsHistory[payload.entryKey] || []),
1320+
[payload.ncId]: [
1321+
...(state.nanoContract.contractsHistory[payload.ncId] || []),
13221322
...payload.history
13231323
],
13241324
},

0 commit comments

Comments
 (0)