forked from kissyteam/kissy
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcomplex-spec.js
More file actions
239 lines (185 loc) · 6.08 KB
/
complex-spec.js
File metadata and controls
239 lines (185 loc) · 6.08 KB
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
/**
* complext tc for base and attribute
* @author yiminghe@gmail.com
*/
KISSY.use("base", function (S, Base) {
describe("complex base/attribute", function () {
it("can merge property value object from parent class", function () {
function a() {
a.superclass.constructor.apply(this, S.makeArray(arguments));
}
a.ATTRS = {
x:{
getter:function () {
return 1;
}
}
};
S.extend(a, Base);
function b() {
b.superclass.constructor.apply(this, S.makeArray(arguments));
}
b.ATTRS = {
x:{
value:2
}
};
S.extend(b, a);
var t = new b();
expect(t.get("x")).toBe(1);
});
it("support validator", function () {
function a() {
a.superclass.constructor.apply(this, S.makeArray(arguments));
}
a.ATTRS = {
tt:{
validator:function (v) {
return v > 1;
}
}
};
S.extend(a, Base);
var t = new a();
expect(t.set("tt", 10)).not.toBe(false);
expect(t.get("tt")).toBe(10);
expect(t.set("tt", 0)).toBe(false);
expect(t.get("tt")).toBe(10);
});
it("support validators", function () {
function Aa() {
Aa.superclass.constructor.apply(this, S.makeArray(arguments));
}
Aa.ATTRS = {
tt:{
validator:function (v, name,all) {
if (all && (v > all["t"])) {
return "tt>t!";
}
}
},
t:{
validator:function (v) {
if (v < 0) {
return "t<0!";
}
}
}
};
S.extend(Aa, Base);
var t = new Aa(),
e1;
expect(t.set("t", -1, {
error:function (v) {
e1 = v;
}
})).toBe(false);
expect(e1).toBe("t<0!");
expect(t.get("t")).not.toBe(-1);
var e2;
expect(t.set({
tt:2,
t:-1
}, {
error:function (v) {
e2 = v;
}
})).toBe(false);
expect(e2.sort()).toEqual(["t<0!", "tt>t!"].sort());
expect(t.get("t")).not.toBe(-1);
expect(t.get("tt")).not.toBe(2);
var e3;
expect(t.set({
tt:3,
t:4
}, {
error:function (v) {
e3 = v;
}
})).not.toBe(false);
expect(e3).toBeUndefined();
expect(t.get("t")).toBe(4);
expect(t.get("tt")).toBe(3);
});
it("support sub attribute name", function () {
function a() {
a.superclass.constructor.apply(this, S.makeArray(arguments));
}
a.ATTRS = {
tt:{
// do not use this in real world code
// forbid changing value in getter
getter:function (v) {
v.x.y++;
return v;
},
setter:function (v) {
v.x.y++;
return v;
}
}
};
S.extend(a, Base);
var t = new a({
tt:{
x:{
y:1
}
}
});
var ret = [], getterVal, setterVal;
t.on("beforeTtChange", function (e) {
ret.push(e.prevVal.x.y);
ret.push(e.newVal.x.y);
});
t.on("afterTtChange", function (e) {
ret.push(e.prevVal.x.y);
ret.push(e.newVal.x.y);
});
// only can when tt is a object (not custom object newed from custom clz)
expect(t.get("tt.x.y")).toBe(3);
t.set("tt.x.y", 3);
expect(t.get("tt.x.y")).toBe(5);
expect(ret).toEqual([4, 3, 4, 4]);
});
it("should fire *Change once for set({})", function () {
function a() {
a.superclass.constructor.apply(this, S.makeArray(arguments));
}
S.extend(a, Base);
var aa = new a({x:1, y:{z:1}}),
ok = 0,
afterAttrChange = {};
aa.on("*Change", function (e) {
expect(e.newVal).toEqual([11, {z:22}]);
expect(e.prevVal).toEqual([1, {z:1}]);
expect(e.attrName).toEqual(["x", "y"]);
expect(e.subAttrName).toEqual(["x", "y.z"]);
ok++;
});
aa.on("afterXChange", function (e) {
expect(e.attrName).toBe("x");
expect(e.newVal).toBe(11);
expect(e.prevVal).toBe(1);
expect(e.subAttrName).toBe("x");
afterAttrChange.x = 1;
});
aa.on("afterYChange", function (e) {
expect(e.attrName).toBe("y");
expect(e.newVal).toEqual({z:22});
expect(e.prevVal).toEqual({z:1});
expect(e.subAttrName).toBe("y.z");
afterAttrChange.y = 1;
});
aa.set({
x:11,
"y.z":22
});
expect(aa.get("x")).toBe(11);
expect(aa.get("y.z")).toBe(22);
expect(ok).toBe(1);
expect(afterAttrChange.x).toBe(1);
expect(afterAttrChange.y).toBe(1);
});
});
});