import * as bazinga64 from './index' ; import { helloDecodedArray , helloDecodedString , helloEncodedArray , helloEncodedString , numberEncoded , } from './test/TestValues' ; const fixtures = require ( './test/fixtures.json' ) ; describe ( 'fromBase64' , ( ) => { it ( 'decodes arrays' , ( ) => { const decoded = bazinga64 . Decoder . fromBase64 ( helloEncodedArray ) ; const arrayBufferView = new Uint8Array ( helloDecodedArray ) ; expect ( decoded . asBytes ) . toEqual ( arrayBufferView ) ; expect ( decoded . asString ) . toBe ( helloDecodedString ) ; } ) ; it ( 'does not decode from an array with an invalid length' , ( ) => { expect ( ( ) => { bazinga64 . Decoder . fromBase64 ( helloDecodedArray ) ; } ) . toThrowError ( 'Invalid string. Length must be a multiple of 4.' ) ; } ) ; it ( 'decodes into a byte array' , ( ) => { const decoded = bazinga64 . Decoder . fromBase64 ( helloEncodedString ) ; const arrayBufferView = new Uint8Array ( helloDecodedArray ) ; expect ( decoded . asBytes ) . toEqual ( arrayBufferView ) ; expect ( decoded . asString ) . toBe ( helloDecodedString ) ; } ) ; it ( 'decodes into a string' , ( ) => { const encoded = 'SGVsbG8sIHdvcmxk' ; const decoded = bazinga64 . Decoder . fromBase64 ( encoded ) ; expect ( decoded . asString ) . toBe ( 'Hello, world' ) ; } ) ; it ( 'decodes numbers' , ( ) => { const decoded = bazinga64 . Decoder . fromBase64 ( numberEncoded ) ; expect ( decoded . asString ) . toBe ( '1337' ) ; } ) ; it ( 'sanitizes characters not found in the Base64 alphabet (according to RFC 2045).' , ( ) => { const firstLine = 'owABAaEAWEASf22tK9iQp8my5sgvtK8qiURy+5aCBglRKYLuwTlDYVBeAyydEVNDHd+pPoqvt1Es' ; const secondLine = '4zfU8cH1ccO02+4kfgoaAqEAoQBYIFBeAyydEVNDHd+pPoqvt1Es4zfU8cH1ccO02+4kfgoa' ; let encoded = `${ firstLine }\r\n${ secondLine }` ; let bytes = bazinga64 . Decoder . fromBase64 ( encoded ) . asBytes ; expect ( bytes . byteLength ) . toBeDefined ( ) ; encoded = `${ firstLine }${ secondLine }:` ; bytes = bazinga64 . Decoder . fromBase64 ( encoded ) . asBytes ; expect ( bytes . byteLength ) . toBeDefined ( ) ; encoded = `${ firstLine }${ secondLine }.` ; bytes = bazinga64 . Decoder . fromBase64 ( encoded ) . asBytes ; expect ( bytes . byteLength ) . toBeDefined ( ) ; encoded = `${ firstLine }${ secondLine }!` ; bytes = bazinga64 . Decoder . fromBase64 ( encoded ) . asBytes ; expect ( bytes . byteLength ) . toBeDefined ( ) ; encoded = `${ firstLine }\\${ secondLine }!` ; bytes = bazinga64 . Decoder . fromBase64 ( encoded ) . asBytes ; expect ( bytes . byteLength ) . toBeDefined ( ) ; } ) ; it ( 'decodes very long strings.' , async ( ) => { const encoded = fixtures . files [ 'yaoqi-lai-21901-unsplash_800.jpg' ] ; const bytes = bazinga64 . Decoder . fromBase64 ( encoded ) . asBytes ; expect ( bytes instanceof Uint8Array ) . toBe ( true ) ; expect ( bytes . byteLength ) . toBeGreaterThan ( 0 ) ; } ) ; } ) ;