Skip to content

Commit cd0ac31

Browse files
authored
Merge pull request #16 from gadget-inc/fix-identifier-bugs
Fix two bugs with identifiers
2 parents 2312f3d + d602e34 commit cd0ac31

File tree

4 files changed

+21
-3
lines changed

4 files changed

+21
-3
lines changed

spec/api.spec.ts

+6
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,8 @@ describe("getSnapshot", () => {
9393
const m = types.model({
9494
testModels: types.array(TestModel),
9595
ref: types.reference(NamedThing),
96+
safeRef1: types.safeReference(NamedThing),
97+
safeRef2: types.safeReference(NamedThing),
9698
});
9799

98100
const instance = m.createReadOnly({
@@ -101,10 +103,14 @@ describe("getSnapshot", () => {
101103
{ bool: false, nested: { key: "b", name: "b 2" }, frozen: { test: "string" } },
102104
],
103105
ref: "b",
106+
safeRef1: "x",
107+
safeRef2: "a",
104108
});
105109

106110
expect(instance.ref.name).toEqual("b 2");
107111
expect(getSnapshot(instance).ref).toEqual("b");
112+
expect(getSnapshot(instance).safeRef1).toBeUndefined();
113+
expect(getSnapshot(instance).safeRef2).toEqual("a");
108114
});
109115

110116
test("returns the proper root for an MST instance", () => {

spec/map.spec.ts

+11
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,17 @@ test("can create a map of simple types", () => {
1111
expect(mapType.createReadOnly({ a: "A", b: "B" }).toJSON()).toEqual(expect.objectContaining({ a: "A", b: "B" }));
1212
});
1313

14+
test("is can create a map of frozen types from a snapshot", () => {
15+
const mapType = types.map(types.frozen<string | null>());
16+
const snapshot = { A: "one", B: null };
17+
expect(mapType.createReadOnly(snapshot).toJSON()).toEqual(
18+
expect.objectContaining({
19+
A: "one",
20+
B: null,
21+
})
22+
);
23+
});
24+
1425
test("can create a map of complex types", () => {
1526
const mapType = types.map(InventoryItem);
1627

src/map.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { IInterceptor, IMapDidChange, IMapWillChange, Lambda } from "mobx";
22
import { isStateTreeNode, types } from "mobx-state-tree";
33
import { BaseType, setParent, setType } from "./base";
44
import { getSnapshot } from "./snapshot";
5-
import { $identifier, $type } from "./symbols";
5+
import { $type } from "./symbols";
66
import type { CreateTypes, IAnyStateTreeNode, IAnyType, IMapType, IMSTMap, Instance, InstantiateContext, SnapshotOut } from "./types";
77

88
export class QuickMap<T extends IAnyType> extends Map<string, T["InstanceType"]> implements IMSTMap<T> {
@@ -86,7 +86,7 @@ class MapType<T extends IAnyType> extends BaseType<
8686
for (const key in snapshot) {
8787
const item = this.childrenType.instantiate(snapshot[key], context);
8888
setParent(item, map);
89-
map.set(item[$identifier] ?? key, item);
89+
map.set(key, item);
9090
}
9191
}
9292

src/snapshot.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@ const snapshot = (value: any): unknown => {
3333
for (const name in type.properties) {
3434
const propType = type.properties[name];
3535
if (isReferenceType(propType)) {
36-
modelSnapshot[name] = (value as any)[name][$identifier];
36+
const maybeRef = (value as any)[name];
37+
modelSnapshot[name] = maybeRef?.[$identifier];
3738
} else {
3839
modelSnapshot[name] = snapshot((value as any)[name]);
3940
}

0 commit comments

Comments
 (0)