|
| 1 | +// © 2026 Adobe. MIT License. See /LICENSE for details. |
| 2 | + |
| 3 | +import { describe, test, expect, assertType } from "vitest"; |
| 4 | +import { withSwitch } from "./with-switch.js"; |
| 5 | +import { fromConstant } from "./from-constant.js"; |
| 6 | +import { createState } from "./create-state.js"; |
| 7 | +import type { Observe } from "./index.js"; |
| 8 | + |
| 9 | +describe("withSwitch", () => { |
| 10 | + test("should switch and observe the selected observable", () => { |
| 11 | + const record = { |
| 12 | + a: fromConstant(1), |
| 13 | + b: fromConstant(2), |
| 14 | + c: fromConstant(3), |
| 15 | + }; |
| 16 | + const key = fromConstant("b" as const); |
| 17 | + const picked = withSwitch(record, key); |
| 18 | + |
| 19 | + let result: number | undefined; |
| 20 | + picked((value) => { |
| 21 | + result = value; |
| 22 | + })(); |
| 23 | + |
| 24 | + expect(result).toBe(2); |
| 25 | + }); |
| 26 | + |
| 27 | + test("should switch observables when key changes", () => { |
| 28 | + const record = { |
| 29 | + a: fromConstant(10), |
| 30 | + b: fromConstant(20), |
| 31 | + c: fromConstant(30), |
| 32 | + }; |
| 33 | + const [key, setKey] = createState<"a" | "b" | "c">("a"); |
| 34 | + const picked = withSwitch(record, key); |
| 35 | + |
| 36 | + const values: number[] = []; |
| 37 | + const unsubscribe = picked((value) => { |
| 38 | + values.push(value); |
| 39 | + }); |
| 40 | + |
| 41 | + setKey("b"); |
| 42 | + setKey("c"); |
| 43 | + |
| 44 | + unsubscribe(); |
| 45 | + |
| 46 | + expect(values).toEqual([10, 20, 30]); |
| 47 | + }); |
| 48 | + |
| 49 | + test("should unsubscribe from previous observable when key changes", () => { |
| 50 | + const [observableA, setA] = createState(100); |
| 51 | + const [observableB, setB] = createState(200); |
| 52 | + const record = { a: observableA, b: observableB }; |
| 53 | + const [key, setKey] = createState<"a" | "b">("a"); |
| 54 | + const picked = withSwitch(record, key); |
| 55 | + |
| 56 | + const values: number[] = []; |
| 57 | + const unsubscribe = picked((value) => { |
| 58 | + values.push(value); |
| 59 | + }); |
| 60 | + |
| 61 | + setA(101); // Should be observed |
| 62 | + setKey("b"); // Switch to b |
| 63 | + setA(102); // Should NOT be observed (unsubscribed from a) |
| 64 | + setB(201); // Should be observed |
| 65 | + |
| 66 | + unsubscribe(); |
| 67 | + |
| 68 | + expect(values).toEqual([100, 101, 200, 201]); |
| 69 | + }); |
| 70 | + |
| 71 | + test("should clean up all subscriptions on unobserve", () => { |
| 72 | + const [observableA, setA] = createState(1); |
| 73 | + const [observableB, setB] = createState(2); |
| 74 | + const record = { a: observableA, b: observableB }; |
| 75 | + const [key, setKey] = createState<"a" | "b">("a"); |
| 76 | + const picked = withSwitch(record, key); |
| 77 | + |
| 78 | + const values: number[] = []; |
| 79 | + const unsubscribe = picked((value) => { |
| 80 | + values.push(value); |
| 81 | + }); |
| 82 | + |
| 83 | + setA(10); |
| 84 | + unsubscribe(); |
| 85 | + |
| 86 | + // After unsubscribe, no further notifications |
| 87 | + setA(20); |
| 88 | + setB(30); |
| 89 | + setKey("b"); |
| 90 | + |
| 91 | + expect(values).toEqual([1, 10]); |
| 92 | + }); |
| 93 | + |
| 94 | + test("should handle rapid key changes", () => { |
| 95 | + const record = { |
| 96 | + x: fromConstant("first"), |
| 97 | + y: fromConstant("second"), |
| 98 | + z: fromConstant("third"), |
| 99 | + }; |
| 100 | + const [key, setKey] = createState<"x" | "y" | "z">("x"); |
| 101 | + const picked = withSwitch(record, key); |
| 102 | + |
| 103 | + const values: string[] = []; |
| 104 | + const unsubscribe = picked((value) => { |
| 105 | + values.push(value); |
| 106 | + }); |
| 107 | + |
| 108 | + setKey("y"); |
| 109 | + setKey("z"); |
| 110 | + setKey("x"); |
| 111 | + setKey("z"); |
| 112 | + |
| 113 | + unsubscribe(); |
| 114 | + |
| 115 | + expect(values).toEqual(["first", "second", "third", "first", "third"]); |
| 116 | + }); |
| 117 | + |
| 118 | + test("should throw error when key is not in record", () => { |
| 119 | + const record = { |
| 120 | + a: fromConstant(1), |
| 121 | + b: fromConstant(2), |
| 122 | + }; |
| 123 | + const [key, setKey] = createState<string>("a"); |
| 124 | + const picked = withSwitch(record, key); |
| 125 | + |
| 126 | + const unsubscribe = picked(() => {}); |
| 127 | + |
| 128 | + expect(() => { |
| 129 | + setKey("invalid"); |
| 130 | + }).toThrow('Key "invalid" not found in observable record'); |
| 131 | + |
| 132 | + unsubscribe(); |
| 133 | + }); |
| 134 | + |
| 135 | + test("type inference: should infer union type from subset of keys", () => { |
| 136 | + // Compile-time type test |
| 137 | + const record = { |
| 138 | + a: fromConstant(true), |
| 139 | + b: fromConstant("hello"), |
| 140 | + c: fromConstant(42), |
| 141 | + }; |
| 142 | + |
| 143 | + const key = fromConstant("a" as "a" | "b"); |
| 144 | + const result = withSwitch(record, key); |
| 145 | + |
| 146 | + // Type should be Observe<boolean | string>, not Observe<boolean | string | number> |
| 147 | + assertType<Observe<boolean | string>>(result); |
| 148 | + }); |
| 149 | + |
| 150 | + test("type inference: should work with all keys", () => { |
| 151 | + // Compile-time type test |
| 152 | + const record = { |
| 153 | + a: fromConstant(true), |
| 154 | + b: fromConstant("hello"), |
| 155 | + c: fromConstant(42), |
| 156 | + }; |
| 157 | + |
| 158 | + const key = fromConstant("a" as "a" | "b" | "c"); |
| 159 | + const result = withSwitch(record, key); |
| 160 | + |
| 161 | + // Type should be Observe<boolean | string | number> |
| 162 | + assertType<Observe<boolean | string | number>>(result); |
| 163 | + }); |
| 164 | + |
| 165 | + test("type inference: should work with single key", () => { |
| 166 | + // Compile-time type test |
| 167 | + const record = { |
| 168 | + a: fromConstant(true), |
| 169 | + b: fromConstant("hello"), |
| 170 | + c: fromConstant(42), |
| 171 | + }; |
| 172 | + |
| 173 | + const key = fromConstant("b" as const); |
| 174 | + const result = withSwitch(record, key); |
| 175 | + |
| 176 | + // Type should be Observe<string> |
| 177 | + assertType<Observe<string>>(result); |
| 178 | + }); |
| 179 | +}); |
0 commit comments