From 666674327f009e9b1013218fc384f193b64c6997 Mon Sep 17 00:00:00 2001 From: Elizabeth Hunt Date: Sun, 14 Dec 2025 22:39:18 -0800 Subject: Adds unit tests --- tst/optional.test.ts | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 tst/optional.test.ts (limited to 'tst/optional.test.ts') diff --git a/tst/optional.test.ts b/tst/optional.test.ts new file mode 100644 index 0000000..ece6b31 --- /dev/null +++ b/tst/optional.test.ts @@ -0,0 +1,37 @@ +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); + }); +}); -- cgit v1.2.3-70-g09d2