-
Notifications
You must be signed in to change notification settings - Fork 25
/
recursive.test.ts
161 lines (131 loc) · 3.72 KB
/
recursive.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
import { assert, _ } from "spec.ts"
import {
Model,
ModelAutoTypeCheckingMode,
prop,
setGlobalConfig,
tProp,
types,
TypeToData,
} from "../../src"
import { testModel } from "../utils"
beforeEach(() => {
setGlobalConfig({
modelAutoTypeChecking: ModelAutoTypeCheckingMode.AlwaysOn,
})
})
test("self recursive", () => {
@testModel("myApp/TreeNode")
class TreeNode extends Model({ children: prop<TreeNode[]>(() => []), x: prop(0) }) {}
const tn = new TreeNode({ children: [new TreeNode({})] })
assert(tn, _ as TreeNode)
assert(tn.x, _ as number)
assert(tn.children, _ as TreeNode[])
assert(tn.children[0], _ as TreeNode)
assert(tn.children[0].x, _ as number)
assert(tn.children[0].children, _ as TreeNode[])
expect(tn.children[0] instanceof TreeNode).toBeTruthy()
})
test("self recursive type checked", () => {
@testModel("myAppTC/TreeNode")
class TreeNode extends Model({
children: tProp(types.array(types.model<TreeNode>(() => TreeNode)), () => []),
x: tProp(types.number, 0),
}) {}
const tn = new TreeNode({ children: [new TreeNode({})] })
assert(tn, _ as TreeNode)
assert(tn.x, _ as number)
assert(tn.children, _ as TreeNode[])
assert(tn.children[0], _ as TreeNode)
assert(tn.children[0].x, _ as number)
assert(tn.children[0].children, _ as TreeNode[])
expect(tn.children[0] instanceof TreeNode).toBeTruthy()
})
test("cross-referenced", () => {
@testModel("myApp/A")
class A extends Model({ b: prop<B | undefined>(), x: prop(0) }) {}
@testModel("myApp/B")
class B extends Model({ a: prop<A | undefined>(), y: prop("") }) {}
const a = new A({
b: new B({
a: new A({}),
}),
})
assert(a, _ as A)
assert(a.x, _ as number)
assert(a.b, _ as B | undefined)
assert(a.b!.y, _ as string)
assert(a.b!.a, _ as A | undefined)
assert(a.b!.a!.x, _ as number)
expect(a.b!.a instanceof A).toBeTruthy()
})
test("cross-referenced type checked", () => {
@testModel("myAppTC/A")
class A extends Model({
b: tProp(types.maybe(types.model<B>(() => B))),
x: tProp(types.number, 0),
}) {}
@testModel("myAppTC/B")
class B extends Model({
a: tProp(types.maybe(types.model<A>(() => A))),
y: tProp(types.string, ""),
}) {}
const a = new A({
b: new B({
a: new A({}),
}),
})
assert(a, _ as A)
assert(a.x, _ as number)
assert(a.b, _ as B | undefined)
assert(a.b!.y, _ as string)
assert(a.b!.a, _ as A | undefined)
assert(a.b!.a!.x, _ as number)
expect(a.b!.a instanceof A).toBeTruthy()
})
test("recursive with object", () => {
interface Obj {
aa?: AA
meObj?: Obj
}
@testModel("myApp/AA")
class AA extends Model({
obj: prop<Obj | undefined>(),
}) {}
const aa = new AA({
obj: {
aa: new AA({}),
meObj: { aa: new AA({}) },
},
})
assert(aa, _ as AA)
assert(aa.obj, _ as Obj | undefined)
assert(aa.obj!.aa, _ as AA | undefined)
assert(aa.obj!.meObj, _ as Obj | undefined)
assert(aa.obj!.meObj!.aa, _ as AA | undefined)
expect(aa.obj!.meObj!.aa instanceof AA).toBeTruthy()
})
test("recursive with object type checked", () => {
const typeObj = types.object(() => ({
aa: types.maybe(types.model<AA>(() => AA)),
meObj: types.maybe(typeObj),
x: types.maybe(types.number),
}))
type Obj2 = TypeToData<typeof typeObj>
@testModel("myAppTC/AA")
class AA extends Model({
obj: tProp(types.maybe(typeObj)),
}) {}
const aa = new AA({
obj: {
aa: new AA({}),
meObj: { aa: new AA({}) },
},
})
assert(aa, _ as AA)
assert(aa.obj, _ as Obj2 | undefined)
assert(aa.obj!.aa, _ as AA | undefined)
assert(aa.obj!.meObj, _ as Obj2 | undefined)
assert(aa.obj!.meObj!.aa, _ as AA | undefined)
expect(aa.obj!.meObj!.aa instanceof AA).toBeTruthy()
})