Skip to content

Commit

Permalink
Merge branch 'main' of https://github.com/takeramagan/solid into take…
Browse files Browse the repository at this point in the history
…ramagan-main
  • Loading branch information
ryansolid committed Jan 8, 2024
2 parents cbc8d3e + 01e6a3e commit 8c1b266
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
9 changes: 8 additions & 1 deletion packages/solid/store/src/modifiers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,16 @@ function applyState(
merge: boolean | undefined,
key: string | null
) {
const previous = parent[property];
let previous = parent[property];
if (target === previous) return;
const isArray = Array.isArray(target);
if (Array.isArray(previous) && !isArray) {
parent[property] = {};
} else if (!Array.isArray(previous) && isArray) {
parent[property] = [];
}

previous = parent[property];
if (
property !== $ROOT &&
(!isWrappable(target) ||
Expand Down
22 changes: 22 additions & 0 deletions packages/solid/store/test/modifiers.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,28 @@ describe("setState with reconcile", () => {
expect(store.id).toBe(undefined);
expect(store.value).toBe(undefined);
});

test("Reconcile overwirte an object with an array", () => {
const [store, setStore] = createStore<{ value: {} | [] }>({
value: { a: { b: 1 } }
});

setStore(reconcile({ value: { c: [1, 2, 3] } }));
expect(store.value).toEqual({ c: [1, 2, 3] });
});

test("Reconcile overwirte an array with an object", () => {
const [store, setStore] = createStore<{ value: {} | [] }>({
value: [1, 2, 3]
});
setStore(reconcile({ value: { name: "John" } }));
expect(Array.isArray(store.value)).toBeFalsy();
expect(store.value).toEqual({ name: "John" });
setStore(reconcile({ value: [1, 2, 3] }));
expect(store.value).toEqual([1, 2, 3]);
setStore(reconcile({ value: { q: "aa" } }));
expect(store.value).toEqual({ q: "aa" });
});
});

describe("setState with produce", () => {
Expand Down

0 comments on commit 8c1b266

Please sign in to comment.