import { IOptionalEmptyError, Optional, isOptional } from '../lib/index'; describe('types/fn/optional', () => { test('from creates some/none', () => { expect(Optional.from('x').get()).toBe('x'); expect(Optional.from(null).present()).toBe(false); expect(Optional.from(undefined).present()).toBe(false); }); test('get throws on none', () => { expect(() => Optional.none().get()).toThrow(IOptionalEmptyError); }); test('map/filter/flatMap work', () => { const res = Optional.from(2) .map((n: number) => n * 2) .filter((n: number) => n > 3) .flatMap((n: number) => Optional.from(n.toString())); expect(res.get()).toBe('4'); }); test('orSome supplies fallback', () => { const res = Optional.none().orSome(() => 5); expect(res.get()).toBe(5); }); test('iterator yields only when present', () => { expect(Array.from(Optional.some(1))).toEqual([1]); expect(Array.from(Optional.none())).toEqual([]); }); test('isOptional detects tagged values', () => { expect(isOptional(Optional.some('x'))).toBe(true); expect(isOptional({})).toBe(false); }); });