diff --git a/src/get-scope.browser.test.ts b/src/get-scope.browser.test.ts index 81c5097..54fa014 100644 --- a/src/get-scope.browser.test.ts +++ b/src/get-scope.browser.test.ts @@ -96,7 +96,7 @@ describe("getClientScope", () => { }); /** * Current fix for this test is only implemented inside `effector-react@22.5.4` - * + * * TODO: After fix is ported into original createWatch of `effector` package in the 23.0.0 release, remove skip */ test.skip("watchers should re-run, if value is changed after server values injection", async () => { @@ -159,6 +159,35 @@ describe("getClientScope", () => { expect(clientScopeTwo.getState($count)).toEqual(4); }); + + test("should support custom serializers", async () => { + const $homeDate = createStore(null, { + serialize: { + read: (dateStringOrNull) => + typeof dateStringOrNull === "string" + ? new Date(dateStringOrNull) + : null, + write: (dateOrNull) => (dateOrNull ? dateOrNull.toISOString() : null), + }, + sid: "test_sid", + }); + + const serverScope = fork(); + + await allSettled($homeDate, { + scope: serverScope, + params: new Date(2024, 10, 3), + }); + + const values = serialize(serverScope); + + const scope = getScope(values); + + const clientValue = scope.getState($homeDate); + + expect(clientValue instanceof Date).toBe(true); + expect(clientValue!.getTime()).toEqual(new Date(2024, 10, 3).getTime()); + }); }); describe("getScope implementation details", () => { diff --git a/src/get-scope.ts b/src/get-scope.ts index a562222..4480a22 100644 --- a/src/get-scope.ts +++ b/src/get-scope.ts @@ -75,6 +75,11 @@ function INTERNAL_getClientScope(values?: Values) { function HACK_injectValues(scope: Scope, values: Values) { // @ts-expect-error this is a really hacky way to "hydrate" scope Object.assign(scope.values.sidMap, values); + /** + * We should explicitly set this flag to true, because otherwise the scope will be treated as it was not created from serialized values + * => effector will not apply custom serializers to the scope + */ + (scope as any).fromSerialize = true; } function HACK_updateScopeRefs(tscope: Scope, values: Values) {