Additional Option tests (#4378)

This commit is contained in:
Jaco
2021-12-22 11:35:45 +02:00
committed by GitHub
parent adbc1799a2
commit 1af5fb35bc
2 changed files with 23 additions and 2 deletions
+9 -1
View File
@@ -2,7 +2,7 @@
// SPDX-License-Identifier: Apache-2.0
import { TypeRegistry } from '@polkadot/types';
import { Bytes, Option, Text, U32 } from '@polkadot/types-codec';
import { bool, Bytes, Option, Text, U32 } from '@polkadot/types-codec';
const registry = new TypeRegistry();
@@ -29,6 +29,14 @@ describe('Option', (): void => {
expect(new Option(registry, Text, '0x').isNone).toBe(true);
});
it('correctly handles booleans', (): void => {
expect(new Option(registry, bool).isNone).toBe(true);
expect(new Option(registry, bool, true).isSome).toBe(true);
expect(new Option(registry, bool, true).unwrap().isTrue).toBe(true);
expect(new Option(registry, bool, false).isSome).toBe(true);
expect(new Option(registry, bool, false).unwrap().isTrue).toBe(false);
});
it('converts an option to an option', (): void => {
expect(
new Option(registry, Text, new Option(registry, Text, 'hello')).toString()
+14 -1
View File
@@ -6,7 +6,7 @@
import type { CodecTo } from '@polkadot/types-codec/types';
import { TypeRegistry } from '@polkadot/types';
import { Enum, Struct, Text, U32, Vec } from '@polkadot/types-codec';
import { Bool, Enum, Option, Struct, Text, U32, Vec } from '@polkadot/types-codec';
import { TEST_A } from './Struct.data';
@@ -64,6 +64,19 @@ describe('Struct', (): void => {
expect(new Clazz(registry, undefined).toHuman()).toEqual(expected);
});
it('decodes with Optionals', (): void => {
const Clazz = Struct.with({
a: 'Option<Bool>',
b: 'Option<Bool>'
});
const c = new Clazz(registry, { a: false }) as unknown as { a: Option<Bool>, b: Option<Bool> };
expect(c.a.isSome).toEqual(true);
expect(c.a.unwrap().isTrue).toEqual(false);
expect(c.b.isSome).toEqual(false);
});
it('decodes reusing instantiated inputs', (): void => {
const foo = new Text(registry, 'bar');