summaryrefslogtreecommitdiff
path: root/tst/optional.test.ts
diff options
context:
space:
mode:
Diffstat (limited to 'tst/optional.test.ts')
-rw-r--r--tst/optional.test.ts37
1 files changed, 37 insertions, 0 deletions
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<number>().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<number>())).toEqual([]);
+ });
+
+ test('isOptional detects tagged values', () => {
+ expect(isOptional(Optional.some('x'))).toBe(true);
+ expect(isOptional({})).toBe(false);
+ });
+});