Additional tests (w/ lazy) (#1174)

This commit is contained in:
Jaco
2021-11-12 08:44:00 +02:00
committed by GitHub
parent 9b30b4b09e
commit 31d55b218a
8 changed files with 172 additions and 3 deletions
+17 -1
View File
@@ -1,7 +1,7 @@
// Copyright 2017-2021 @polkadot/util authors & contributors
// SPDX-License-Identifier: Apache-2.0
import { assert } from '.';
import { assert, assertReturn } from '.';
describe('assert', (): void => {
it('should not throw an error when test is true', (): void => {
@@ -20,3 +20,19 @@ describe('assert', (): void => {
).toThrow(/message from function/);
});
});
describe('assertReturn', (): void => {
it('should not throw an error when result is true', (): void => {
expect(assertReturn(true, 'nothing should be thrown')).toEqual(true);
});
it('should not throw an error when result is false', (): void => {
expect(assertReturn(false, 'nothing should be thrown')).toEqual(false);
});
it('should throw an error when result is undefined', (): void => {
expect(
() => assertReturn(undefined, 'something thrown')
).toThrow(/something thrown/);
});
});
+18
View File
@@ -0,0 +1,18 @@
// Copyright 2017-2021 @polkadot/util authors & contributors
// SPDX-License-Identifier: Apache-2.0
import { isArray } from '.';
describe('isArray', (): void => {
it('is false on no value', (): void => {
expect(isArray()).toEqual(false);
});
it('is false on non-array', (): void => {
expect(isArray(123)).toEqual(false);
});
it('is true when Array is found', (): void => {
expect(isArray([1, 2, 3])).toEqual(true);
});
});
+1 -1
View File
@@ -5,6 +5,6 @@
* @name isArray
* @summary Tests for a Array instance.
*/
export function isArray <T> (value: unknown): value is Array<T> {
export function isArray <T> (value?: unknown): value is Array<T> {
return Array.isArray(value);
}
+25
View File
@@ -0,0 +1,25 @@
// Copyright 2017-2021 @polkadot/util authors & contributors
// SPDX-License-Identifier: Apache-2.0
import BN from 'bn.js';
import { isCompact } from '.';
describe('isCompact', (): void => {
it('is false on no value', (): void => {
expect(isCompact()).toEqual(false);
});
it('is false on non-compact value', (): void => {
expect(isCompact(123)).toEqual(false);
});
it('is true when compact-like signature is found', (): void => {
expect(isCompact({
toBigInt: () => BigInt(1),
toBn: () => new BN(1),
toNumber: () => 1,
unwrap: () => new BN(1)
})).toEqual(true);
});
});
+2 -1
View File
@@ -16,8 +16,9 @@ interface Compact<T> {
* @name isCompact
* @summary Tests for SCALE-Compact-like object instance.
*/
export function isCompact <T> (value: unknown): value is Compact<T> {
export function isCompact <T> (value?: unknown): value is Compact<T> {
return (
!!value &&
isFunction((value as Compact<T>).toBigInt) &&
isFunction((value as Compact<T>).toBn) &&
isFunction((value as Compact<T>).toNumber) &&
+23
View File
@@ -0,0 +1,23 @@
// Copyright 2017-2021 @polkadot/util authors & contributors
// SPDX-License-Identifier: Apache-2.0
import BN from 'bn.js';
import { isToBn } from '.';
describe('isToBn', (): void => {
it('is false on no value', (): void => {
expect(isToBn()).toEqual(false);
});
it('is false on non-compact value', (): void => {
expect(isToBn(123)).toEqual(false);
});
it('is true when compact-like signature is found', (): void => {
expect(isToBn({
toBigInt: () => BigInt(1),
toBn: () => new BN(1)
})).toEqual(true);
});
});
+18
View File
@@ -0,0 +1,18 @@
// Copyright 2017-2021 @polkadot/util authors & contributors
// SPDX-License-Identifier: Apache-2.0
import { isWasm } from '.';
describe('isWasm', (): void => {
it('is false on no value', (): void => {
expect(isWasm()).toEqual(false);
});
it('is false on non-wasm header', (): void => {
expect(isWasm(new Uint8Array([1, 2, 3]))).toEqual(false);
});
it('is true when a wasm header is found', (): void => {
expect(isWasm(new Uint8Array([0, 97, 115, 109, 1, 2, 3]))).toEqual(true);
});
});
+68
View File
@@ -0,0 +1,68 @@
// Copyright 2017-2021 @polkadot/util authors & contributors
// SPDX-License-Identifier: Apache-2.0
import { lazyMethod, lazyMethods } from '.';
describe('lazyMethod', (): void => {
it('adds a key on the object', (): void => {
const test: Record<string, unknown> = {};
expect(test.a).not.toBeDefined();
lazyMethod(test, 'a', () => 123);
expect(test.a).toEqual(123);
});
it('allows the name to be retrieved', (): void => {
const test: Record<string, unknown> = {};
expect(test.a).not.toBeDefined();
lazyMethod(test, 456, () => 123, () => 'a');
expect(test.a).toEqual(123);
});
it('calls the getter lazily', (): void => {
const test: Record<string, unknown> = {};
const getter = jest.fn(() => 123);
expect(test.a).not.toBeDefined();
lazyMethod(test, 'a', getter);
expect(Object.prototype.hasOwnProperty.call(test, 'a')).toEqual(true);
expect(getter).not.toHaveBeenCalled();
expect(test.a).toEqual(123);
expect(getter).toHaveBeenCalledWith('a');
});
it('calls the getter a single time', (): void => {
const test: Record<string, unknown> = {};
const getter = jest.fn(() => 123);
lazyMethod(test, 'a', getter);
expect(getter).not.toHaveBeenCalled();
expect(test.a).toEqual(123);
expect(getter).toHaveBeenCalledWith('a');
expect(test.a).toEqual(123);
expect(getter).toHaveBeenCalledTimes(1);
});
});
describe('lazyMethods', (): void => {
it('sets multiple properties', (): void => {
const test: Record<string, unknown> = {};
const getter = jest.fn((name: string) => name);
const set = lazyMethods(test, ['a', 'b', 'c'], getter);
expect(set.a).toEqual('a');
expect(set.b).toEqual('b');
expect(getter).toHaveBeenCalledTimes(2);
expect(set.a).toEqual('a');
expect(set.b).toEqual('b');
expect(getter).toHaveBeenCalledTimes(2);
});
});