This repository was archived by the owner on Jan 17, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathBlockchain.spec.ts
More file actions
155 lines (153 loc) · 5.74 KB
/
Copy pathBlockchain.spec.ts
File metadata and controls
155 lines (153 loc) · 5.74 KB
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
import { stringToHex } from '@polkadot/util'
import { Codec } from '@polkadot/types/types'
import Accumulator from '../attestation/Accumulator'
import BlockchainError from './BlockchainError'
import api from './__mocks__/BlockchainApi'
import BlockchainMock from './__mocks__/Blockchain'
import connect from '../blockchainApiConnection/BlockchainApiConnection'
jest.mock('../blockchainApiConnection/BlockchainApiConnection')
describe('chain mocks', () => {
const dummyAddress = 'dummyAddress'
const emptyValue = 0x00
describe('Positive tests', () => {
it('Should connect', async () => {
await expect(connect()).resolves.toStrictEqual(BlockchainMock)
})
it('Should getAccumulatorCount', async () => {
const count = await BlockchainMock.getAccumulatorCount(dummyAddress)
expect(count).toBe(dummyAddress.length)
})
it('Should getAccumulator', async () => {
const accu: Accumulator = await BlockchainMock.getAccumulator(
dummyAddress,
1
)
expect(accu.toString()).toBe(dummyAddress)
})
it('Should getLatestAccumulator', async () => {
api.query.portablegabi.accumulatorCount.mockResolvedValue(
Promise.resolve(100)
)
api.query.portablegabi.accumulatorList.mockResolvedValueOnce(
(stringToHex('dummyAccumulator') as unknown) as Promise<Codec>
)
const accumulator: Accumulator = await BlockchainMock.getLatestAccumulator(
dummyAddress
)
expect(accumulator.toString()).toBe('dummyAccumulator')
})
it('Should buildUpdateAccumulatorTX', async () => {
const newAccumulator = new Accumulator('newAccumulator')
// set current accumulator
api.query.portablegabi.accumulatorList.mockResolvedValue(
(stringToHex('currAccumulator') as unknown) as Promise<Codec>
)
const currAccumulator = await BlockchainMock.getLatestAccumulator(
dummyAddress
)
expect(currAccumulator.toString()).toBe('currAccumulator')
// update to new accumulator
const tx = BlockchainMock.buildUpdateAccumulatorTX(newAccumulator)
await expect(
BlockchainMock.signAndSend(tx, 's' as any)
).resolves.toBeUndefined()
const latestAccumulator = await BlockchainMock.getLatestAccumulator(
dummyAddress
)
expect(latestAccumulator.toString()).toBe(newAccumulator.toString())
})
it('Should getAccumulatorArray', async () => {
api.query.portablegabi.accumulatorList.mockResolvedValue(
(stringToHex('currAccumulator') as unknown) as Promise<Codec>
)
const currAccumulators = await BlockchainMock.getAccumulatorArray(
dummyAddress,
1,
4
)
expect(Array.isArray(currAccumulators)).toBeTruthy()
expect(currAccumulators.length).toEqual(4)
})
})
describe('Negative tests', () => {
it('Should throw error for swapped indices when requesting accumulator array', async () => {
api.query.portablegabi.accumulatorList.mockResolvedValue(
(stringToHex('currAccumulator') as unknown) as Promise<Codec>
)
expect(
BlockchainMock.getAccumulatorArray(dummyAddress, 4, 1)
).rejects.toThrowError()
})
it('Should throw error when accumulator array index out is of range', async () => {
api.query.portablegabi.accumulatorList.mockResolvedValue(
(stringToHex('currAccumulator') as unknown) as Promise<Codec>
)
expect(
BlockchainMock.getAccumulatorArray(dummyAddress, 999999999)
).rejects.toThrowError()
})
it('Should throw for empty accumulatorList (maxIndex === -1)', async () => {
const accCount = 0
const requestedIndex = 1
api.query.portablegabi.accumulatorCount.mockResolvedValueOnce(
Promise.resolve(accCount)
)
api.query.portablegabi.accumulatorList.mockReturnValue(
(emptyValue as unknown) as Promise<Codec>
)
await expect(
BlockchainMock.getAccumulator(dummyAddress, requestedIndex)
).rejects.toThrowError(BlockchainError.maxIndexZero(dummyAddress))
})
it('Should throw when requesting accumulator index > maxIndex', async () => {
const accCount = 1
const requestedIndex = 2
api.query.portablegabi.accumulatorCount.mockResolvedValueOnce(
Promise.resolve(accCount)
)
api.query.portablegabi.accumulatorList.mockReturnValue(
(emptyValue as unknown) as Promise<Codec>
)
await expect(
BlockchainMock.getAccumulator(dummyAddress, requestedIndex)
).rejects.toThrowError(
BlockchainError.indexOutOfRange(
dummyAddress,
requestedIndex,
accCount - 1
)
)
})
it('Should throw when requesting accumulator for index < 0', async () => {
const accCount = 1
const requestedIndex = -1
api.query.portablegabi.accumulatorCount.mockResolvedValueOnce(
Promise.resolve(accCount)
)
api.query.portablegabi.accumulatorList.mockReturnValue(
(emptyValue as unknown) as Promise<Codec>
)
await expect(
BlockchainMock.getAccumulator(dummyAddress, requestedIndex)
).rejects.toThrowError(
BlockchainError.indexOutOfRange(
dummyAddress,
requestedIndex,
accCount - 1
)
)
})
it('Should throw when requesting latest accumulator for empty list', async () => {
const accCount = 0
api.query.portablegabi.accumulatorCount.mockResolvedValueOnce(
Promise.resolve(accCount)
)
api.query.portablegabi.accumulatorList.mockReturnValue(
(emptyValue as unknown) as Promise<Codec>
)
await expect(
BlockchainMock.getLatestAccumulator(dummyAddress)
).rejects.toThrowError(BlockchainError.maxIndexZero(dummyAddress))
})
})
})