[FIX] reactivity: work even if no callback is given

This commit is contained in:
Géry Debongnie
2021-12-22 14:55:06 +01:00
committed by Samuel Degueldre
parent 489e20843c
commit 7fdca35a74
2 changed files with 8 additions and 1 deletions
+1 -1
View File
@@ -130,7 +130,7 @@ const reactiveCache = new WeakMap<Target, WeakMap<Callback, Reactive>>();
* reactive has changed
* @returns a proxy that tracks changes to it
*/
export function reactive<T extends Target>(target: T, callback: Callback): Reactive<T> {
export function reactive<T extends Target>(target: T, callback: Callback = () => {}): Reactive<T> {
if (!canBeMadeReactive(target)) {
throw new Error(`Cannot make the given value reactive`);
}
+7
View File
@@ -60,6 +60,13 @@ describe("Reactivity", () => {
expect(Array.isArray(state)).toBe(true);
});
test("work if there are no callback given", () => {
const state = reactive({ a: 1 });
expect(state.a).toBe(1);
state.a = 2;
expect(state.a).toBe(2);
});
test("Throw error if value is not proxifiable", () => {
expect(() => createReactive(1)).toThrow("Cannot make the given value reactive");
});