generated from PolymeshAssociation/typescript-boilerplate
-
Notifications
You must be signed in to change notification settings - Fork 11
/
CurrentIdentity.ts
141 lines (105 loc) · 4.1 KB
/
CurrentIdentity.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
import sinon from 'sinon';
import { CurrentIdentity, Identity, Venue } from '~/api/entities';
import { createVenue, inviteAccount, removeSecondaryKeys } from '~/api/procedures';
import { Context, TransactionQueue } from '~/base';
import { dsMockUtils, entityMockUtils } from '~/testUtils/mocks';
import { SecondaryKey, SubCallback, VenueType } from '~/types';
jest.mock(
'~/api/entities/Venue',
require('~/testUtils/mocks/entities').mockVenueModule('~/api/entities/Venue')
);
describe('CurrentIdentity class', () => {
let context: Context;
beforeAll(() => {
entityMockUtils.initMocks();
dsMockUtils.initMocks();
});
beforeEach(() => {
context = dsMockUtils.getContextInstance();
});
afterEach(() => {
entityMockUtils.reset();
dsMockUtils.reset();
});
afterAll(() => {
entityMockUtils.cleanup();
dsMockUtils.cleanup();
});
test('should extend Identity', () => {
expect(CurrentIdentity.prototype instanceof Identity).toBe(true);
});
describe('method: getSecondaryKeys', () => {
test('should return a list of Secondaries', async () => {
const fakeResult = [
{
signer: entityMockUtils.getAccountInstance({ address: 'someAddress' }),
permissions: [],
},
];
dsMockUtils.configureMocks({ contextOptions: { secondaryKeys: fakeResult } });
const did = 'someDid';
const identity = new CurrentIdentity({ did }, context);
const result = await identity.getSecondaryKeys();
expect(result).toEqual(fakeResult);
});
test('should allow subscription', async () => {
const unsubCallback = 'unsubCallBack';
const getSecondaryKeysStub = dsMockUtils
.getContextInstance()
.getSecondaryKeys.resolves(unsubCallback);
const did = 'someDid';
const identity = new CurrentIdentity({ did }, context);
const callback = (() => [] as unknown) as SubCallback<SecondaryKey[]>;
const result = await identity.getSecondaryKeys(callback);
expect(result).toEqual(unsubCallback);
sinon.assert.calledWithExactly(getSecondaryKeysStub, callback);
});
});
describe('method: removeSecondaryKeys', () => {
test('should prepare the procedure with the correct arguments and context, and return the resulting transaction queue', async () => {
const did = 'someDid';
const identity = new CurrentIdentity({ did }, context);
const signers = [entityMockUtils.getAccountInstance({ address: 'someAccount' })];
const expectedQueue = ('someQueue' as unknown) as TransactionQueue<void>;
sinon
.stub(removeSecondaryKeys, 'prepare')
.withArgs({ signers }, context)
.resolves(expectedQueue);
const queue = await identity.removeSecondaryKeys({ signers });
expect(queue).toBe(expectedQueue);
});
});
describe('method: inviteAccount', () => {
test('should prepare the procedure with the correct arguments and context, and return the resulting transaction queue', async () => {
const did = 'someDid';
const identity = new CurrentIdentity({ did }, context);
const args = {
targetAccount: 'someAccount',
};
const expectedQueue = ('someQueue' as unknown) as TransactionQueue<void>;
sinon
.stub(inviteAccount, 'prepare')
.withArgs(args, context)
.resolves(expectedQueue);
const queue = await identity.inviteAccount(args);
expect(queue).toBe(expectedQueue);
});
});
describe('method: createVenue', () => {
test('should prepare the procedure with the correct arguments and context, and return the resulting transaction queue', async () => {
const did = 'someDid';
const identity = new CurrentIdentity({ did }, context);
const args = {
details: 'details',
type: VenueType.Distribution,
};
const expectedQueue = ('someQueue' as unknown) as TransactionQueue<Venue>;
sinon
.stub(createVenue, 'prepare')
.withArgs(args, context)
.resolves(expectedQueue);
const queue = await identity.createVenue(args);
expect(queue).toBe(expectedQueue);
});
});
});