Additional tests (need to expand more...) (#1172)

* Additional utility tests

* Args on constructor
This commit is contained in:
Jaco
2021-11-11 15:20:01 +02:00
committed by GitHub
parent 313b44388a
commit 9e1e49a1b8
7 changed files with 94 additions and 6 deletions
@@ -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');
});
});
+3 -3
View File
@@ -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;
+24
View File
@@ -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');
});
});