|
1 | 1 | import type { IsExact } from "conditional-type-checks"; |
2 | 2 | import { assert } from "conditional-type-checks"; |
3 | | -import type { SnapshotOut } from "../src"; |
| 3 | +import type { IReferenceType, SnapshotOut } from "../src"; |
4 | 4 | import { types } from "../src"; |
5 | 5 | import { getSnapshot } from "../src/api"; |
6 | 6 | import { TestClassModel } from "./fixtures/TestClassModel"; |
@@ -99,4 +99,35 @@ describe("getSnapshot", () => { |
99 | 99 | verifySnapshot(snapshot); |
100 | 100 | assert<IsExact<typeof snapshot.map.test_key, SnapshotOut<typeof NamedThing>>>(true); |
101 | 101 | }); |
| 102 | + |
| 103 | + test("snapshots a reference type correctly in a map / array", () => { |
| 104 | + const RootType = types.model({ |
| 105 | + things: types.map(NamedThing), |
| 106 | + refMap: types.map(types.reference(NamedThing)), |
| 107 | + refArray: types.array(types.reference(NamedThing)), |
| 108 | + }); |
| 109 | + |
| 110 | + const instance = RootType.createReadOnly({ |
| 111 | + things: { |
| 112 | + a: { key: "a", name: "test a" }, |
| 113 | + b: { key: "b", name: "test b" }, |
| 114 | + }, |
| 115 | + refMap: { a: "a", b: "b" }, |
| 116 | + refArray: ["a", "b"], |
| 117 | + }); |
| 118 | + |
| 119 | + const snapshot = getSnapshot(instance); |
| 120 | + assert<IsExact<typeof snapshot.things.test_key, SnapshotOut<typeof NamedThing>>>(true); |
| 121 | + assert<IsExact<typeof snapshot.refMap.test_key, SnapshotOut<IReferenceType<typeof NamedThing>>>>(true); |
| 122 | + expect(snapshot.refMap.a).toEqual("a"); |
| 123 | + expect(snapshot.refMap.b).toEqual("b"); |
| 124 | + expect(snapshot.refArray[0]).toEqual("a"); |
| 125 | + expect(snapshot.refArray[1]).toEqual("b"); |
| 126 | + |
| 127 | + const reconstructedInstance = RootType.createReadOnly(snapshot); |
| 128 | + expect(reconstructedInstance.refMap.get("a")?.name).toEqual("test a"); |
| 129 | + expect(reconstructedInstance.refMap.get("b")?.name).toEqual("test b"); |
| 130 | + expect(reconstructedInstance.refArray[0]?.name).toEqual("test a"); |
| 131 | + expect(reconstructedInstance.refArray[1]?.name).toEqual("test b"); |
| 132 | + }); |
102 | 133 | }); |
0 commit comments