Skip to content

Commit

Permalink
fix: improve methods tests
Browse files Browse the repository at this point in the history
  • Loading branch information
shuffledex committed Feb 13, 2020
1 parent 272a54b commit d8fa9bf
Show file tree
Hide file tree
Showing 3 changed files with 110 additions and 104 deletions.
1 change: 0 additions & 1 deletion package.json
Expand Up @@ -61,7 +61,6 @@
"@polkadot/ui-settings": "^0.49.1",
"@polymathnetwork/polkadot": "0.101.0-beta.9",
"@types/sinon": "^7.5.1",
"bn.js": "^5.1.1",
"json-stable-stringify": "^1.0.1"
}
}
44 changes: 27 additions & 17 deletions src/Context.ts
Expand Up @@ -10,16 +10,20 @@ interface BuildParams {
accountSeed?: string | undefined;
}

interface Pair {
current: KeyringPair;
did: Option<LinkedKeyInfo>;
}

interface ConstructorParams {
polymeshApi: ApiPromise;
keyring: Keyring;
currentPair?: KeyringPair;
did?: Option<LinkedKeyInfo>;
pair?: Pair;
}

interface AddressPair {
interface AccountData {
address: string;
meta: KeyringPair$Meta;
name?: string;
}

/**
Expand All @@ -34,20 +38,20 @@ export class Context {

public polymeshApi: ApiPromise;

public currentPair: KeyringPair | undefined;
public currentPair?: KeyringPair;

public currentIdentity: Identity | undefined;
public currentIdentity?: Identity;

// eslint-disable-next-line require-jsdoc
private constructor(params: ConstructorParams) {
const { polymeshApi, keyring, currentPair, did } = params;
const { polymeshApi, keyring, pair } = params;

this.polymeshApi = polymeshApi;
this.keyring = keyring;

if (currentPair && did) {
this.currentPair = currentPair;
this.currentIdentity = new Identity({ did: did.toString() }, this);
if (pair) {
this.currentPair = pair.current;
this.currentIdentity = new Identity({ did: pair.did.toString() }, this);
}
}

Expand All @@ -61,30 +65,36 @@ export class Context {

if (accountSeed) {
if (accountSeed.length !== 32) {
// TODO it should uses polymath error class
// TODO - MSDK-49 Create Polymesh Error class
throw new Error('Seed must be 32 length size');
}

const currentPair = keyring.addFromSeed(stringToU8a(accountSeed));
const did = await polymeshApi.query.identity.keyToIdentityIds(currentPair.publicKey);
return new Context({ polymeshApi, keyring, currentPair, did });
const current = keyring.addFromSeed(stringToU8a(accountSeed));
const did = await polymeshApi.query.identity.keyToIdentityIds(current.publicKey);
return new Context({ polymeshApi, keyring, pair: { current, did } });
}
return new Context({ polymeshApi, keyring });
}

public getAddresses = (): Array<AddressPair> => {
/**
* Retrieve a list of addresses associated with the account
*/
public getAccounts = (): Array<AccountData> => {
const { keyring } = this;
return keyring.getPairs().map(({ address, meta }) => {
return { address, meta };
return { address, name: meta.name };
});
};

/**
* Set a pair as a current account keyring pair
*/
public setPair = (address: string): void => {
const { keyring } = this;
try {
this.currentPair = keyring.getPair(address);
} catch (e) {
// TODO it should uses polymath error class
// TODO - MSDK-49 Create Polymesh Error class
throw new Error('The address is not present in the keyring set');
}
};
Expand Down
169 changes: 83 additions & 86 deletions src/__tests__/Context.ts
Expand Up @@ -18,112 +18,109 @@ describe('Context Class', () => {
mockIdentity.restore();
});

test('should throw if accountSeed parameter is not a 32 lenght string', async () => {
const context = Context.create({
polymeshApi: apiPromise.getMockInstance(),
accountSeed: 'abc',
describe('method: create', () => {
test('should throw if accountSeed parameter is not a 32 lenght string', async () => {
const context = Context.create({
polymeshApi: apiPromise.getMockInstance(),
accountSeed: 'abc',
});

await expect(context).rejects.toThrow(new Error('Seed must be 32 length size'));
});

await expect(context).rejects.toThrow(new Error('Seed must be 32 length size'));
});
test('should create a Context class with Pair and Identity attached', async () => {
const keyToIdentityIdsStub = sinon.stub().returns('identityId');
const keyringAddFromSeedStub = mockKeyring.mock('addFromSeed', 'currentPair');
apiPromise.set('query', {
identity: {
keyToIdentityIds: keyToIdentityIdsStub,
},
});

test('should create a Context class with Pair and Identity attached', async () => {
const keyToIdentityIds = jest.fn(() => true);
const keyringAddFromSeedMock = mockKeyring.mock('addFromSeed', true);
apiPromise.set('query', {
identity: {
keyToIdentityIds: keyToIdentityIds,
},
});
const context = await Context.create({
polymeshApi: apiPromise.getMockInstance(),
accountSeed: 'Alice'.padEnd(32, ' '),
});

const context = await Context.create({
polymeshApi: apiPromise.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);
});

sinon.assert.calledOnce(keyringAddFromSeedMock);
expect(keyToIdentityIds).toBeCalled();
expect(context.currentPair).toBe(true);
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);
apiPromise.set('query', {
identity: {
keyToIdentityIds: keyToIdentityIdsStub,
},
});

test('should create a Context class without Pair and Identity attached', async () => {
const keyToIdentityIds = jest.fn(() => true);
const keyringAddFromSeedMock = mockKeyring.mock('addFromSeed', true);
apiPromise.set('query', {
identity: {
keyToIdentityIds: keyToIdentityIds,
},
});
const context = await Context.create({
polymeshApi: apiPromise.getMockInstance(),
});

const context = await Context.create({
polymeshApi: apiPromise.getMockInstance(),
sinon.assert.notCalled(keyringAddFromSeedMock);
sinon.assert.notCalled(keyToIdentityIdsStub);
expect(context.currentPair).toBe(undefined);
expect(context.currentIdentity).toBe(undefined);
});

sinon.assert.notCalled(keyringAddFromSeedMock);
expect(keyToIdentityIds).not.toBeCalled();
expect(context.currentPair).toBe(undefined);
expect(context.currentIdentity).toBe(undefined);
});

test('should getAddresses method retrieve an array of address and meta data', async () => {
const addresses = [
{
address: '01',
meta: {
name: 'name 01',
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,
},
somethingelse: false,
},
{
address: '02',
meta: {
name: 'name 02',
{
address: '02',
meta: {},
somethingelse: false,
},
somethingelse: false,
},
];
const keyringGetAddressesMock = mockKeyring.mock('getPairs', addresses);

const context = await Context.create({
polymeshApi: apiPromise.getMockInstance(),
];
const keyringGetAccountsStub = mockKeyring.mock('getPairs', addresses);

const context = await Context.create({
polymeshApi: apiPromise.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);
});

const result = context.getAddresses();
sinon.assert.calledOnce(keyringGetAddressesMock);
expect(result[0].address).toBe('01');
expect(result[1].address).toBe('02');
expect(result[0].meta.name).toBe('name 01');
expect(result[1].meta.name).toBe('name 02');
});

test('throw error if the pair does not exist in the keyrign set', async () => {
mockKeyring
.mock('getPair')
.withArgs('012')
.throws();
const context = await Context.create({
polymeshApi: apiPromise.getMockInstance(),
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: apiPromise.getMockInstance(),
});

expect(() => context.setPair('012')).toThrow('The address is not present in the keyring set');
});

try {
context.setPair('012');
sinon.assert.fail();
} catch (e) {}
});
test('should expect currentPair to be set to the new value', async () => {
const keyringGetPairStub = mockKeyring.mock('getPair', true);

test('should set a new currentPair', async () => {
const keyringGetPairMock = mockKeyring.mock('getPair', true).withArgs('012');
const context = await Context.create({
polymeshApi: apiPromise.getMockInstance(),
});

const context = await Context.create({
polymeshApi: apiPromise.getMockInstance(),
});

try {
context.setPair('012');
sinon.assert.calledOnce(keyringGetPairMock);
} catch (e) {
sinon.assert.fail();
}
sinon.assert.calledOnce(keyringGetPairStub);
});
});
});

0 comments on commit d8fa9bf

Please sign in to comment.