@@ -13,6 +13,12 @@ const params1 = {
13
13
amount : new BigNumber ( 1 ) ,
14
14
spender : '0x1' ,
15
15
owner : '0x3' ,
16
+ } ;
17
+
18
+ const params2 = {
19
+ amount : new BigNumber ( 1 ) ,
20
+ spender : '0x1' ,
21
+ owner : '0x3' ,
16
22
tokenAddress : '0x2222222222222222222222222222222222222222' ,
17
23
} ;
18
24
@@ -21,34 +27,51 @@ describe('ApproveErc20', () => {
21
27
let contextMock : MockManager < contextObject . Context > ;
22
28
let wrappersMock : MockManager < polymathBaseObject . PolymathBase > ;
23
29
let erc20Mock : MockManager < contractWrappersObject . ERC20 > ;
24
- let wrapperMockStub : SinonStub < any , any > ;
30
+ let polyTokenMock : MockManager < contractWrappersObject . PolyToken > ;
31
+
32
+ let checkPolyAllowanceStub : SinonStub < any , any > ;
33
+ let checkPolyAddressStub : SinonStub < any , any > ;
34
+ let checkPolyBalanceStub : SinonStub < any , any > ;
35
+
36
+ let checkErc20AllowanceStub : SinonStub < any , any > ;
37
+ let checkErc20AddressStub : SinonStub < any , any > ;
38
+ let checkErc20BalanceStub : SinonStub < any , any > ;
25
39
26
40
beforeAll ( ( ) => {
27
41
// Mock the context and wrappers, including currentWallet and balanceOf to test ApproveErc20
28
42
contextMock = ImportMock . mockClass ( contextObject , 'Context' ) ;
29
43
wrappersMock = ImportMock . mockClass ( polymathBaseObject , 'PolymathBase' ) ;
30
44
erc20Mock = ImportMock . mockClass ( contractWrappersObject , 'ERC20' ) ;
31
- erc20Mock . mock ( 'balanceOf' , Promise . resolve ( params1 . amount ) ) ;
32
- wrapperMockStub = wrappersMock . mock ( 'getERC20TokenWrapper' , erc20Mock . getMockInstance ( ) ) ;
45
+ polyTokenMock = ImportMock . mockClass ( contractWrappersObject , 'PolyToken' ) ;
46
+
47
+ // Setup poly token
48
+ checkPolyBalanceStub = polyTokenMock . mock ( 'balanceOf' , Promise . resolve ( new BigNumber ( 2 ) ) ) ;
49
+ checkPolyAddressStub = polyTokenMock . mock ( 'address' , Promise . resolve ( params1 . spender ) ) ;
50
+ checkPolyAllowanceStub = polyTokenMock . mock ( 'allowance' , Promise . resolve ( new BigNumber ( 0 ) ) ) ;
51
+
33
52
contextMock . set ( 'contractWrappers' , wrappersMock . getMockInstance ( ) ) ;
53
+ wrappersMock . set ( 'polyToken' , polyTokenMock . getMockInstance ( ) ) ;
54
+ wrappersMock . mock ( 'isTestnet' , Promise . resolve ( false ) ) ;
34
55
const ownerPromise = new Promise < string > ( ( resolve , reject ) => {
35
56
resolve ( ) ;
36
57
} ) ;
37
58
contextMock . set ( 'currentWallet' , new Wallet ( { address : ( ) => ownerPromise } ) ) ;
38
-
39
- // Instantiate ApproveErc20
40
- target = new ApproveErc20 ( params1 , contextMock . getMockInstance ( ) ) ;
41
59
} ) ;
42
60
43
61
describe ( 'Types' , ( ) => {
44
62
test ( 'should extend procedure and have ApproveErc20 type' , async ( ) => {
63
+ // Instantiate ApproveErc20
64
+ target = new ApproveErc20 ( params1 , contextMock . getMockInstance ( ) ) ;
45
65
expect ( target instanceof Procedure ) . toBe ( true ) ;
46
66
expect ( target . type ) . toBe ( 'ApproveErc20' ) ;
47
67
} ) ;
48
68
} ) ;
49
69
50
- describe ( 'createCheckpoint' , ( ) => {
51
- test ( 'should send the transaction to createCheckpoint' , async ( ) => {
70
+ describe ( 'ApproveErc20' , ( ) => {
71
+ test ( 'should send the transaction to ApproveErc20 with a poly token' , async ( ) => {
72
+ // Instantiate ApproveErc20
73
+ target = new ApproveErc20 ( params1 , contextMock . getMockInstance ( ) ) ;
74
+
52
75
// Real call
53
76
await target . prepareTransactions ( ) ;
54
77
@@ -57,7 +80,32 @@ describe('ApproveErc20', () => {
57
80
expect ( sinon . spy ( target , 'prepareTransactions' ) . calledOnce ) ;
58
81
expect ( sinon . spy ( target , 'addProcedure' ) . calledOnce ) ;
59
82
expect ( sinon . spy ( target , 'addTransaction' ) . calledOnce ) ;
60
- expect ( wrapperMockStub ( ) . calledOnce ) ;
83
+ expect ( checkPolyBalanceStub ( ) . calledOnce ) ;
84
+ expect ( checkPolyAllowanceStub ( ) . calledOnce ) ;
85
+ expect ( checkPolyAddressStub ( ) . calledOnce ) ;
61
86
} ) ;
62
87
} ) ;
88
+
89
+ test ( 'should send the transaction to createCheckpoint with a custom erc20 token' , async ( ) => {
90
+ // Used by custom erc20 token
91
+ checkErc20BalanceStub = erc20Mock . mock ( 'balanceOf' , Promise . resolve ( params2 . amount ) ) ;
92
+ checkErc20AddressStub = erc20Mock . mock ( 'address' , Promise . resolve ( params2 . spender ) ) ;
93
+ checkErc20AllowanceStub = erc20Mock . mock ( 'allowance' , Promise . resolve ( new BigNumber ( 0 ) ) ) ;
94
+
95
+ const wrapperMockStub = wrappersMock . mock ( 'getERC20TokenWrapper' , erc20Mock . getMockInstance ( ) ) ;
96
+ // Instantiate ApproveErc20
97
+ target = new ApproveErc20 ( params2 , contextMock . getMockInstance ( ) ) ;
98
+ // Real call
99
+ await target . prepareTransactions ( ) ;
100
+
101
+ // Verifications
102
+ expect ( sinon . spy ( target , 'prepare' ) . calledOnce ) ;
103
+ expect ( sinon . spy ( target , 'prepareTransactions' ) . calledOnce ) ;
104
+ expect ( sinon . spy ( target , 'addProcedure' ) . calledOnce ) ;
105
+ expect ( sinon . spy ( target , 'addTransaction' ) . calledOnce ) ;
106
+ expect ( wrapperMockStub ( ) . calledOnce ) ;
107
+ expect ( checkErc20BalanceStub ( ) . calledOnce ) ;
108
+ expect ( checkErc20AllowanceStub ( ) . calledOnce ) ;
109
+ expect ( checkErc20AddressStub ( ) . calledOnce ) ;
110
+ } ) ;
63
111
} ) ;
0 commit comments