@@ -6,11 +6,17 @@ import {
66} from '@waiting/shared-core'
77import * as assert from 'power-assert'
88import rewire = require( 'rewire' )
9+ import {
10+ TextDecoder as NodeTextDecoder ,
11+ TextEncoder as NodeTextEncoder ,
12+ } from 'util'
913
1014import { ErrorMsg } from '../src/index'
1115import {
1216 parseDecodeInputBase64 ,
1317 parseEncodeInputString ,
18+ parseTextDecoder ,
19+ parseTextEncoder ,
1420 validateEncoder ,
1521} from '../src/lib/helper'
1622
@@ -74,4 +80,59 @@ describe(filename, () => {
7480 } )
7581 } )
7682
83+
84+ describe ( 'parseTextEncoder() works' , ( ) => {
85+ it ( 'with valid input' , ( ) => {
86+ const ret = parseTextEncoder ( NodeTextEncoder )
87+ assert ( ret === NodeTextEncoder )
88+ } )
89+
90+ it ( 'with invalid input' , ( ) => {
91+ try {
92+ // @ts -ignore
93+ const ret = parseTextEncoder ( null )
94+ // node.js v11+ exports global TextEncoder
95+ if ( typeof TextEncoder === 'function' ) {
96+ assert ( true )
97+ }
98+ else {
99+ assert ( false , 'Should throw error, but NOT' )
100+ }
101+ }
102+ catch ( ex ) {
103+ assert (
104+ ex . message && ex . message . includes ( ErrorMsg . textEncoderUndefined ) ,
105+ ex . message ,
106+ )
107+ }
108+ } )
109+ } )
110+
111+ describe ( 'parseTextDecoder() works' , ( ) => {
112+ it ( 'with valid input' , ( ) => {
113+ const ret = parseTextDecoder ( NodeTextDecoder )
114+ assert ( ret === NodeTextDecoder )
115+ } )
116+
117+ it ( 'with invalid input' , ( ) => {
118+ try {
119+ // @ts -ignore
120+ const ret = parseTextDecoder ( null )
121+ // node.js v11+ exports global TextDecoder
122+ if ( typeof TextDecoder === 'function' ) {
123+ assert ( true )
124+ }
125+ else {
126+ assert ( false , 'Should throw error, but NOT' )
127+ }
128+ }
129+ catch ( ex ) {
130+ assert (
131+ ex . message && ex . message . includes ( ErrorMsg . textDecoderUndefined ) ,
132+ ex . message ,
133+ )
134+ }
135+ } )
136+ } )
137+
77138} )
0 commit comments