session-desktop/libtextsecure/test/helpers_test.js

30 lines
907 B
JavaScript
Raw Normal View History

2018-07-21 23:51:20 +02:00
describe('Helpers', () => {
describe('ArrayBuffer->String conversion', () => {
it('works', () => {
const b = new ArrayBuffer(3);
const a = new Uint8Array(b);
2018-05-02 18:51:22 +02:00
a[0] = 0;
a[1] = 255;
a[2] = 128;
assert.equal(getString(b), '\x00\xff\x80');
});
});
2018-07-21 23:51:20 +02:00
describe('stringToArrayBuffer', () => {
it('returns ArrayBuffer when passed string', () => {
const anArrayBuffer = new ArrayBuffer(1);
const typedArray = new Uint8Array(anArrayBuffer);
2018-05-02 18:51:22 +02:00
typedArray[0] = 'a'.charCodeAt(0);
assertEqualArrayBuffers(stringToArrayBuffer('a'), anArrayBuffer);
});
2018-07-21 23:51:20 +02:00
it('throws an error when passed a non string', () => {
const notStringable = [{}, undefined, null, new ArrayBuffer()];
notStringable.forEach(notString => {
assert.throw(() => {
2018-05-02 18:51:22 +02:00
stringToArrayBuffer(notString);
}, Error);
});
2018-05-02 18:51:22 +02:00
});
});
});