generated from PolymeshAssociation/typescript-boilerplate
/
Context.ts
148 lines (127 loc) · 4.88 KB
/
Context.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
import sinon from 'sinon';
import { Context } from '~/Context';
import { ImportMock } from 'ts-mock-imports';
import * as polkadotModule from '@polymathnetwork/polkadot/api';
import * as identityModule from '~/api/entities/Identity';
import { QueryableStorage } from '@polymathnetwork/polkadot/api/types';
describe('Context class', () => {
const mockKeyring = ImportMock.mockClass(polkadotModule, 'Keyring');
const mockApiPromise = ImportMock.mockClass<polkadotModule.ApiPromise>(
polkadotModule,
'ApiPromise'
);
afterAll(() => {
mockKeyring.restore();
mockApiPromise.restore();
});
describe('method: create', () => {
test('should throw if accountSeed parameter is not a 32 length string', async () => {
const context = Context.create({
polymeshApi: mockApiPromise.getMockInstance(),
accountSeed: 'abc',
});
await expect(context).rejects.toThrow(new Error('Seed must be 32 characters in length'));
});
test('should create a Context class with Pair and Identity attached', async () => {
const keyToIdentityIdsStub = sinon.stub().returns({
unwrap: () => {
return { asUnique: '012abc' };
},
});
const keyringAddFromSeedStub = mockKeyring.mock('addFromSeed', 'currentPair');
mockApiPromise.set('query', ({
identity: {
keyToIdentityIds: keyToIdentityIdsStub,
},
} as unknown) as QueryableStorage<'promise'>);
const context = await Context.create({
polymeshApi: mockApiPromise.getMockInstance(),
accountSeed: 'Alice'.padEnd(32, ' '),
});
sinon.assert.calledOnce(keyringAddFromSeedStub);
sinon.assert.calledOnce(keyToIdentityIdsStub);
expect(context.currentPair).toEqual('currentPair');
sinon.assert.match(context.currentIdentity instanceof identityModule.Identity, true);
});
test('should create a Context class without Pair and Identity attached', async () => {
const keyToIdentityIdsStub = sinon.stub().returns('identityId');
const keyringAddFromSeedMock = mockKeyring.mock('addFromSeed', true);
mockApiPromise.set('query', ({
identity: {
keyToIdentityIds: keyToIdentityIdsStub,
},
} as unknown) as QueryableStorage<'promise'>);
const context = await Context.create({
polymeshApi: mockApiPromise.getMockInstance(),
});
sinon.assert.notCalled(keyringAddFromSeedMock);
sinon.assert.notCalled(keyToIdentityIdsStub);
expect(context.currentPair).toBe(undefined);
expect(context.currentIdentity).toBe(undefined);
});
test('should throw if the account seed is not assotiated with an IdentityId ', async () => {
const keyToIdentityIdsStub = sinon.stub().returns({
unwrap: sinon.stub().throws(),
});
mockKeyring.mock('addFromSeed', 'currentPair');
mockApiPromise.set('query', ({
identity: {
keyToIdentityIds: keyToIdentityIdsStub,
},
} as unknown) as QueryableStorage<'promise'>);
const context = Context.create({
polymeshApi: mockApiPromise.getMockInstance(),
accountSeed: 'Alice'.padEnd(32, ' '),
});
await expect(context).rejects.toThrow(new Error('Identity ID does not exist'));
});
});
describe('method: getAccounts', () => {
test('should getAccounts method retrieve an array of address and meta data', async () => {
const addresses = [
{
address: '01',
meta: {
name: 'name 01',
},
somethingelse: false,
},
{
address: '02',
meta: {},
somethingelse: false,
},
];
const keyringGetAccountsStub = mockKeyring.mock('getPairs', addresses);
const context = await Context.create({
polymeshApi: mockApiPromise.getMockInstance(),
});
const result = context.getAccounts();
sinon.assert.calledOnce(keyringGetAccountsStub);
expect(result[0].address).toBe('01');
expect(result[1].address).toBe('02');
expect(result[0].name).toBe('name 01');
expect(result[1].name).toBe(undefined);
});
});
describe('method: getPair', () => {
test('should throw error if the pair does not exist in the keyring set', async () => {
mockKeyring
.mock('getPair')
.withArgs('012')
.throws();
const context = await Context.create({
polymeshApi: mockApiPromise.getMockInstance(),
});
expect(() => context.setPair('012')).toThrow('The address is not present in the keyring set');
});
test('should expect currentPair to be set to the new value', async () => {
const keyringGetPairStub = mockKeyring.mock('getPair', true);
const context = await Context.create({
polymeshApi: mockApiPromise.getMockInstance(),
});
context.setPair('012');
sinon.assert.calledOnce(keyringGetPairStub);
});
});
});