@simplito/privmx-webendpoint
Version:
PrivMX Web Endpoint library
53 lines (52 loc) • 2.65 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const utils_1 = require("../utils");
describe('Serialization Helpers', () => {
test('serializeObject should correctly serialize objects', () => {
const obj = { key: 'value', num: 42 };
const result = (0, utils_1.serializeObject)(obj);
expect(result).toBeInstanceOf(Uint8Array);
expect((0, utils_1.uint8ToStr)(result)).toBe(JSON.stringify(obj));
});
test('serializeObject should throw an error for invalid inputs', () => {
expect(() => (0, utils_1.serializeObject)(null)).toThrow(TypeError);
expect(() => (0, utils_1.serializeObject)('string')).toThrow(TypeError);
});
test('deserializeObject should correctly deserialize Uint8Array', () => {
const obj = { key: 'value', num: 42 };
const serialized = (0, utils_1.strToUint8)(JSON.stringify(obj));
const result = (0, utils_1.deserializeObject)(serialized);
expect(result).toEqual(obj);
});
test('deserializeObject should handle empty input gracefully', () => {
const empty = new Uint8Array();
const result = (0, utils_1.deserializeObject)(empty);
expect(result).toEqual({});
});
test('deserializeObject should throw an error for invalid inputs', () => {
expect(() => (0, utils_1.deserializeObject)(null)).toThrow(TypeError);
expect(() => (0, utils_1.deserializeObject)('string')).toThrow(TypeError);
const invalidJson = (0, utils_1.strToUint8)('{ key: value }');
expect(() => (0, utils_1.deserializeObject)(invalidJson)).toThrow(SyntaxError);
});
test('uint8ToStr should correctly convert Uint8Array to string', () => {
const str = 'Hello, world!';
const arr = (0, utils_1.strToUint8)(str);
const result = (0, utils_1.uint8ToStr)(arr);
expect(result).toBe(str);
});
test('uint8ToStr should throw an error for invalid inputs', () => {
expect(() => (0, utils_1.uint8ToStr)(null)).toThrow(TypeError);
expect(() => (0, utils_1.uint8ToStr)('string')).toThrow(TypeError);
});
test('strToUint8 should correctly convert string to Uint8Array', () => {
const str = 'Hello, world!';
const result = (0, utils_1.strToUint8)(str);
expect(result).toBeInstanceOf(Uint8Array);
expect((0, utils_1.uint8ToStr)(result)).toBe(str);
});
test('strToUint8 should throw an error for invalid inputs', () => {
expect(() => (0, utils_1.strToUint8)(null)).toThrow(TypeError);
expect(() => (0, utils_1.strToUint8)(42)).toThrow(TypeError);
});
});