/
dispatcher.test.ts
130 lines (113 loc) · 3.17 KB
/
dispatcher.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
// eslint-disable-next-line max-classes-per-file
import { EventEmitter } from 'events'
import { BigNumber, Wallet } from 'ethers'
import Dispatcher from '../../src/fulfillment/dispatcher'
import { vi } from 'vitest'
const lockAddress = '0x5Cd3FC283c42B4d5083dbA4a6bE5ac58fC0f0267'
const recipient = '0xAaAdEED4c0B861cB36f4cE006a9C90BA2E43fdc2'
const standardLock = {
asOf: 227,
balance: '0.01',
expirationDuration: 2592000,
keyPrice: '0.01',
maxNumberOfKeys: 10,
outstandingKeys: 1,
owner: '0xAaAdEED4c0B861cB36f4cE006a9C90BA2E43fdc2',
}
const mockWeb3Service = {
getLock: vi
.fn()
.mockResolvedValue(standardLock)
.mockResolvedValueOnce(standardLock),
isKeyGranter: vi.fn().mockResolvedValue(true),
}
class MockLockContract extends EventEmitter {
renewMembershipFor = vi.fn()
}
const mockLockContract = new MockLockContract()
class MockWalletService extends EventEmitter {
connect = vi.fn()
purchaseKey = vi.fn(() => {
this.emit(
'transaction.new',
'a transaction hash',
'the sender',
'the recipient',
'some data'
)
})
setUnlockAddress = vi.fn()
grantKeys = vi.fn()
getLockContract = vi.fn(async () => mockLockContract)
renewMembershipFor = vi.fn(async () => ({ hash: 'txhash' }))
}
const mockWalletService = new MockWalletService()
vi.mock('@unlock-protocol/unlock-js', () => ({
Web3Service: function Web3Service() {
return mockWeb3Service
},
WalletService: function WalletService() {
return mockWalletService
},
}))
vi.mock('@unlock-protocol/networks', () => ({
default: {
31337: {
provider: 'http://127.0.0.1:8545',
publicProvider: 'http://127.0.0.1:8545',
},
},
}))
vi.mock('../../src/utils/gasSettings', () => ({
getGasSettings: vi.fn(() => {
return {
maxFeePerGas: BigNumber.from(40000000000),
maxPriorityFeePerGas: BigNumber.from(40000000000),
}
}),
}))
describe('Dispatcher', () => {
describe('grantKey', () => {
it('should call grant keys on the key granter', async () => {
expect.assertions(1)
const callback = vi.fn()
await new Dispatcher().grantKeys(
lockAddress,
[{ recipient }],
31337,
callback
)
expect(mockWalletService.grantKeys).toBeCalledWith(
{
lockAddress: '0x5Cd3FC283c42B4d5083dbA4a6bE5ac58fC0f0267',
expirations: [],
keyManagers: [],
recipients: ['0xAaAdEED4c0B861cB36f4cE006a9C90BA2E43fdc2'],
},
{
maxFeePerGas: BigNumber.from(40000000000),
maxPriorityFeePerGas: BigNumber.from(40000000000),
},
callback
)
})
})
describe('renewMembershipFor', () => {
it('should call the function from walletService', async () => {
expect.assertions(1)
const keyId = '1'
await new Dispatcher().renewMembershipFor(31337, lockAddress, keyId)
expect(mockWalletService.renewMembershipFor).toBeCalledWith(
{
lockAddress,
referrer: undefined,
tokenId: keyId,
},
{
maxFeePerGas: BigNumber.from(40000000000),
maxPriorityFeePerGas: BigNumber.from(40000000000),
}
)
})
})
})