From 1af5fb35bcf708507141f46d70969ba3e3360187 Mon Sep 17 00:00:00 2001 From: Jaco Date: Wed, 22 Dec 2021 10:35:45 +0100 Subject: [PATCH] Additional Option tests (#4378) --- packages/types-codec/src/base/Option.spec.ts | 10 +++++++++- packages/types-codec/src/native/Struct.spec.ts | 15 ++++++++++++++- 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/packages/types-codec/src/base/Option.spec.ts b/packages/types-codec/src/base/Option.spec.ts index a7ad5f243c..22920a0329 100644 --- a/packages/types-codec/src/base/Option.spec.ts +++ b/packages/types-codec/src/base/Option.spec.ts @@ -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() diff --git a/packages/types-codec/src/native/Struct.spec.ts b/packages/types-codec/src/native/Struct.spec.ts index 201eaebb47..a66a38928c 100644 --- a/packages/types-codec/src/native/Struct.spec.ts +++ b/packages/types-codec/src/native/Struct.spec.ts @@ -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', + b: 'Option' + }); + + const c = new Clazz(registry, { a: false }) as unknown as { a: Option, b: Option }; + + 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');