Skip to content

Commit

Permalink
Make array snapshots not be quick arrays
Browse files Browse the repository at this point in the history
  • Loading branch information
thegedge committed Nov 7, 2023
1 parent e7c370b commit 0c2d2d0
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 8 deletions.
8 changes: 4 additions & 4 deletions spec/array.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import { types } from "../src";

test("can create an array of simple types", () => {
const arrayType = types.array(types.string);
expect(Array.from(arrayType.createReadOnly())).toEqual([]);
expect(Array.from(arrayType.createReadOnly(["a", "b"]))).toEqual(["a", "b"]);
expect(arrayType.createReadOnly().toJSON()).toEqual([]);
expect(arrayType.createReadOnly(["a", "b"]).toJSON()).toEqual(["a", "b"]);
});

test("can create an array of complex types", () => {
Expand All @@ -14,8 +14,8 @@ test("can create an array of complex types", () => {

const arrayType = types.array(inventoryType);

expect(Array.from(arrayType.createReadOnly())).toEqual([]);
expect(Array.from(arrayType.createReadOnly([{ itemName: "A", amount: 10 }, { itemName: "B" }]))).toEqual([
expect(arrayType.createReadOnly().toJSON()).toEqual([]);
expect(arrayType.createReadOnly([{ itemName: "A", amount: 10 }, { itemName: "B" }]).toJSON()).toEqual([
expect.objectContaining({ itemName: "A", amount: 10 }),
expect.objectContaining({ itemName: "B", amount: 0 }),
]);
Expand Down
4 changes: 2 additions & 2 deletions spec/class-model.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -518,8 +518,8 @@ describe("class models", () => {
describe("class models with array/map/complex properties", () => {
test("should default to empty arrays/maps when createReadOnly with undefined values", () => {
const instance = NestedComplex.createReadOnly({});
expect(Array.from(instance.stringArray)).toEqual([]);
expect(Object.fromEntries(Object.entries(instance.numberMap))).toEqual({});
expect(instance.stringArray.toJSON()).toEqual([]);
expect(instance.numberMap.toJSON()).toEqual({});
});
});

Expand Down
3 changes: 2 additions & 1 deletion src/array.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { isStateTreeNode, types } from "mobx-state-tree";
import { BaseType } from "./base";
import { ensureRegistered } from "./class-model";
import { getSnapshot } from "./snapshot";
import { $env, $parent, $readOnly, $type } from "./symbols";
import type { IAnyStateTreeNode, IAnyType, IArrayType, IMSTArray, IStateTreeNode, Instance, InstantiateContext } from "./types";

Expand Down Expand Up @@ -48,7 +49,7 @@ export class QuickArray<T extends IAnyType> extends Array<Instance<T>> implement
}

toJSON(): Instance<T>[] {
return this;
return getSnapshot(this);
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/snapshot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export function getSnapshot<T extends IAnyType>(value: IStateTreeNode<T>): Snaps

const snapshot = (value: any): unknown => {
if (value instanceof QuickArray) {
return value.map((v) => snapshot(v));
return Array.from(value.map((v) => snapshot(v)));
}

if (value instanceof QuickMap) {
Expand Down

0 comments on commit 0c2d2d0

Please sign in to comment.