-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathTokenPocket.test.ts
182 lines (154 loc) · 5.63 KB
/
TokenPocket.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
import './__mocks__/tp-eosjs.mock'
import { walletResponse } from './__mocks__/data.mock'
import { Chain, RpcEndpoint, UALErrorType } from 'universal-authenticator-library'
import * as tp from 'tp-eosjs'
import { Name } from './interfaces'
import { TokenPocket } from './TokenPocket'
import { UALTokenPocketError } from './UALTokenPocketError'
describe('TokenPocket', () => {
describe('init', () => {
beforeEach(() => {
tp.isConnected.mockReturnValue(true)
})
it('loading should be false if Token Pocket is connected', async () => {
const tokenPocket = new TokenPocket([] as Chain[])
await tokenPocket.init()
expect(tokenPocket.isLoading()).toBe(false)
expect(tp.isConnected).toHaveBeenCalled()
})
it('loading should be false if Token Pocket is not connected', async () => {
tp.isConnected.mockReturnValue(false)
const tokenPocket = new TokenPocket([] as Chain[])
await tokenPocket.init()
expect(tokenPocket.isLoading()).toBe(false)
expect(tp.isConnected).toHaveBeenCalled()
})
it('sets initError if it cant connect to Token Pocket', async () => {
tp.isConnected.mockReturnValue(false)
const tokenPocket = new TokenPocket([] as Chain[])
await tokenPocket.init()
const error = tokenPocket.getError() as UALTokenPocketError
expect(error.source).toEqual(Name)
expect(error.message).toEqual('Error occurred during autologin')
expect(error.type).toEqual(UALErrorType.Initialization)
expect(error.cause).toEqual(new Error('Unable to connect'))
})
})
describe('shouldRender', () => {
const chains = [
{
chainId: 'aca376f206b8fc25a6ed44dbdc66547c36c6c33e3a119ffbeaef943642f0e906',
rpcEndpoints: [] as RpcEndpoint[]
}
]
beforeEach(() => {
tp.isConnected.mockReturnValue(true)
})
it('should return true if all given chains are supported and Token Pocket is connected', () => {
const tokenPocket = new TokenPocket(chains)
const shouldRender = tokenPocket.shouldRender()
expect(shouldRender).toBe(true)
})
it('should return false if a given chain is not supported', () => {
const chainsWithUnsupportedChain = [
{
chainId: 'testChain',
rpcEndpoints: [] as RpcEndpoint[]
},
{
chainId: 'aca376f206b8fc25a6ed44dbdc66547c36c6c33e3a119ffbeaef943642f0e906',
rpcEndpoints: [] as RpcEndpoint[]
}
]
const tokenPocket = new TokenPocket(chainsWithUnsupportedChain)
const shouldRender = tokenPocket.shouldRender()
expect(shouldRender).toBe(false)
})
it('returns false if Token Pocket is not connected', async () => {
tp.isConnected.mockReturnValue(false)
const tokenPocket = new TokenPocket(chains)
const shouldRender = tokenPocket.shouldRender()
expect(shouldRender).toBe(false)
})
it('should return false if a given chain is not supported and Token Pocket is not connected', () => {
const chainsWithUnsupportedChain = [
{
chainId: 'testChain',
rpcEndpoints: [] as RpcEndpoint[]
},
{
chainId: 'aca376f206b8fc25a6ed44dbdc66547c36c6c33e3a119ffbeaef943642f0e906',
rpcEndpoints: [] as RpcEndpoint[]
}
]
tp.isConnected.mockReturnValue(false)
const tokenPocket = new TokenPocket(chainsWithUnsupportedChain)
const shouldRender = tokenPocket.shouldRender()
expect(shouldRender).toBe(false)
})
})
describe('login', () => {
beforeEach(() => {
tp.getCurrentWallet.mockReturnValue(walletResponse)
})
it('should get the accounts', async () => {
const tokenPocket = new TokenPocket([] as Chain[])
const accounts = await tokenPocket.login()
expect(accounts.length).toBe(1)
expect(tp.getCurrentWallet).toHaveBeenCalled()
})
it('should get the accounts names', async () => {
const tokenPocket = new TokenPocket([] as Chain[])
const accounts = await tokenPocket.login()
const accountName = await accounts[0].getAccountName()
expect(accountName).toBe(walletResponse.data.name)
expect(tp.getCurrentWallet).toHaveBeenCalled()
})
it('throws UALError if result is not set', async () => {
tp.getCurrentWallet.mockReturnValue({
result: false,
data: {},
msg: 'test'
})
const tokenPocket = new TokenPocket([] as Chain[])
let didThrow = true
try {
await tokenPocket.login()
didThrow = false
} catch (e) {
const ex = e as UALTokenPocketError
expect(ex.source).toEqual(Name)
expect(ex.type).toEqual(UALErrorType.Login)
expect(ex.cause).toEqual(new Error('No result returned'))
}
expect(didThrow).toBe(true)
})
it('throws UALError on api error', async () => {
const errorMsg = 'Unable to login'
tp.getCurrentWallet.mockImplementation(() => {
throw new Error(errorMsg)
})
const tokenPocket = new TokenPocket([] as Chain[])
let didThrow = true
try {
await tokenPocket.login()
didThrow = false
} catch (e) {
const ex = e as UALTokenPocketError
expect(ex.source).toEqual(Name)
expect(ex.type).toEqual(UALErrorType.Login)
expect(ex.cause).toEqual(new Error(errorMsg))
}
expect(didThrow).toBe(true)
})
})
describe('get authenticator name', () => {
it('should be able to get authenticator name', () => {
const tokenPocket = new TokenPocket([] as Chain[])
expect(tokenPocket.getName()).toBe(Name)
})
})
afterEach(() => {
jest.clearAllMocks()
})
})