/
define.spec.ts
173 lines (170 loc) 路 4.29 KB
/
define.spec.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
162
163
164
165
166
167
168
169
170
171
172
173
import { define, model, observable, autorun } from '..'
import { observe } from '../observe'
import { FormPath } from '@formily/shared'
import { batch } from '../batch'
describe('makeObservable', () => {
test('observable annotation', () => {
const target: any = {
aa: {},
}
define(target, {
aa: observable,
})
const handler = jest.fn()
const handler1 = jest.fn()
const handler2 = jest.fn()
autorun(() => {
handler(FormPath.getIn(target, 'aa.bb.cc'))
})
observe(target, handler1)
observe(target.aa, handler2)
target.aa.bb = { cc: { dd: { ee: 123 } } }
target.aa = { hh: 123 }
expect(handler).toBeCalledTimes(3)
expect(handler).nthCalledWith(1, undefined)
expect(handler).nthCalledWith(2, { dd: { ee: 123 } })
expect(handler).nthCalledWith(3, undefined)
expect(handler1).toBeCalledTimes(2)
expect(handler2).toBeCalledTimes(2)
})
test('shallow annotation', () => {
const target: any = {
aa: {},
}
define(target, {
aa: observable.shallow,
})
const handler = jest.fn()
const handler1 = jest.fn()
const handler2 = jest.fn()
autorun(() => {
handler(FormPath.getIn(target, 'aa.bb.cc'))
})
observe(target, handler1)
observe(target.aa, handler2)
target.aa.bb = { cc: { dd: { ee: 123 } } }
target.aa.bb.cc.kk = 333
target.aa = { hh: 123 }
expect(handler).toBeCalledTimes(3)
expect(handler).nthCalledWith(1, undefined)
expect(handler).nthCalledWith(2, { dd: { ee: 123 }, kk: 333 })
expect(handler).nthCalledWith(3, undefined)
expect(handler1).toBeCalledTimes(2)
expect(handler2).toBeCalledTimes(2)
})
test('box annotation', () => {
const target: any = {}
define(target, {
aa: observable.box,
})
const handler = jest.fn()
const handler1 = jest.fn()
const handler2 = jest.fn()
autorun(() => {
handler(target.aa.get())
})
observe(target, handler1)
observe(target.aa, handler2)
expect(handler).lastCalledWith(undefined)
target.aa.set(123)
expect(handler).toBeCalledTimes(2)
expect(handler).lastCalledWith(123)
expect(handler1).toBeCalledTimes(1)
expect(handler2).toBeCalledTimes(1)
})
test('ref annotation', () => {
const target: any = {}
define(target, {
aa: observable.ref,
})
const handler = jest.fn()
const handler1 = jest.fn()
autorun(() => {
handler(target.aa)
})
observe(target, handler1)
expect(handler).lastCalledWith(undefined)
target.aa = 123
expect(handler).toBeCalledTimes(2)
expect(handler).lastCalledWith(123)
expect(handler1).toBeCalledTimes(1)
})
test('action annotation', () => {
const target = {
aa: {
bb: null,
cc: null,
},
setData() {
target.aa.bb = 123
target.aa.cc = 312
},
}
define(target, {
aa: observable,
setData: batch,
})
const handler = jest.fn()
autorun(() => {
handler([target.aa.bb, target.aa.cc])
})
expect(handler).toBeCalledTimes(1)
target.setData()
expect(handler).toBeCalledTimes(2)
})
test('computed annotation', () => {
const handler = jest.fn()
const target = {
aa: 11,
bb: 22,
get cc() {
handler()
return this.aa + this.bb
},
}
define(target, {
aa: observable,
bb: observable,
cc: observable.computed,
})
autorun(() => {
target.cc
})
expect(handler).toBeCalledTimes(1)
expect(target.cc).toEqual(33)
target.aa = 22
expect(handler).toBeCalledTimes(2)
expect(target.cc).toEqual(44)
})
test('unexpect target', () => {
const testFn = jest.fn()
const testArr = []
const obs1 = define(4 as any, {
value: observable.computed,
})
const obs2 = define('123' as any, {
value: observable.computed,
})
const obs3 = define(testFn as any, {
value: observable.computed,
})
const obs4 = define(testArr as any, {
value: observable.computed,
})
expect(obs1).toBe(4)
expect(obs2).toBe('123')
expect(obs3).toBe(testFn)
expect(obs4).toBe(testArr)
})
})
test('define model', () => {
const obs = model({
aa: 1,
action() {
this.aa++
},
})
const { action } = obs
action()
expect(obs.aa).toEqual(2)
})