1
1
import { put } from 'redux-saga/effects' ;
2
- import { ncApi } from '@hathor/wallet-lib' ;
2
+ import { ncApi , addressUtils , transactionUtils } from '@hathor/wallet-lib' ;
3
3
import { jest , test , expect , beforeEach , describe } from '@jest/globals' ;
4
4
import {
5
- formatNanoContractRegistryEntry ,
6
5
failureMessage ,
7
6
requestHistoryNanoContract ,
8
7
fetchHistory
@@ -14,7 +13,6 @@ import {
14
13
onExceptionCaptured
15
14
} from '../../../src/actions' ;
16
15
import { STORE } from '../../../src/store' ;
17
- import { nanoContractKey } from '../../../src/constants' ;
18
16
import { fixtures } from './fixtures' ;
19
17
20
18
jest . mock ( '@hathor/wallet-lib' ) ;
@@ -24,24 +22,32 @@ beforeEach(() => {
24
22
STORE . clearItems ( ) ;
25
23
} ) ;
26
24
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
-
34
25
describe ( 'sagas/nanoContract/fetchHistory' , ( ) => {
35
26
test ( 'success' , async ( ) => {
27
+ // arrange wallet mock
28
+ const mockedWallet = {
29
+ getNetworkObject : jest . fn ( ) ,
30
+ storage : {
31
+ isAddressMine : jest . fn ( ) ,
32
+ } ,
33
+ } ;
36
34
// arrange ncApi mock
37
35
const mockedNcApi = jest . mocked ( ncApi ) ;
38
36
mockedNcApi . getNanoContractHistory
39
37
. 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 ( { } ) ;
40
46
41
47
// call fetchHistory
42
48
const count = 1 ;
43
49
const after = null ;
44
- const result = await fetchHistory ( fixtures . ncId , count , after ) ;
50
+ const result = await fetchHistory ( fixtures . ncId , count , after , mockedWallet ) ;
45
51
46
52
// assert result is defined
47
53
expect ( result . history ) . toBeDefined ( ) ;
@@ -72,58 +78,73 @@ describe('sagas/nanoContract/requestHistoryNanoContract', () => {
72
78
73
79
// call effect to request history
74
80
const gen = requestHistoryNanoContract ( nanoContractHistoryRequest ( { address, ncId } ) ) ;
81
+ // select wallet
82
+ gen . next ( ) ;
83
+ // feed back wallet
84
+ gen . next ( fixtures . wallet . readyAndMine ) ;
75
85
76
86
// expect failure
77
- expect ( gen . next ( ) . value )
87
+ // feed back isNanoContractRegistered
88
+ expect ( gen . next ( false ) . value )
78
89
. toStrictEqual ( put ( nanoContractHistoryFailure ( failureMessage . notRegistered ) ) ) ;
79
90
} ) ;
80
91
81
92
test ( 'fetch history fails' , ( ) => {
82
93
// arrange Nano Contract registration inputs
83
94
const { address, ncId } = fixtures ;
84
- addNanoContractEntry ( address , ncId ) ;
95
+ const storage = STORE . getStorage ( ) ;
96
+ storage . registerNanoContract ( ncId , { ncId } ) ;
85
97
86
98
// call effect to request history
87
99
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 ;
93
110
94
111
// 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 ) ) ) ;
98
115
} ) ;
99
116
100
117
test ( 'history with success' , ( ) => {
101
118
// arrange Nano Contract registration inputs
102
119
const { address, ncId } = fixtures ;
103
- const ncEntry = addNanoContractEntry ( address , ncId ) ;
104
120
105
121
// call effect to request history
106
122
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 ;
113
137
114
138
// 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 ( ) ) ) ;
119
147
// assert termination
120
148
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 ) ;
128
149
} ) ;
129
150
} ) ;
0 commit comments