]> git.proxmox.com Git - extjs.git/blame - extjs/packages/core/test/specs/data/schema/legacy-association/HasOne.js
add extjs 6.0.1 sources
[extjs.git] / extjs / packages / core / test / specs / data / schema / legacy-association / HasOne.js
CommitLineData
6527f429
DM
1describe("Ext.data.association.HasOne_legacy", function() {\r
2 \r
3 var rec;\r
4 \r
5 function definePerson(cfg, fields) {\r
6 Ext.define('spec.Person', {\r
7 extend: 'Ext.data.Model',\r
8 fields: fields || ['id', 'profile_id', 'aField'],\r
9 hasOne: Ext.apply({\r
10 model: 'spec.Profile'\r
11 }, cfg)\r
12 })\r
13 }\r
14 \r
15 function doSet(profile, options, scope) {\r
16 rec.setProfile(profile, options, scope);\r
17 }\r
18 \r
19 function doGet(options, scope) {\r
20 return rec.getProfile(options, scope);\r
21 }\r
22\r
23 function complete(data, status) {\r
24 Ext.Ajax.mockComplete({\r
25 status: status || 200,\r
26 responseText: Ext.JSON.encode(data)\r
27 });\r
28 }\r
29 \r
30 beforeEach(function() {\r
31 MockAjaxManager.addMethods();\r
32 Ext.data.Model.schema.setNamespace('spec');\r
33 Ext.define('spec.Profile', {\r
34 extend: 'Ext.data.Model',\r
35 fields: ['id', 'bio', 'age']\r
36 });\r
37 \r
38 Ext.define('spec.User', {\r
39 extend: 'Ext.data.Model',\r
40 fields: ['id', 'name', 'profile_id'],\r
41 hasOne: 'spec.Profile'\r
42 });\r
43 });\r
44 \r
45 afterEach(function() {\r
46 MockAjaxManager.removeMethods();\r
47 Ext.undefine('spec.User');\r
48 Ext.undefine('spec.Profile');\r
49 Ext.undefine('spec.Person');\r
50 \r
51 Ext.data.Model.schema.clear(true);\r
52 \r
53 rec = null;\r
54 });\r
55 \r
56 describe("declarations", function() {\r
57 afterEach(function() {\r
58 Ext.undefine('spec.Foo');\r
59 });\r
60 \r
61 var expectGetSet = function(getKey, setKey) {\r
62 var proto = spec.Foo.prototype;\r
63 expect(Ext.isFunction(proto[getKey])).toBe(true);\r
64 expect(Ext.isFunction(proto[setKey])).toBe(true);\r
65 }\r
66 \r
67 it("should read a single string", function() {\r
68 Ext.define('spec.Foo', {\r
69 extend: 'Ext.data.Model',\r
70 hasOne: 'spec.Profile'\r
71 });\r
72 expectGetSet('getProfile', 'setProfile');\r
73 }); \r
74 \r
75 it("should read an array of strings", function() {\r
76 Ext.define('spec.Bar', {\r
77 extend: 'Ext.data.Model'\r
78 });\r
79 \r
80 Ext.define('spec.Foo', {\r
81 extend: 'Ext.data.Model',\r
82 hasOne: ['spec.Profile', 'spec.Bar']\r
83 });\r
84 expectGetSet('getProfile', 'setProfile');\r
85 expectGetSet('getBar', 'setBar');\r
86 \r
87 Ext.undefine('spec.Bar');\r
88 });\r
89 \r
90 it("should read a single object", function() {\r
91 Ext.define('spec.Foo', {\r
92 extend: 'Ext.data.Model',\r
93 hasOne: {\r
94 model: 'spec.Profile'\r
95 } \r
96 }); \r
97 expectGetSet('getProfile', 'setProfile');\r
98 });\r
99 \r
100 it("should read an array of objects", function() {\r
101 Ext.define('spec.Bar', {\r
102 extend: 'Ext.data.Model'\r
103 });\r
104 \r
105 Ext.define('spec.Foo', {\r
106 extend: 'Ext.data.Model',\r
107 hasOne: [{\r
108 model: 'spec.Profile'\r
109 }, {\r
110 model: 'spec.Bar'\r
111 }]\r
112 });\r
113 \r
114 expectGetSet('getProfile', 'setProfile');\r
115 expectGetSet('getBar', 'setBar');\r
116 \r
117 Ext.undefine('spec.Bar');\r
118 });\r
119 \r
120 it("should read an associations array", function() {\r
121 Ext.define('spec.Bar', {\r
122 extend: 'Ext.data.Model'\r
123 });\r
124 \r
125 Ext.define('spec.Foo', {\r
126 extend: 'Ext.data.Model',\r
127 associations: [{\r
128 type: 'hasOne',\r
129 model: 'spec.Profile'\r
130 }, {\r
131 type: 'hasOne',\r
132 model: 'spec.Bar'\r
133 }]\r
134 });\r
135 \r
136 expectGetSet('getProfile', 'setProfile');\r
137 expectGetSet('getBar', 'setBar');\r
138 \r
139 Ext.undefine('spec.Bar');\r
140 });\r
141\r
142 it("should use the name parameter as the role", function() {\r
143 Ext.define('spec.Foo', {\r
144 extend: 'Ext.data.Model',\r
145 hasOne: {\r
146 model: 'spec.Profile',\r
147 name: 'noobfile'\r
148 }\r
149 });\r
150 expectGetSet('getNoobfile', 'setNoobfile');\r
151 });\r
152 });\r
153 \r
154 describe("getter", function() {\r
155 var profile, spy;\r
156\r
157 beforeEach(function() {\r
158 spy = jasmine.createSpy();\r
159 });\r
160\r
161 afterEach(function() {\r
162 spy = null;\r
163 });\r
164 \r
165 describe("instance already set", function() {\r
166 beforeEach(function() {\r
167 rec = new spec.User({\r
168 id: 4\r
169 });\r
170 \r
171 profile = new spec.Profile({\r
172 id: 2\r
173 });\r
174 \r
175 \r
176 doSet(profile);\r
177 });\r
178 \r
179 afterEach(function() {\r
180 profile = null;\r
181 }); \r
182 \r
183 it("should return the same instance", function() {\r
184 expect(doGet()).toBe(profile);\r
185 });\r
186 \r
187 it("should not attempt to load", function() {\r
188 spy = spyOn(spec.Profile.getProxy(), 'read');\r
189 doGet();\r
190 expect(spy).not.toHaveBeenCalled();\r
191 });\r
192 \r
193 it("should attempt to reload if called with options.reload", function() {\r
194 spy = spyOn(spec.Profile.getProxy(), 'read').andReturn();\r
195 doGet({\r
196 reload: true\r
197 }); \r
198 expect(spy).toHaveBeenCalled();\r
199 });\r
200 \r
201 describe("callbacks", function() {\r
202 it("should accept a function and default the scope to the model", function() {\r
203 doGet(spy);\r
204 var call = spy.mostRecentCall;\r
205 expect(call.args[0]).toBe(profile);\r
206 expect(call.object).toBe(rec);\r
207 });\r
208 \r
209 it("should accept a function with a scope", function() {\r
210 var o = {};\r
211 doGet(spy, o);\r
212 expect(spy.mostRecentCall.object).toBe(o); \r
213 });\r
214 \r
215 it("should accept an options object and call success", function() {\r
216 doGet({\r
217 success: spy\r
218 });\r
219 var call = spy.mostRecentCall;\r
220 expect(call.args[0]).toBe(profile);\r
221 expect(call.object).toBe(rec); \r
222 });\r
223 \r
224 it("should accept an options object and call callback", function() {\r
225 doGet({\r
226 callback: spy \r
227 });\r
228 var call = spy.mostRecentCall;\r
229 expect(call.args[0]).toBe(profile);\r
230 expect(call.object).toBe(rec); \r
231 });\r
232 });\r
233 });\r
234 \r
235 describe("instance not set", function() {\r
236 describe("keys", function() {\r
237 it("should default the primaryKey to 'id' and set it on the model", function() {\r
238 rec = new spec.User({\r
239 'profile_id': 10\r
240 });\r
241 profile = doGet();\r
242 expect(profile.get('id')).toBe(10); \r
243 });\r
244 \r
245 it("should use a custom foreign key", function() {\r
246 definePerson({\r
247 foreignKey: 'aField'\r
248 });\r
249 rec = new spec.Person({\r
250 'aField': 12\r
251 });\r
252 profile = doGet();\r
253 expect(profile.get('id')).toBe(12); \r
254 });\r
255 });\r
256 \r
257 describe("callbacks", function() {\r
258 it("should accept a function and the scope should default to the model", function() {\r
259 rec = new spec.User({\r
260 'profile_id': 3\r
261 }); \r
262 profile = doGet(spy);\r
263 complete({});\r
264 var call = spy.mostRecentCall;\r
265 expect(call.args[0]).toBe(profile);\r
266 expect(call.object).toBe(rec);\r
267 });\r
268 \r
269 it("should accept a function and a scope", function() {\r
270 rec = new spec.User({\r
271 'profile_id': 3\r
272 }); \r
273 var o = {};\r
274 doGet(spy, o);\r
275 complete({});\r
276 expect(spy.mostRecentCall.object).toBe(o);\r
277 }); \r
278 \r
279 it("should pass the options to load", function() {\r
280 rec = new spec.User({\r
281 'profile_id': 3\r
282 }); \r
283 \r
284 var spy = spyOn(spec.Profile.getProxy(), 'read');\r
285 doGet({\r
286 params: {\r
287 someKey: 1\r
288 }\r
289 });\r
290 expect(spy.mostRecentCall.args[0].getParams()).toEqual({\r
291 someKey: 1\r
292 });\r
293 });\r
294 });\r
295 \r
296 it("should return null if the foreignKey value is empty", function() {\r
297 rec = new spec.User();\r
298 expect(doGet()).toBeNull(); \r
299 });\r
300 });\r
301 });\r
302 \r
303 describe("setter", function() {\r
304 var spy;\r
305 beforeEach(function() {\r
306 spy = jasmine.createSpy();\r
307 rec = new spec.User({\r
308 id: 7\r
309 });\r
310 });\r
311\r
312 afterEach(function() {\r
313 spy = null;\r
314 });\r
315 \r
316 describe("instance", function() {\r
317 it("should have the same record reference", function() {\r
318 var profile = new spec.Profile({\r
319 id: 3\r
320 });\r
321 doSet(profile);\r
322 \r
323 expect(doGet()).toBe(profile);\r
324 });\r
325 \r
326 it("should set the underlying key value", function() {\r
327 var profile = new spec.Profile({\r
328 id: 3\r
329 });\r
330 doSet(profile);\r
331 expect(rec.get('profile_id')).toBe(3); \r
332 });\r
333 });\r
334 \r
335 describe("value", function() {\r
336 it("should set the underlying key", function() {\r
337 doSet(16);\r
338 expect(rec.get('profile_id')).toBe(16); \r
339 }); \r
340 \r
341 it("should keep the same reference if setting the value with a matching id", function() {\r
342 var profile = new spec.Profile({\r
343 id: 3\r
344 });\r
345 doSet(profile);\r
346 doSet(3);\r
347 expect(doGet()).toBe(profile);\r
348 });\r
349 \r
350 it("should clear the reference if a model is already set and a new id is passed", function() {\r
351 var profile = new spec.Profile({\r
352 id: 3\r
353 });\r
354 doSet(profile);\r
355 doSet(13);\r
356 spy = spyOn(spec.Profile.getProxy(), 'read');\r
357 // Reference doesn't exist, so need to grab it again here\r
358 doGet();\r
359 expect(spy.mostRecentCall.args[0].getId()).toBe(13);\r
360 });\r
361 \r
362 it("should set a custom foreignKey", function() {\r
363 definePerson({\r
364 foreignKey: 'aField'\r
365 });\r
366 rec = new spec.Person({\r
367 id: 1\r
368 }); \r
369 doSet(13);\r
370 expect(rec.get('aField')).toBe(13);\r
371 \r
372 });\r
373 });\r
374 \r
375 describe("callbacks", function() {\r
376 it("should accept a function as the second arg, scope should default to the model", function() {\r
377 doSet(16, spy);\r
378 complete({});\r
379 var call = spy.mostRecentCall;\r
380 expect(call.args[0]).toBe(rec);\r
381 expect(call.object).toBe(rec);\r
382 }); \r
383 \r
384 it("should accept a function with a scope", function() {\r
385 var o = {};\r
386 doSet(16, spy, o);\r
387 complete({});\r
388 expect(spy.mostRecentCall.object).toBe(o);\r
389 });\r
390\r
391 describe("options object", function() {\r
392 var successSpy, failureSpy, callbackSpy;\r
393\r
394 beforeEach(function() {\r
395 successSpy = jasmine.createSpy();\r
396 failureSpy = jasmine.createSpy();\r
397 callbackSpy = jasmine.createSpy();\r
398 });\r
399\r
400 afterEach(function() {\r
401 successSpy = failureSpy = callbackSpy = null;\r
402 });\r
403\r
404 describe("on success", function() {\r
405 it("should call success/callback and scope should default to the model", function() {\r
406 doSet(16, {\r
407 success: successSpy,\r
408 callback: callbackSpy,\r
409 failure: failureSpy\r
410 });\r
411 complete({});\r
412 expect(failureSpy).not.toHaveBeenCalled();\r
413 expect(successSpy).toHaveBeenCalled();\r
414 expect(callbackSpy).toHaveBeenCalled();\r
415 expect(successSpy.mostRecentCall.object).toBe(rec);\r
416 expect(callbackSpy.mostRecentCall.object).toBe(rec);\r
417 });\r
418\r
419 it("should use a passed scope", function() {\r
420 var scope = {};\r
421 doSet(16, {\r
422 scope: scope,\r
423 success: successSpy,\r
424 callback: callbackSpy\r
425 });\r
426 complete({});\r
427 expect(successSpy.mostRecentCall.object).toBe(scope);\r
428 expect(callbackSpy.mostRecentCall.object).toBe(scope);\r
429 });\r
430 });\r
431\r
432 describe("on failure", function() {\r
433 it("should call failure/callback and scope should default to the model", function() {\r
434 doSet(16, {\r
435 success: successSpy,\r
436 callback: callbackSpy,\r
437 failure: failureSpy\r
438 });\r
439 complete(null, 500);\r
440 expect(successSpy).not.toHaveBeenCalled();\r
441 expect(failureSpy).toHaveBeenCalled();\r
442 expect(callbackSpy).toHaveBeenCalled();\r
443 expect(failureSpy.mostRecentCall.object).toBe(rec);\r
444 expect(callbackSpy.mostRecentCall.object).toBe(rec);\r
445 });\r
446\r
447 it("should use a passed scope", function() {\r
448 var scope = {};\r
449 doSet(16, {\r
450 scope: scope,\r
451 failure: failureSpy,\r
452 callback: callbackSpy\r
453 });\r
454 complete(null, 500);\r
455 expect(failureSpy.mostRecentCall.object).toBe(scope);\r
456 expect(callbackSpy.mostRecentCall.object).toBe(scope);\r
457 });\r
458 });\r
459 });\r
460 });\r
461 });\r
462 \r
463 describe("reading nested with assocationKey", function() {\r
464 it("should default the key to the association name", function() {\r
465 var reader = new Ext.data.reader.Json({\r
466 model: spec.User\r
467 });\r
468 \r
469 rec = reader.read([{\r
470 id: 1,\r
471 'profile': {\r
472 id: 3\r
473 }\r
474 }]).getRecords()[0];\r
475 \r
476 expect(doGet().getId()).toBe(3);\r
477 });\r
478\r
479 it("should read when there is no attached field", function() {\r
480 definePerson(null, ['id']);\r
481 var reader = new Ext.data.reader.Json({\r
482 model: spec.Person\r
483 });\r
484 \r
485 rec = reader.read([{\r
486 id: 1,\r
487 profile: {\r
488 id: 3\r
489 }\r
490 }]).getRecords()[0];\r
491 expect(doGet().getId()).toBe(3);\r
492 });\r
493 \r
494 it("should read a complex association", function() {\r
495 definePerson({\r
496 associationKey: 'nested.another[1].two'\r
497 });\r
498 \r
499 var reader = new Ext.data.reader.Json({\r
500 model: spec.Person\r
501 });\r
502 \r
503 rec = reader.read([{\r
504 id: 1,\r
505 nested: {\r
506 another: [{\r
507 \r
508 }, {\r
509 two: {\r
510 id: 65\r
511 }\r
512 }]\r
513 }\r
514 }]).getRecords()[0];\r
515 expect(doGet().getId()).toBe(65);\r
516 });\r
517 });\r
518 \r
519 describe("inverse association", function() {\r
520 it("should set the record if it has an inverse belongsTo", function() {\r
521 Ext.define('spec.Parent', {\r
522 extend: 'Ext.data.Model',\r
523 fields: ['id'],\r
524 hasOne: 'spec.Child'\r
525 });\r
526 \r
527 spyOn(Ext.log, 'warn');\r
528 \r
529 Ext.define('spec.Child', {\r
530 extend: 'Ext.data.Model',\r
531 fields: ['id', 'parent_id'],\r
532 belongsTo: 'spec.Parent'\r
533 });\r
534 \r
535 var reader = new Ext.data.reader.Json({\r
536 model: spec.Parent\r
537 });\r
538 \r
539 rec = reader.read([{\r
540 id: 1,\r
541 'child': {\r
542 id: 17 \r
543 }\r
544 }]).getRecords()[0];\r
545 \r
546 var child = rec.getChild();\r
547 expect(child.getParent()).toBe(rec);\r
548 \r
549 Ext.undefine('spec.Parent');\r
550 Ext.undefine('spec.Child');\r
551 });\r
552 });\r
553 \r
554});