From 06fc3a2c776e6445dbe60d1f242b23791d1ddcbd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?G=C3=A9ry=20Debongnie?= Date: Wed, 22 Dec 2021 14:55:06 +0100 Subject: [PATCH] [FIX] reactivity: work even if no callback is given --- src/reactivity.ts | 2 +- tests/reactivity.test.ts | 7 +++++++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/src/reactivity.ts b/src/reactivity.ts index e507d048..1f3660e6 100644 --- a/src/reactivity.ts +++ b/src/reactivity.ts @@ -130,7 +130,7 @@ const reactiveCache = new WeakMap>(); * reactive has changed * @returns a proxy that tracks changes to it */ -export function reactive(target: T, callback: Callback): Reactive { +export function reactive(target: T, callback: Callback = () => {}): Reactive { if (!canBeMadeReactive(target)) { throw new Error(`Cannot make the given value reactive`); } diff --git a/tests/reactivity.test.ts b/tests/reactivity.test.ts index 87c1a95b..235bd96f 100644 --- a/tests/reactivity.test.ts +++ b/tests/reactivity.test.ts @@ -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"); });