UNPKG

@yoroi/common

Version:

The Common package of Yoroi SDK

295 lines (293 loc) 12.8 kB
"use strict"; var _zod = require("zod"); var _parsers = require("./parsers"); describe('parsers', () => { describe('isArrayOfType', () => { it('returns true if array is empty', () => { expect((0, _parsers.isArrayOfType)([], _parsers.isString)).toEqual(true); }); it('returns true if array contains only elements of given type', () => { expect((0, _parsers.isArrayOfType)(['a', 'b', 'c'], _parsers.isString)).toEqual(true); }); it('returns false if array contains elements of different type', () => { expect((0, _parsers.isArrayOfType)(['a', 'b', 1], _parsers.isString)).toEqual(false); }); }); describe('isString', () => { it('returns true if string', () => { expect((0, _parsers.isString)('hello')).toEqual(true); }); it('returns false if not string', () => { expect((0, _parsers.isString)(123)).toEqual(false); expect((0, _parsers.isString)({})).toEqual(false); expect((0, _parsers.isString)([])).toEqual(false); expect((0, _parsers.isString)(null)).toEqual(false); expect((0, _parsers.isString)(undefined)).toEqual(false); expect((0, _parsers.isString)(true)).toEqual(false); }); }); describe('isUrl', () => { it('returns true if string is a valid url', () => { expect((0, _parsers.isUrl)('https://domiain.com')).toEqual(true); expect((0, _parsers.isUrl)('http://domain.com')).toEqual(true); expect((0, _parsers.isUrl)('bitcoin://domain.com/path')).toEqual(true); expect((0, _parsers.isUrl)('ipfs://domain.com/path?query=string')).toEqual(true); expect((0, _parsers.isUrl)('https://domain.com/path?query=string#hash')).toEqual(true); }); it('returns false if string is not a valid url', () => { expect((0, _parsers.isUrl)('hello')).toEqual(false); expect((0, _parsers.isUrl)('123')).toEqual(false); expect((0, _parsers.isUrl)({})).toEqual(false); expect((0, _parsers.isUrl)([])).toEqual(false); expect((0, _parsers.isUrl)(null)).toEqual(false); expect((0, _parsers.isUrl)(undefined)).toEqual(false); expect((0, _parsers.isUrl)(true)).toEqual(false); }); }); describe('isRecord', () => { it('returns true if is an object', () => { expect((0, _parsers.isRecord)({})).toEqual(true); }); it('returns false if is not an object or its array or its null', () => { expect((0, _parsers.isRecord)([])).toEqual(false); expect((0, _parsers.isRecord)(null)).toEqual(false); expect((0, _parsers.isRecord)(undefined)).toEqual(false); expect((0, _parsers.isRecord)(123)).toEqual(false); expect((0, _parsers.isRecord)('hello')).toEqual(false); expect((0, _parsers.isRecord)(true)).toEqual(false); }); }); describe('isNonNullable', () => { it('returns true if value is not null nor undefined', () => { expect((0, _parsers.isNonNullable)(1)).toEqual(true); expect((0, _parsers.isNonNullable)('hello')).toEqual(true); expect((0, _parsers.isNonNullable)({})).toEqual(true); expect((0, _parsers.isNonNullable)([])).toEqual(true); expect((0, _parsers.isNonNullable)(false)).toEqual(true); expect((0, _parsers.isNonNullable)(true)).toEqual(true); expect((0, _parsers.isNonNullable)(0)).toEqual(true); expect((0, _parsers.isNonNullable)('')).toEqual(true); }); it('returns false if null or undefined', () => { expect((0, _parsers.isNonNullable)(null)).toEqual(false); expect((0, _parsers.isNonNullable)(undefined)).toEqual(false); }); }); describe('parseBoolean', () => { it('should return a boolean if the data is a boolean', () => { expect((0, _parsers.parseBoolean)('true')).toBe(true); expect((0, _parsers.parseBoolean)('false')).toBe(false); }); it('should return undefined if the data is not a boolean', () => { expect((0, _parsers.parseBoolean)('abc')).toBeUndefined(); expect((0, _parsers.parseBoolean)('123')).toBeUndefined(); }); }); describe('parseString', () => { it('should return a string if the data is a string', () => { expect((0, _parsers.parseString)('"hello"')).toBe('hello'); }); it('should return undefined if the data is not a string', () => { expect((0, _parsers.parseString)('true')).toBeUndefined(); expect((0, _parsers.parseString)('123')).toBeUndefined(); expect((0, _parsers.parseString)({})).toBeUndefined(); expect((0, _parsers.parseString)({ hi: 1 })).toBeUndefined(); expect((0, _parsers.parseString)(1)).toBeUndefined(); expect((0, _parsers.parseString)(null)).toBeUndefined(); expect((0, _parsers.parseString)(undefined)).toBeUndefined(); }); }); describe('parseNumber', () => { it('should return a number if the parsed data is a number', () => { expect((0, _parsers.parseNumber)('42')).toEqual(42); expect((0, _parsers.parseNumber)(42)).toEqual(42); expect((0, _parsers.parseNumber)('3.14')).toEqual(3.14); }); it('should return undefined if the parsed data is not a number', () => { expect((0, _parsers.parseNumber)('abc')).toBeUndefined(); expect((0, _parsers.parseNumber)({})).toBeUndefined(); expect((0, _parsers.parseNumber)(null)).toBeUndefined(); expect((0, _parsers.parseNumber)(undefined)).toBeUndefined(); expect((0, _parsers.parseNumber)(true)).toBeUndefined(); }); it('should return undefined for non-number strings', () => { expect((0, _parsers.parseNumber)('hello')).toBeUndefined(); expect((0, _parsers.parseNumber)('true')).toBeUndefined(); expect((0, _parsers.parseNumber)('false')).toBeUndefined(); }); it('should handle edge cases', () => { expect((0, _parsers.parseNumber)(Infinity)).toBeUndefined(); expect((0, _parsers.parseNumber)(-Infinity)).toBeUndefined(); expect((0, _parsers.parseNumber)(NaN)).toBeUndefined(); // Depends on how you define "isNumber" }); }); describe('parseSafe', () => { it('should return parsed JSON if valid JSON', () => { expect((0, _parsers.parseSafe)('{"key": "value"}')).toEqual({ key: 'value' }); }); it('should return undefined if invalid JSON', () => { expect((0, _parsers.parseSafe)('{key: value}')).toBeUndefined(); }); }); describe('isBoolean', () => { it('should return true if the data is a boolean', () => { expect((0, _parsers.isBoolean)(true)).toBe(true); expect((0, _parsers.isBoolean)(false)).toBe(true); }); it('should return false if the data is not a boolean', () => { expect((0, _parsers.isBoolean)('true')).toBe(false); }); }); describe('isNumber', () => { it('should return true if the data is a number', () => { expect((0, _parsers.isNumber)(123)).toBe(true); }); it('should return false if the data is not a number', () => { expect((0, _parsers.isNumber)('123')).toBe(false); }); }); describe('createTypeGuardFromSchema', () => { const isPerson = (0, _parsers.createTypeGuardFromSchema)(_zod.z.object({ name: _zod.z.string(), age: _zod.z.number() })); it('should return true if the data matches the schema', () => { expect(isPerson({ name: 'John', age: 30 })).toBe(true); }); it('should return false if the data does not match the schema', () => { expect(isPerson({ name: 'John', age: '30' })).toBe(false); }); }); describe('isArray', () => { it('should return true if the data is an array', () => { expect((0, _parsers.isArray)(['a', 'b', 'c'])).toBe(true); }); it('should return false if the data is not an array', () => { expect((0, _parsers.isArray)('hello')).toBe(false); }); }); describe('isPositiveNumber', () => { it('should return true if the data is a positive number', () => { expect((0, _parsers.isPositiveNumber)(1)).toBe(true); }); it('should return false if the data is zero or a negative number', () => { expect((0, _parsers.isPositiveNumber)(0)).toBe(false); expect((0, _parsers.isPositiveNumber)(-1)).toBe(false); }); it('should return false if the data is not a number', () => { expect((0, _parsers.isPositiveNumber)('1')).toBe(false); expect((0, _parsers.isPositiveNumber)(undefined)).toBe(false); expect((0, _parsers.isPositiveNumber)(null)).toBe(false); expect((0, _parsers.isPositiveNumber)({})).toBe(false); expect((0, _parsers.isPositiveNumber)([])).toBe(false); expect((0, _parsers.isPositiveNumber)(NaN)).toBe(false); expect((0, _parsers.isPositiveNumber)(Infinity)).toBe(false); }); }); describe('isArrayOfString', () => { it('should return true if the data is an array of strings', () => { expect((0, _parsers.isArrayOfString)(['a', 'b', 'c'])).toBe(true); }); it('should return false if the data is not an array of strings', () => { expect((0, _parsers.isArrayOfString)(['a', 1, 'c'])).toBe(false); expect((0, _parsers.isArrayOfString)([1, 2, 3])).toBe(false); expect((0, _parsers.isArrayOfString)('abc')).toBe(false); }); }); describe('isStringOrArrayOfString', () => { it('should return true if the data is a string', () => { expect((0, _parsers.isStringOrArrayOfString)('hello')).toBe(true); }); it('should return true if the data is an array of strings', () => { expect((0, _parsers.isStringOrArrayOfString)(['hello', 'world'])).toBe(true); expect((0, _parsers.isStringOrArrayOfString)([])).toBe(true); }); it('should return false if the data is neither a string nor an array of strings', () => { expect((0, _parsers.isStringOrArrayOfString)(['hello', 1])).toBe(false); expect((0, _parsers.isStringOrArrayOfString)(123)).toBe(false); expect((0, _parsers.isStringOrArrayOfString)(null)).toBe(false); expect((0, _parsers.isStringOrArrayOfString)(undefined)).toBe(false); expect((0, _parsers.isStringOrArrayOfString)({})).toBe(false); }); }); describe('isKeyOf', () => { it('should return true if the key is in the object', () => { expect((0, _parsers.isKeyOf)('a', { a: 1 })).toBe(true); }); it('should return false if the key is not in the object', () => { expect((0, _parsers.isKeyOf)('b', { a: 1 })).toBe(false); expect((0, _parsers.isKeyOf)(1, { a: 1 })).toBe(false); expect((0, _parsers.isKeyOf)('a', { 1: 1 })).toBe(false); }); }); describe('getKeys', () => { it('should return the keys of the object', () => { expect((0, _parsers.getKeys)({ a: 1, b: 2 })).toEqual(['a', 'b']); }); it('should return an empty array if the object is empty', () => { expect((0, _parsers.getKeys)({})).toEqual([]); }); }); describe('isStringLiteral function', () => { const literalsArray = ['apple', 'banana', 'cherry']; it('should return true for a value that is part of the literals array', () => { const value = 'banana'; const result = (0, _parsers.isStringLiteral)(literalsArray, value); expect(result).toBe(true); }); it('should return false for a value that is not part of the literals array', () => { const value = 'orange'; const result = (0, _parsers.isStringLiteral)(literalsArray, value); expect(result).toBe(false); }); it('should return false for a number value when literals array contains only strings', () => { const value = 123; const result = (0, _parsers.isStringLiteral)(literalsArray, value); expect(result).toBe(false); }); it('should return false for an object value when literals array contains only strings', () => { const value = { fruit: 'banana' }; const result = (0, _parsers.isStringLiteral)(literalsArray, value); expect(result).toBe(false); }); it('should return false for a boolean value when literals array contains only strings', () => { const value = true; const result = (0, _parsers.isStringLiteral)(literalsArray, value); expect(result).toBe(false); }); it('should return false for null when literals array contains only strings', () => { const value = null; const result = (0, _parsers.isStringLiteral)(literalsArray, value); expect(result).toBe(false); }); it('should return false for undefined when literals array contains only strings', () => { const value = undefined; const result = (0, _parsers.isStringLiteral)(literalsArray, value); expect(result).toBe(false); }); }); }); //# sourceMappingURL=parsers.test.js.map