]> git.proxmox.com Git - extjs.git/blame - extjs/packages/core/test/specs/Util.js
add extjs 6.0.1 sources
[extjs.git] / extjs / packages / core / test / specs / Util.js
CommitLineData
6527f429
DM
1describe("Ext.Util", function() {\r
2 describe("Ext.callback", function() {\r
3 var spy;\r
4\r
5 beforeEach(function() {\r
6 spy = jasmine.createSpy();\r
7 });\r
8\r
9 it('should not fail if given a null callback', function () {\r
10 expect(function() {\r
11 Ext.callback(null);\r
12 }).not.toThrow();\r
13 });\r
14\r
15 it('should not fail if given an undefined callback', function () {\r
16 expect(function() {\r
17 Ext.callback(undefined);\r
18 }).not.toThrow();\r
19 });\r
20\r
21 it('should not fail if given an invalid callback', function () {\r
22 expect(function() {\r
23 Ext.callback(42);\r
24 }).not.toThrow();\r
25 });\r
26\r
27 it("should pass arguments to the callback function", function() {\r
28 Ext.callback(spy, fakeScope, [1, 2, 3, 4, 6]);\r
29 expect(spy).toHaveBeenCalledWith(1, 2, 3, 4,6);\r
30 });\r
31\r
32 it("should be able to defer function call", function() {\r
33 runs(function() {\r
34 Ext.callback(spy, undefined, [1, 2, 3, 4, 6], 1);\r
35 expect(spy).not.toHaveBeenCalled();\r
36 });\r
37 waitsFor(function() {\r
38 return spy.callCount > 0;\r
39 }, "deferred callback never called");\r
40 runs(function() {\r
41 expect(spy).toHaveBeenCalledWith(1, 2, 3, 4, 6);\r
42 });\r
43\r
44 });\r
45\r
46 it("should return the return value of the given function", function () {\r
47 var x = 0,\r
48 fn = function (y) {\r
49 return x = this.z * 10 + y;\r
50 },\r
51 y = Ext.callback(fn, {z: 42}, [7]);\r
52\r
53 expect(x).toBe(427);\r
54 expect(y).toBe(427);\r
55 });\r
56 \r
57 describe("scoping", function() {\r
58 describe("with a function", function() {\r
59 describe("scope 'this'", function() {\r
60 it("should resolve the scope to the defaultScope", function() {\r
61 Ext.callback(spy, 'this', undefined, undefined, undefined, fakeScope);\r
62 expect(spy.mostRecentCall.object).toBe(fakeScope);\r
63 });\r
64\r
65 it("should resolve the scope to the caller", function() {\r
66 Ext.callback(spy, 'this', undefined, undefined, fakeScope);\r
67 expect(spy.mostRecentCall.object).toBe(fakeScope);\r
68 });\r
69\r
70 it("should prefer the defaultScope to the caller", function() {\r
71 Ext.callback(spy, 'this', undefined, undefined, {}, fakeScope);\r
72 expect(spy.mostRecentCall.object).toBe(fakeScope);\r
73 });\r
74\r
75 it("should fallback to global scope if no caller is passed", function() {\r
76 Ext.callback(spy, 'this');\r
77 expect(spy.mostRecentCall.object).toBe(Ext.global);\r
78 });\r
79 });\r
80\r
81 describe("scope 'controller'", function() {\r
82 it("should resolve the scope to the defaultScope", function() {\r
83 Ext.callback(spy, 'controller', undefined, undefined, undefined, fakeScope);\r
84 expect(spy.mostRecentCall.object).toBe(fakeScope);\r
85 });\r
86\r
87 it("should resolve the scope to the caller", function() {\r
88 Ext.callback(spy, 'controller', undefined, undefined, fakeScope);\r
89 expect(spy.mostRecentCall.object).toBe(fakeScope);\r
90 });\r
91\r
92 it("should prefer the defaultScope to the caller", function() {\r
93 Ext.callback(spy, 'controller', undefined, undefined, {}, fakeScope);\r
94 expect(spy.mostRecentCall.object).toBe(fakeScope);\r
95 });\r
96\r
97 it("should fallback to global scope if no caller is passed", function() {\r
98 Ext.callback(spy, 'controller');\r
99 expect(spy.mostRecentCall.object).toBe(Ext.global);\r
100 });\r
101 });\r
102\r
103 it("should execute the passed function in the specified scope", function() {\r
104 Ext.callback(spy, fakeScope);\r
105 expect(spy.mostRecentCall.object).toBe(fakeScope);\r
106 });\r
107 \r
108 it("should default the scope to Ext.global", function() {\r
109 Ext.callback(spy);\r
110 expect(spy.mostRecentCall.object).toBe(Ext.global);\r
111 });\r
112 });\r
113 \r
114 describe("with a string", function() {\r
115 var scopeInfo;\r
116 \r
117 beforeEach(function() {\r
118 scopeInfo = {\r
119 foo: function() {\r
120 \r
121 }\r
122 };\r
123 spyOn(scopeInfo, 'foo');\r
124 });\r
125 \r
126 describe("without caller", function() {\r
127 it("should throw if no scope is passed", function() {\r
128 expect(function() {\r
129 Ext.callback('foo');\r
130 }).toThrow();\r
131 });\r
132 \r
133 it("should throw if the method cannot be found on the passed scope", function() {\r
134 expect(function() {\r
135 Ext.callback('foo', {});\r
136 }).toThrow();\r
137 });\r
138\r
139 // Can't resolve the string scope without a caller\r
140 it("should throw if passed 'this' as scope", function() {\r
141 expect(function() {\r
142 Ext.callback('foo', 'this');\r
143 }).toThrow();\r
144 });\r
145\r
146 it("should throw if passed 'controller' as scope", function() {\r
147 expect(function() {\r
148 Ext.callback('foo', 'controller');\r
149 }).toThrow();\r
150 });\r
151 \r
152 it("should call the resolved method on the passed scope", function() {\r
153 Ext.callback('foo', scopeInfo);\r
154 expect(scopeInfo.foo).toHaveBeenCalled();\r
155 expect(scopeInfo.foo.mostRecentCall.object).toBe(scopeInfo);\r
156 });\r
157 \r
158 it("should retain scope on defer", function() {\r
159 runs(function() {\r
160 Ext.callback('foo', scopeInfo, undefined, 1);\r
161 expect(scopeInfo.foo).not.toHaveBeenCalled();\r
162 });\r
163 waitsFor(function() {\r
164 return scopeInfo.foo.callCount > 0;\r
165 }, "deferred callback never called");\r
166 runs(function() {\r
167 expect(scopeInfo.foo).toHaveBeenCalled();\r
168 expect(scopeInfo.foo.mostRecentCall.object).toBe(scopeInfo);\r
169 });\r
170 });\r
171 });\r
172 \r
173 describe("with caller", function() {\r
174 var theScope, caller;\r
175 beforeEach(function() {\r
176 theScope = {\r
177 foo: function() {\r
178 \r
179 }\r
180 };\r
181 caller = {\r
182 resolveListenerScope: function() {\r
183 return theScope;\r
184 }\r
185 };\r
186 spyOn(theScope, 'foo');\r
187 });\r
188 \r
189 describe("object scope", function() {\r
190 it("should favour a passed scope", function() {\r
191 Ext.callback('foo', scopeInfo, undefined, undefined, caller);\r
192 expect(scopeInfo.foo).toHaveBeenCalled();\r
193 expect(scopeInfo.foo.mostRecentCall.object).toBe(scopeInfo);\r
194 expect(theScope.foo).not.toHaveBeenCalled();\r
195 });\r
196 \r
197 it("should throw if the method cannot be found on the passed caller", function() {\r
198 expect(function() {\r
199 Ext.callback('fake', undefined, undefined, undefined, caller);\r
200 }).toThrow();\r
201 });\r
202 \r
203 it("should call the resolved method on the passed scope", function() {\r
204 Ext.callback('foo', undefined, undefined, undefined, caller);\r
205 expect(theScope.foo).toHaveBeenCalled();\r
206 expect(theScope.foo.mostRecentCall.object).toBe(caller.resolveListenerScope());\r
207 });\r
208 \r
209 it("should retain scope on defer", function() {\r
210 runs(function() {\r
211 Ext.callback('foo', undefined, undefined, 1, caller);\r
212 expect(theScope.foo).not.toHaveBeenCalled();\r
213 });\r
214 waitsFor(function() {\r
215 return theScope.foo.callCount > 0;\r
216 }, "deferred callback never called");\r
217 runs(function() {\r
218 expect(theScope.foo).toHaveBeenCalled();\r
219 expect(theScope.foo.mostRecentCall.object).toBe(caller.resolveListenerScope());\r
220 });\r
221 });\r
222 });\r
223\r
224 describe("scope: 'this'", function() {\r
225 it("should call resolveListenerScope on the caller", function() {\r
226 spyOn(caller, 'resolveListenerScope').andCallThrough();\r
227 Ext.callback('foo', 'this', undefined, undefined, caller);\r
228 expect(caller.resolveListenerScope).toHaveBeenCalledWith('this');\r
229 expect(theScope.foo).toHaveBeenCalled();\r
230 expect(theScope.foo.mostRecentCall.object).toBe(theScope);\r
231 });\r
232\r
233 it("should throw if the method cannot be found on the passed caller", function() {\r
234 expect(function() {\r
235 Ext.callback('fake', 'this', undefined, undefined, caller);\r
236 }).toThrow();\r
237 });\r
238\r
239 it("should retain scope on defer", function() {\r
240 runs(function() {\r
241 Ext.callback('foo', 'this', undefined, 1, caller);\r
242 expect(theScope.foo).not.toHaveBeenCalled();\r
243 });\r
244 waitsFor(function() {\r
245 return theScope.foo.callCount > 0;\r
246 }, "deferred callback never called");\r
247 runs(function() {\r
248 expect(theScope.foo).toHaveBeenCalled();\r
249 expect(theScope.foo.mostRecentCall.object).toBe(theScope);\r
250 });\r
251 });\r
252 });\r
253\r
254 describe("scope: 'controller'", function() {\r
255 it("should call resolveListenerScope on the caller", function() {\r
256 spyOn(caller, 'resolveListenerScope').andCallThrough();\r
257 Ext.callback('foo', 'controller', undefined, undefined, caller);\r
258 expect(caller.resolveListenerScope).toHaveBeenCalledWith('controller');\r
259 expect(theScope.foo).toHaveBeenCalled();\r
260 expect(theScope.foo.mostRecentCall.object).toBe(theScope);\r
261 });\r
262\r
263 it("should throw if the method cannot be found on the passed caller", function() {\r
264 expect(function() {\r
265 Ext.callback('fake', 'controller', undefined, undefined, caller);\r
266 }).toThrow();\r
267 });\r
268\r
269 it("should retain scope on defer", function() {\r
270 runs(function() {\r
271 Ext.callback('foo', 'controller', undefined, 1, caller);\r
272 expect(theScope.foo).not.toHaveBeenCalled();\r
273 });\r
274 waitsFor(function() {\r
275 return theScope.foo.callCount > 0;\r
276 }, "deferred callback never called");\r
277 runs(function() {\r
278 expect(theScope.foo).toHaveBeenCalled();\r
279 expect(theScope.foo.mostRecentCall.object).toBe(theScope);\r
280 });\r
281 });\r
282 });\r
283 });\r
284 });\r
285 });\r
286 }); // Ext.callback\r
287\r
288 describe('copyToIf String[]', function () {\r
289 var dest;\r
290 var source = { a: 1, b: 'x', c: 42 };\r
291\r
292 beforeEach(function () {\r
293 dest = { a: 427 };\r
294 });\r
295\r
296 it('should leave existing properties alone', function () {\r
297 Ext.copyToIf(dest, source, ['a']);\r
298 expect(dest).toEqual({ a: 427 });\r
299 });\r
300\r
301 it('should add new properties', function () {\r
302 Ext.copyToIf(dest, source, ['a','b']);\r
303 expect(dest).toEqual({ a: 427, b: 'x' });\r
304 });\r
305 });\r
306\r
307 describe('copyToIf String', function () {\r
308 var dest;\r
309 var source = { a: 1, b: 'x', c: 42 };\r
310\r
311 beforeEach(function () {\r
312 dest = { a: 427 };\r
313 });\r
314\r
315 it('should leave existing properties alone', function () {\r
316 Ext.copyToIf(dest, source, 'a');\r
317 expect(dest).toEqual({ a: 427 });\r
318 });\r
319\r
320 it('should add new properties', function () {\r
321 Ext.copyToIf(dest, source, 'a,b');\r
322 expect(dest).toEqual({ a: 427, b: 'x' });\r
323 });\r
324 })\r
325});\r