Additional Option tests (#4378)
This commit is contained in:
@@ -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()
|
||||
|
||||
@@ -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');
|
||||
|
||||
|
||||
Reference in New Issue
Block a user