generated from PolymeshAssociation/typescript-boilerplate
-
Notifications
You must be signed in to change notification settings - Fork 11
/
index.ts
236 lines (191 loc) · 6.63 KB
/
index.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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
import { u64 } from '@polkadot/types';
import BigNumber from 'bignumber.js';
import sinon from 'sinon';
import { addInstruction, Context, Entity, Instruction, TransactionQueue, Venue } from '~/internal';
import { dsMockUtils, entityMockUtils } from '~/testUtils/mocks';
import { Mocked } from '~/testUtils/types';
import { VenueType } from '~/types';
import * as utilsConversionModule from '~/utils/conversion';
jest.mock(
'~/api/entities/Identity',
require('~/testUtils/mocks/entities').mockIdentityModule('~/api/entities/Identity')
);
jest.mock(
'~/api/entities/Instruction',
require('~/testUtils/mocks/entities').mockInstructionModule('~/api/entities/Instruction')
);
describe('Venue class', () => {
let context: Mocked<Context>;
let venue: Venue;
let id: BigNumber;
let rawId: u64;
beforeAll(() => {
dsMockUtils.initMocks();
entityMockUtils.initMocks();
id = new BigNumber(1);
rawId = dsMockUtils.createMockU64(id.toNumber());
});
beforeEach(() => {
context = dsMockUtils.getContextInstance();
venue = new Venue({ id }, context);
});
afterEach(() => {
dsMockUtils.reset();
entityMockUtils.reset();
});
afterAll(() => {
dsMockUtils.cleanup();
entityMockUtils.cleanup();
});
test('should extend Entity', () => {
expect(Venue.prototype instanceof Entity).toBe(true);
});
describe('method: isUniqueIdentifiers', () => {
test('should return true if the object conforms to the interface', () => {
expect(Venue.isUniqueIdentifiers({ id: new BigNumber(1) })).toBe(true);
expect(Venue.isUniqueIdentifiers({})).toBe(false);
expect(Venue.isUniqueIdentifiers({ id: 3 })).toBe(false);
});
});
describe('method: exists', () => {
afterAll(() => {
sinon.restore();
});
test('should return whether if the venue exists or not', async () => {
const owner = 'someDid';
entityMockUtils.configureMocks({ identityOptions: { did: owner } });
sinon
.stub(utilsConversionModule, 'numberToU64')
.withArgs(id, context)
.returns(rawId);
dsMockUtils
.createQueryStub('settlement', 'venueInfo')
.withArgs(rawId)
.resolves(dsMockUtils.createMockOption());
const result = await venue.exists();
expect(result).toEqual(false);
});
});
describe('method: details', () => {
afterAll(() => {
sinon.restore();
});
test("should throw an error if the venue doesn't exist", async () => {
dsMockUtils
.createQueryStub('settlement', 'venueInfo')
.resolves(dsMockUtils.createMockOption());
entityMockUtils.configureMocks({
numberedPortfolioOptions: {
exists: false,
},
});
return expect(venue.details()).rejects.toThrow("The Venue doesn't exist");
});
test('should return the Venue details', async () => {
const description = 'someDescription';
const type = VenueType.Other;
const owner = 'someDid';
entityMockUtils.configureMocks({ identityOptions: { did: owner } });
sinon
.stub(utilsConversionModule, 'numberToU64')
.withArgs(id, context)
.returns(rawId);
dsMockUtils
.createQueryStub('settlement', 'venueInfo')
.withArgs(rawId)
.resolves(
dsMockUtils.createMockOption(
dsMockUtils.createMockVenue({
creator: dsMockUtils.createMockIdentityId(owner),
instructions: [],
details: dsMockUtils.createMockVenueDetails(description),
// eslint-disable-next-line @typescript-eslint/camelcase
venue_type: dsMockUtils.createMockVenueType(type),
})
)
);
const result = await venue.details();
expect(result).toEqual({
owner: entityMockUtils.getIdentityInstance(),
description,
type,
});
});
});
describe('method: getPendingInstructions', () => {
afterAll(() => {
sinon.restore();
});
test("should throw an error if the venue doesn't exist", async () => {
dsMockUtils
.createQueryStub('settlement', 'venueInfo')
.resolves(dsMockUtils.createMockOption());
entityMockUtils.configureMocks({
numberedPortfolioOptions: {
exists: false,
},
});
return expect(venue.getPendingInstructions()).rejects.toThrow("The Venue doesn't exist");
});
test("should return the Venue's pending instructions", async () => {
const description = 'someDescription';
const type = VenueType.Other;
const owner = 'someDid';
const instructionId = new BigNumber(1);
entityMockUtils.configureMocks({ instructionOptions: { id: instructionId } });
sinon
.stub(utilsConversionModule, 'numberToU64')
.withArgs(id, context)
.returns(rawId);
dsMockUtils
.createQueryStub('settlement', 'venueInfo')
.withArgs(rawId)
.resolves(
dsMockUtils.createMockOption(
dsMockUtils.createMockVenue({
creator: dsMockUtils.createMockIdentityId(owner),
instructions: [dsMockUtils.createMockU64(instructionId.toNumber())],
details: dsMockUtils.createMockVenueDetails(description),
// eslint-disable-next-line @typescript-eslint/camelcase
venue_type: dsMockUtils.createMockVenueType(type),
})
)
);
const result = await venue.getPendingInstructions();
expect(result[0].id).toEqual(instructionId);
});
});
describe('method: addInstruction', () => {
let prepareAddInstructionStub: sinon.SinonStub;
beforeAll(() => {
prepareAddInstructionStub = sinon.stub(addInstruction, 'prepare');
});
afterAll(() => {
sinon.restore();
});
test('should prepare the procedure and return the resulting transaction queue', async () => {
const legs = [
{
from: 'someDid',
to: 'anotherDid',
amount: new BigNumber(1000),
token: 'SOME_TOKEN',
},
{
from: 'anotherDid',
to: 'aThirdDid',
amount: new BigNumber(100),
token: 'ANOTHER_TOKEN',
},
];
const validFrom = new Date(new Date().getTime() + 24 * 60 * 60 * 1000);
const endBlock = new BigNumber(10000);
const expectedQueue = ('someQueue' as unknown) as TransactionQueue<Instruction>;
prepareAddInstructionStub
.withArgs({ venueId: id, legs, validFrom, endBlock }, context)
.resolves(expectedQueue);
const queue = await venue.addInstruction({ legs, validFrom, endBlock });
expect(queue).toBe(expectedQueue);
});
});
});