diff --git a/packages/types/src/codec/Struct.spec.ts b/packages/types/src/codec/Struct.spec.ts index e395475659..2e9b6647b7 100644 --- a/packages/types/src/codec/Struct.spec.ts +++ b/packages/types/src/codec/Struct.spec.ts @@ -8,40 +8,43 @@ import Text from '../Text'; import U32 from '../U32'; import Vector from './Vector'; -const testDecode = (type: string, input: any) => - it(`can decode from ${type}`, () => { - const s = new Struct({ - foo: Text, - bar: U32 - }, input); - expect([...s.keys()]).toEqual(['foo', 'bar']); - expect( - [...s.values()].map((v) => - v.toString() - ) - ).toEqual(['bazzing', '69']); - }); - -const testEncode = (to: CodecTo, expected: any) => - it(`can encode ${to}`, () => { - const s = new Struct({ - foo: Text, - bar: U32 - }, { foo: 'bazzing', bar: 69 }); - expect(s[to]()).toEqual(expected); - }); - describe('Struct', () => { + describe('decoding', () => { + const testDecode = (type: string, input: any) => + it(`can decode from ${type}`, () => { + const s = new Struct({ + foo: Text, + bar: U32 + }, input); + expect([...s.keys()]).toEqual(['foo', 'bar']); + expect( + [...s.values()].map((v) => + v.toString() + ) + ).toEqual(['bazzing', '69']); + }); - testDecode('array', ['bazzing', 69]); - testDecode('hex', '0x1c62617a7a696e6745000000'); - testDecode('object', { foo: 'bazzing', bar: 69 }); - testDecode('Uint8Array', Uint8Array.from([28, 98, 97, 122, 122, 105, 110, 103, 69, 0, 0, 0])); + testDecode('array', ['bazzing', 69]); + testDecode('hex', '0x1c62617a7a696e6745000000'); + testDecode('object', { foo: 'bazzing', bar: 69 }); + testDecode('Uint8Array', Uint8Array.from([28, 98, 97, 122, 122, 105, 110, 103, 69, 0, 0, 0])); + }); - testEncode('toHex', '0x1c62617a7a696e6745000000'); - testEncode('toJSON', { foo: 'bazzing', bar: 69 }); - testEncode('toU8a', Uint8Array.from([28, 98, 97, 122, 122, 105, 110, 103, 69, 0, 0, 0])); - testEncode('toString', '{"foo":"bazzing","bar":69}'); + describe('encoding', () => { + const testEncode = (to: CodecTo, expected: any) => + it(`can encode ${to}`, () => { + const s = new Struct({ + foo: Text, + bar: U32 + }, { foo: 'bazzing', bar: 69 }); + expect(s[to]()).toEqual(expected); + }); + + testEncode('toHex', '0x1c62617a7a696e6745000000'); + testEncode('toJSON', { foo: 'bazzing', bar: 69 }); + testEncode('toU8a', Uint8Array.from([28, 98, 97, 122, 122, 105, 110, 103, 69, 0, 0, 0])); + testEncode('toString', '{"foo":"bazzing","bar":69}'); + }); it('decodes null', () => { expect( @@ -85,6 +88,18 @@ describe('Struct', () => { ).toEqual('{"txt":"foo","u32":1193046}'); }); + it('exposes the properties on the object', () => { + const struct = new ( + Struct.with({ + txt: Text, + u32: U32 + }) + )({ txt: 'foo', u32: 0x123456 }); + + expect((struct as any).txt.toString()).toEqual('foo'); + expect((struct as any).u32.toNumber()).toEqual(0x123456); + }); + it('correctly encodes length', () => { expect( new ( diff --git a/packages/types/src/codec/Struct.ts b/packages/types/src/codec/Struct.ts index 61446fd740..8886b0c3cd 100644 --- a/packages/types/src/codec/Struct.ts +++ b/packages/types/src/codec/Struct.ts @@ -111,6 +111,13 @@ export default class Struct< return class extends Struct { constructor (value?: any, jsonMap?: Map) { super(Types, value, jsonMap); + + (Object.keys(Types) as Array).forEach((key) => { + Object.defineProperty(this, key, { + enumerable: true, + get: () => this.get(key) + }); + }); } }; }