Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 30 additions & 1 deletion src/get-scope.browser.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ describe("getClientScope", () => {
});
/**
* Current fix for this test is only implemented inside `[email protected]`
*
*
* 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 () => {
Expand Down Expand Up @@ -159,6 +159,35 @@ describe("getClientScope", () => {

expect(clientScopeTwo.getState($count)).toEqual(4);
});

test("should support custom serializers", async () => {
const $homeDate = createStore<Date | null>(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", () => {
Expand Down
5 changes: 5 additions & 0 deletions src/get-scope.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Loading