Additional tests (need to expand more...) (#1172)
* Additional utility tests * Args on constructor
This commit is contained in:
@@ -21,4 +21,16 @@ describe('stringShorten', (): void => {
|
||||
stringShorten(String('0123456789'), 3)
|
||||
).toEqual('012…789');
|
||||
});
|
||||
|
||||
it('returns the value as-is when <= maxLength (defaults)', (): void => {
|
||||
expect(
|
||||
stringShorten('012345', 4)
|
||||
).toEqual('012345');
|
||||
});
|
||||
|
||||
it('returns the shortened value when > maxLength (defaults)', (): void => {
|
||||
expect(
|
||||
stringShorten('01234567890123456789')
|
||||
).toEqual('012345…456789');
|
||||
});
|
||||
});
|
||||
|
||||
@@ -0,0 +1,18 @@
|
||||
// Copyright 2017-2021 @polkadot/x-textencoder authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import { TextDecoder } from './fallback';
|
||||
|
||||
describe('TextDecoder (fallback)', (): void => {
|
||||
it('decodes correctly', (): void => {
|
||||
expect(
|
||||
new TextDecoder().decode(new Uint8Array([97, 98, 99]))
|
||||
).toEqual('abc');
|
||||
});
|
||||
|
||||
it('decodes correctly (with constructor param)', (): void => {
|
||||
expect(
|
||||
new TextDecoder('utf-8').decode(new Uint8Array([97, 98, 99, 98, 97]))
|
||||
).toEqual('abcba');
|
||||
});
|
||||
});
|
||||
@@ -4,15 +4,15 @@
|
||||
// This is very limited, only handling Ascii values
|
||||
export class TextDecoder {
|
||||
// eslint-disable-next-line @typescript-eslint/no-unused-vars,no-useless-constructor
|
||||
constructor (_: 'utf-8') {
|
||||
constructor (_?: 'utf-8' | 'utf8') {
|
||||
// nothing
|
||||
}
|
||||
|
||||
decode (value: Uint8Array): string {
|
||||
let result = '';
|
||||
|
||||
for (const c of value) {
|
||||
result += String.fromCharCode(c);
|
||||
for (let i = 0; i < value.length; i++) {
|
||||
result += String.fromCharCode(value[i]);
|
||||
}
|
||||
|
||||
return result;
|
||||
|
||||
@@ -0,0 +1,24 @@
|
||||
// Copyright 2017-2021 @polkadot/x-textencoder authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import { xglobal } from '@polkadot/x-global';
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
||||
// @ts-ignore
|
||||
xglobal.TextDecoder = undefined;
|
||||
|
||||
describe('TextDecoder (node)', (): void => {
|
||||
let TD: typeof TextDecoder;
|
||||
|
||||
beforeEach(async (): Promise<void> => {
|
||||
const node = await import('./node');
|
||||
|
||||
TD = node.TextDecoder;
|
||||
});
|
||||
|
||||
it('encodes correctly', (): void => {
|
||||
expect(
|
||||
new TD().decode(new Uint8Array([97, 98, 99]))
|
||||
).toEqual('abc');
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,12 @@
|
||||
// Copyright 2017-2021 @polkadot/x-textencoder authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import { TextEncoder } from './fallback';
|
||||
|
||||
describe('TextEncoder (fallback)', (): void => {
|
||||
it('encodes correctly', (): void => {
|
||||
expect(
|
||||
new TextEncoder().encode('abc')
|
||||
).toEqual(new Uint8Array([97, 98, 99]));
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,24 @@
|
||||
// Copyright 2017-2021 @polkadot/x-textencoder authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import { xglobal } from '@polkadot/x-global';
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
||||
// @ts-ignore
|
||||
xglobal.TextEncoder = undefined;
|
||||
|
||||
describe('TextEncoder (node)', (): void => {
|
||||
let TE: typeof TextEncoder;
|
||||
|
||||
beforeEach(async (): Promise<void> => {
|
||||
const node = await import('./node');
|
||||
|
||||
TE = node.TextEncoder;
|
||||
});
|
||||
|
||||
it('encodes correctly', (): void => {
|
||||
expect(
|
||||
new TE().encode('abc')
|
||||
).toEqual(new Uint8Array([97, 98, 99]));
|
||||
});
|
||||
});
|
||||
@@ -16,9 +16,7 @@ class NodeFallback {
|
||||
|
||||
// For a Jest 26.0.1 environment, Buffer !== Uint8Array
|
||||
encode (value: string): Uint8Array {
|
||||
const encoded = this.#encoder.encode(value);
|
||||
|
||||
return Uint8Array.from(encoded);
|
||||
return Uint8Array.from(this.#encoder.encode(value));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user