|
| 1 | +/// <reference types="mocha" /> |
| 2 | + |
| 3 | +import { |
| 4 | + basename, |
| 5 | + join, |
| 6 | +} from '@waiting/shared-core' |
| 7 | +import * as assert from 'power-assert' |
| 8 | +import rewire = require('rewire') |
| 9 | +import { |
| 10 | + TextDecoder as NodeTextDecoder, |
| 11 | + TextEncoder as NodeTextEncoder, |
| 12 | +} from 'util' |
| 13 | + |
| 14 | +import { ErrorMsg } from '../src/index' |
| 15 | +import { |
| 16 | + fromBuffer, |
| 17 | +} from '../src/lib/browser' |
| 18 | +import { defaultConfig } from '../src/lib/config' |
| 19 | + |
| 20 | +import { input44, input8, input9 } from './config' |
| 21 | + |
| 22 | + |
| 23 | +const filename = basename(__filename) |
| 24 | +const mods = rewire('../src/lib/helper') |
| 25 | + |
| 26 | +describe(filename, () => { |
| 27 | + before(() => { |
| 28 | + defaultConfig.forceBrowser = true |
| 29 | + }) |
| 30 | + after(() => { |
| 31 | + defaultConfig.forceBrowser = false |
| 32 | + }) |
| 33 | + |
| 34 | + describe('fromBuffer() works', () => { |
| 35 | + it('with ArrayBuffer input', () => { |
| 36 | + input8.forEach(row => { |
| 37 | + const u8arr = Uint8Array.from(row[0]) |
| 38 | + const actual = fromBuffer(u8arr.buffer) |
| 39 | + const expected = row[1] |
| 40 | + assert(actual === expected, `Ensure that ${u8arr} serialise to ${expected}`) |
| 41 | + }) |
| 42 | + }) |
| 43 | + |
| 44 | + it('with Uint8Array input', () => { |
| 45 | + input8.forEach(row => { |
| 46 | + const u8arr = Uint8Array.from(row[0]) |
| 47 | + const actual = fromBuffer(u8arr) |
| 48 | + const expected = row[1] |
| 49 | + assert(actual === expected, `Ensure that ${u8arr} serialise to ${expected}`) |
| 50 | + }) |
| 51 | + }) |
| 52 | + |
| 53 | + it('with invalid input', () => { |
| 54 | + input44.forEach(value => { |
| 55 | + try { |
| 56 | + // @ts-ignore |
| 57 | + fromBuffer(value) |
| 58 | + assert(false, 'Should throw error, but NOT') |
| 59 | + } |
| 60 | + catch (ex) { |
| 61 | + assert( |
| 62 | + ex.message && ex.message.includes(ErrorMsg.fromArrayBufferInvalidParam), |
| 63 | + ex.message, |
| 64 | + ) |
| 65 | + } |
| 66 | + }) |
| 67 | + }) |
| 68 | + }) |
| 69 | + |
| 70 | + |
| 71 | +}) |
0 commit comments