UNPKG

@idiosync/react-observable

Version:

State management control layer for React projects

244 lines (243 loc) 11.8 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const general_1 = require("./general"); describe('General utilities', () => { describe('isFunction', () => { it('should return true for functions', () => { expect((0, general_1.isFunction)(() => { })).toBe(true); expect((0, general_1.isFunction)(function () { })).toBe(true); expect((0, general_1.isFunction)(async () => { })).toBe(true); expect((0, general_1.isFunction)(function* () { })).toBe(true); }); it('should return false for non-functions', () => { expect((0, general_1.isFunction)('string')).toBe(false); expect((0, general_1.isFunction)(123)).toBe(false); expect((0, general_1.isFunction)(true)).toBe(false); expect((0, general_1.isFunction)(null)).toBe(false); expect((0, general_1.isFunction)(undefined)).toBe(false); expect((0, general_1.isFunction)({})).toBe(false); expect((0, general_1.isFunction)([])).toBe(false); }); it('should handle edge cases', () => { expect((0, general_1.isFunction)(new Function())).toBe(true); expect((0, general_1.isFunction)(Object)).toBe(true); expect((0, general_1.isFunction)(Array)).toBe(true); expect((0, general_1.isFunction)(Date)).toBe(true); }); }); describe('isObject', () => { it('should return true for objects', () => { expect((0, general_1.isObject)({})).toBe(true); expect((0, general_1.isObject)({ key: 'value' })).toBe(true); expect((0, general_1.isObject)(new Object())).toBe(true); expect((0, general_1.isObject)(Object.create(null))).toBe(true); }); it('should return false for non-objects', () => { expect((0, general_1.isObject)('string')).toBe(false); expect((0, general_1.isObject)(123)).toBe(false); expect((0, general_1.isObject)(true)).toBe(false); expect((0, general_1.isObject)(null)).toBe(false); expect((0, general_1.isObject)(undefined)).toBe(false); expect((0, general_1.isObject)(() => { })).toBe(false); }); it('should handle arrays', () => { expect((0, general_1.isObject)([])).toBe(true); expect((0, general_1.isObject)([1, 2, 3])).toBe(true); expect((0, general_1.isObject)(new Array())).toBe(true); }); it('should handle built-in objects', () => { expect((0, general_1.isObject)(new Date())).toBe(true); expect((0, general_1.isObject)(new RegExp('test'))).toBe(true); expect((0, general_1.isObject)(new Error())).toBe(true); expect((0, general_1.isObject)(new Map())).toBe(true); expect((0, general_1.isObject)(new Set())).toBe(true); }); }); describe('isPlainObject', () => { it('should return true for plain objects', () => { expect((0, general_1.isPlainObject)({})).toBe(true); expect((0, general_1.isPlainObject)({ key: 'value' })).toBe(true); expect((0, general_1.isPlainObject)(Object.create(null))).toBe(true); }); it('should return false for non-plain objects', () => { expect((0, general_1.isPlainObject)([])).toBe(false); expect((0, general_1.isPlainObject)(new Date())).toBe(false); expect((0, general_1.isPlainObject)(new RegExp('test'))).toBe(false); expect((0, general_1.isPlainObject)(new Error())).toBe(false); expect((0, general_1.isPlainObject)(new Map())).toBe(false); expect((0, general_1.isPlainObject)(new Set())).toBe(false); expect((0, general_1.isPlainObject)(() => { })).toBe(false); }); it('should handle edge cases', () => { expect((0, general_1.isPlainObject)(Object.create({}))).toBe(false); expect((0, general_1.isPlainObject)(Object.create(Object.prototype))).toBe(true); }); it('should handle null and undefined', () => { expect((0, general_1.isPlainObject)(null)).toBe(false); expect((0, general_1.isPlainObject)(undefined)).toBe(false); }); }); describe('tryCatch', () => { it('should return success result for successful operations', async () => { const result = await (0, general_1.tryCatch)(async () => 'success'); expect(result.success).toBe(true); expect(result.data).toBe('success'); expect(result.error).toBeUndefined(); }); it('should return error result for failed operations', async () => { const error = new Error('Test error'); const result = await (0, general_1.tryCatch)(async () => { throw error; }); expect(result.success).toBe(false); expect(result.data).toBeUndefined(); expect(result.error).toBe(error); }); it('should handle synchronous errors in async functions', async () => { const result = await (0, general_1.tryCatch)(async () => { throw new Error('Sync error'); }); expect(result.success).toBe(false); expect(result.error).toBeInstanceOf(Error); expect(result.error.message).toBe('Sync error'); }); it('should handle non-error throws', async () => { const result = await (0, general_1.tryCatch)(async () => { throw 'string error'; }); expect(result.success).toBe(false); expect(result.error).toBe('string error'); }); it('should handle complex return values', async () => { const complexValue = { name: 'test', data: [1, 2, 3], nested: { value: true }, }; const result = await (0, general_1.tryCatch)(async () => complexValue); expect(result.success).toBe(true); expect(result.data).toEqual(complexValue); }); }); describe('tryCatchSync', () => { it('should return success result for successful operations', () => { const result = (0, general_1.tryCatchSync)(() => 'success'); expect(result.success).toBe(true); expect(result.data).toBe('success'); expect(result.error).toBeUndefined(); }); it('should return error result for failed operations', () => { const error = new Error('Test error'); const result = (0, general_1.tryCatchSync)(() => { throw error; }); expect(result.success).toBe(false); expect(result.data).toBeUndefined(); expect(result.error).toBe(error); }); it('should handle non-error throws', () => { const result = (0, general_1.tryCatchSync)(() => { throw 'string error'; }); expect(result.success).toBe(false); expect(result.error).toBe('string error'); }); it('should handle complex return values', () => { const complexValue = { name: 'test', data: [1, 2, 3], nested: { value: true }, }; const result = (0, general_1.tryCatchSync)(() => complexValue); expect(result.success).toBe(true); expect(result.data).toEqual(complexValue); }); it('should handle functions that return undefined', () => { const result = (0, general_1.tryCatchSync)(() => undefined); expect(result.success).toBe(true); expect(result.data).toBeUndefined(); }); }); describe('uuid', () => { it('should generate unique strings', () => { const uuid1 = (0, general_1.uuid)(); const uuid2 = (0, general_1.uuid)(); expect(uuid1).not.toBe(uuid2); expect(typeof uuid1).toBe('string'); expect(typeof uuid2).toBe('string'); }); it('should generate strings with expected format', () => { const generatedUuid = (0, general_1.uuid)(); // Should be a string expect(typeof generatedUuid).toBe('string'); // Should not be empty expect(generatedUuid.length).toBeGreaterThan(0); // Should contain only valid characters (alphanumeric and hyphens) expect(generatedUuid).toMatch(/^[a-zA-Z0-9-]+$/); }); it('should generate multiple unique values', () => { const uuids = new Set(); for (let i = 0; i < 100; i++) { uuids.add((0, general_1.uuid)()); } expect(uuids.size).toBe(100); }); it('should handle rapid successive calls', () => { const uuids = []; for (let i = 0; i < 10; i++) { uuids.push((0, general_1.uuid)()); } // All should be unique const uniqueUuids = new Set(uuids); expect(uniqueUuids.size).toBe(10); }); }); describe('Integration tests', () => { it('should work together in complex scenarios', async () => { const testObject = { method: () => 'result', asyncMethod: async () => 'async result', errorMethod: () => { throw new Error('test error'); }, }; // Test isObject expect((0, general_1.isObject)(testObject)).toBe(true); expect((0, general_1.isPlainObject)(testObject)).toBe(true); // Test isFunction expect((0, general_1.isFunction)(testObject.method)).toBe(true); expect((0, general_1.isFunction)(testObject.asyncMethod)).toBe(true); expect((0, general_1.isFunction)(testObject.errorMethod)).toBe(true); // Test tryCatch with function const successResult = await (0, general_1.tryCatch)(testObject.asyncMethod); expect(successResult.success).toBe(true); expect(successResult.data).toBe('async result'); // Test tryCatchSync with function const syncResult = (0, general_1.tryCatchSync)(testObject.method); expect(syncResult.success).toBe(true); expect(syncResult.data).toBe('result'); // Test error handling const errorResult = (0, general_1.tryCatchSync)(testObject.errorMethod); expect(errorResult.success).toBe(false); expect(errorResult.error).toBeInstanceOf(Error); // Test uuid generation const id = (0, general_1.uuid)(); expect(typeof id).toBe('string'); expect(id.length).toBeGreaterThan(0); }); it('should handle edge cases in combination', () => { // Test with null/undefined expect((0, general_1.isObject)(null)).toBe(false); expect((0, general_1.isObject)(undefined)).toBe(false); expect((0, general_1.isFunction)(null)).toBe(false); expect((0, general_1.isFunction)(undefined)).toBe(false); // Test tryCatchSync with null/undefined const nullResult = (0, general_1.tryCatchSync)(() => null); expect(nullResult.success).toBe(true); expect(nullResult.data).toBeNull(); const undefinedResult = (0, general_1.tryCatchSync)(() => undefined); expect(undefinedResult.success).toBe(true); expect(undefinedResult.data).toBeUndefined(); }); }); });