createType makes new instance, StorageData tests (#417)

* createType makes new instance, StorageData tests

* Unused import
This commit is contained in:
Jaco Greeff
2018-11-28 12:04:38 +01:00
committed by GitHub
parent 2f3af48875
commit 2aa78f2cad
5 changed files with 61 additions and 22 deletions
+1 -1
View File
@@ -208,7 +208,7 @@ export default class Rpc implements RpcInterface {
: (
item.value.isNone
? null
: createType(type, item.value.value)
: createType(type, item.value.unwrap())
)
);
+39 -14
View File
@@ -2,25 +2,50 @@
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
import { createType } from '@polkadot/types/codec';
import json from './json/StorageChangeSet.001.json';
import StorageChangeSet from './StorageChangeSet';
describe('StorageChangeSet', () => {
const set = new StorageChangeSet({
hash: '0x1234',
changes: [
{ key: '0xab', value: '0xcd' }
]
describe('construction', () => {
const set = new StorageChangeSet({
block: '0x1234',
changes: [
{ key: '0xab', value: '0xcd' }
]
});
it('wraps blockHash', () => {
expect(
set.block.toHex()
).toEqual('0x1234');
});
it('wraps key/value', () => {
expect(
set.changes[0].value.toString()
).toEqual('0xcd');
});
});
it('wraps hash', () => {
expect(
set.hash.toHex()
).toEqual('0x1234');
});
describe('json', () => {
const set = new StorageChangeSet(json.params.result);
it('wraps key/value', () => {
expect(
set.changes[0].value.toString()
).toEqual('0xcd');
it('has the correct hash', () => {
expect(
set.block.toHex()
).toEqual('0x2ad8077937e9a5ceb2e0d57c95b95a6a9edcedc4fb1f14e3bc13245e223a569d');
});
it('has the changes', () => {
expect(set.changes).toHaveLength(1);
expect(set.changes[0].key.toHex()).toEqual('0x54bdbdb5e438d574dd4da05ee6131cee');
});
it('converts result data to Bytes', () => {
const bytes = createType('Bytes', set.changes[0].value.unwrap());
expect(bytes).toHaveLength(4452);
});
});
});
+4 -4
View File
@@ -10,7 +10,7 @@ import Hash from './Hash';
import { KeyValueOption, KeyValueOptionValue } from './KeyValue';
type StorageChangeSetValue = {
hash?: AnyU8a,
block?: AnyU8a,
changes?: Array<KeyValueOptionValue>
};
@@ -19,7 +19,7 @@ type StorageChangeSetValue = {
export default class StorageChangeSet extends Struct {
constructor (value?: StorageChangeSetValue | Uint8Array) {
super({
hash: Hash,
block: Hash,
changes: Vector.with(KeyValueOption)
}, value);
}
@@ -28,7 +28,7 @@ export default class StorageChangeSet extends Struct {
return this.get('changes') as Vector<KeyValueOption>;
}
get hash (): Hash {
return this.get('hash') as Hash;
get block (): Hash {
return this.get('block') as Hash;
}
}
+1 -3
View File
@@ -161,7 +161,5 @@ export default function createType (type: Text | string, value?: any): Codec {
getTypeDef(type)
);
return value instanceof Type
? value
: new Type(value);
return new Type(value);
}
File diff suppressed because one or more lines are too long