]> git.proxmox.com Git - extjs.git/blob - extjs/packages/core/test/specs/data/schema/legacy-association/HasMany.js
add extjs 6.0.1 sources
[extjs.git] / extjs / packages / core / test / specs / data / schema / legacy-association / HasMany.js
1 describe("Ext.data.association.HasMany_legacy", function() {
2
3 var rec;
4
5 function makeThread(id) {
6 rec = new spec.Thread({
7 id: id
8 });
9 }
10
11 function defineThread(cfg) {
12 Ext.define('spec.Thread', {
13 extend: 'Ext.data.Model',
14 fields: ['id', 'name'],
15 hasMany: Ext.apply({
16 model: 'spec.Post'
17 }, cfg)
18 });
19 }
20
21 beforeEach(function() {
22 Ext.data.Model.schema.setNamespace('spec');
23 Ext.define('spec.Post', {
24 extend: 'Ext.data.Model',
25 fields: ['title', 'content', 'user_id', 'thread_id']
26 });
27
28 Ext.define('spec.Site', {
29 extend: 'Ext.data.Model',
30 fields: ['hits']
31 });
32
33 Ext.define('spec.User', {
34 extend: 'Ext.data.Model',
35 fields: ['id', 'name'],
36 hasMany: 'spec.Post'
37 });
38 });
39
40 afterEach(function() {
41 Ext.undefine('spec.User');
42 Ext.undefine('spec.Post');
43 Ext.undefine('spec.Site');
44 Ext.undefine('spec.Thread');
45
46 Ext.data.Model.schema.clear(true);
47
48 rec = null;
49 });
50
51 describe("declarations", function() {
52 afterEach(function() {
53 Ext.undefine('spec.Foo');
54 });
55
56 var expectFn = function(key) {
57 expect(Ext.isFunction(spec.Foo.prototype[key])).toBe(true);
58 }
59
60 it("should read a single string", function() {
61 Ext.define('spec.Foo', {
62 extend: 'Ext.data.Model',
63 hasMany: 'spec.Post'
64 });
65 expectFn('posts');
66 });
67
68 it("should read an array of strings", function() {
69 Ext.define('spec.Foo', {
70 extend: 'Ext.data.Model',
71 hasMany: ['spec.Post', 'spec.Site']
72 });
73 expectFn('posts');
74 expectFn('sites');
75 });
76
77 it("should read a single object", function() {
78 Ext.define('spec.Foo', {
79 extend: 'Ext.data.Model',
80 hasMany: {
81 model: 'spec.Post'
82 }
83 });
84 expectFn('posts');
85 });
86
87 it("should read an array of objects", function() {
88 Ext.define('spec.Foo', {
89 extend: 'Ext.data.Model',
90 hasMany: [{
91 model: 'spec.Post'
92 }, {
93 model: 'spec.Site'
94 }]
95 });
96 expectFn('posts');
97 expectFn('sites');
98 });
99
100 it("should read an associations array", function() {
101 Ext.define('spec.Foo', {
102 extend: 'Ext.data.Model',
103 associations: [{
104 type: 'hasMany',
105 model: 'spec.Post'
106 }, {
107 type: 'hasMany',
108 model: 'spec.Site'
109 }]
110 });
111 expectFn('posts');
112 expectFn('sites');
113 });
114
115 it("should use the name parameter as the role", function() {
116 Ext.define('spec.Foo', {
117 extend: 'Ext.data.Model',
118 hasMany: {
119 model: 'spec.Post',
120 name: 'pastes'
121 }
122 });
123 expectFn('pastes');
124 });
125
126 it("should accept a storeConfig when given a name", function() {
127 Ext.define('spec.Foo', {
128 extend: 'Ext.data.Model',
129 hasMany: {
130 model: 'spec.Post',
131 name: 'hosts',
132 storeConfig: {
133 trackRemoved: false
134 }
135 }
136 });
137 var o = new spec.Foo();
138 expect(o.hosts().getTrackRemoved()).toBe(false);
139 });
140
141 it("should accept a storeConfig when given an associationKey", function() {
142 Ext.define('spec.Foo', {
143 extend: 'Ext.data.Model',
144 hasMany: {
145 model: 'spec.Post',
146 associationKey: 'asdf',
147 storeConfig: {
148 trackRemoved: false
149 }
150 }
151 });
152 var o = new spec.Foo();
153 expect(o.posts().getTrackRemoved()).toBe(false);
154 });
155 });
156
157 describe("instance", function() {
158 var makeRec = function() {
159 rec = new spec.User({
160 id: 3
161 });
162 };
163
164 var getStore = function() {
165 return rec['posts']();
166 };
167
168 it("should return a store", function() {
169 makeRec();
170 expect(getStore().isStore).toBe(true);
171 });
172
173 it("should set the appropriate model type", function() {
174 makeRec();
175 expect(getStore().model).toBe(spec.Post);
176 });
177
178 it("should return the same store instance on multiple calls", function() {
179 makeRec();
180 var s = getStore();
181 expect(getStore()).toBe(s);
182 });
183
184 it("should apply the storeConfig", function() {
185 defineThread({
186 storeConfig: {
187 autoLoad: true
188 }
189 });
190 makeThread(3);
191 var store = getStore();
192 expect(store.getAutoLoad()).toBe(true);
193 store.destroy();
194 });
195
196 describe("autoLoad", function() {
197 it("should not load the store by default", function() {
198 makeRec();
199 var spy = spyOn(Ext.data.ProxyStore.prototype, 'load').andReturn();
200 getStore();
201 expect(spy.callCount).toBe(0);
202 });
203
204 it("should load the store if configured with autoLoad: true", function() {
205 defineThread({
206 autoLoad: true
207 });
208
209 makeThread(3);
210 var spy = spyOn(Ext.data.ProxyStore.prototype, 'load').andReturn();
211 getStore();
212 expect(spy.callCount).toBe(1);
213 });
214 });
215
216 describe("keys", function() {
217
218 describe("foreignKey", function() {
219 it("should default to {modelName}_id", function() {
220 makeRec();
221 var post = getStore().add({})[0];
222 expect(post.get('user_id')).toBe(3);
223 });
224
225 it("should accept a user value", function() {
226 defineThread({
227 foreignKey: 'content'
228 });
229 makeThread(3);
230 var post = getStore().add({})[0];
231 expect(post.get('content')).toBe(3);
232 });
233 });
234
235 it("should set the primaryKey onto the foreignKey on add", function() {
236 makeRec();
237 var post = getStore().add({
238 'user_id': 1
239 })[0];
240 expect(post.get('user_id')).toBe(3);
241 })
242 });
243 });
244
245 describe("reading nested with assocationKey", function() {
246 var getStore = function() {
247 return rec.posts();
248 };
249
250 it("should default the key to association name", function() {
251 var reader = new Ext.data.reader.Json({
252 model: spec.User
253 });
254
255 rec = reader.read([{
256 id: 1,
257 name: 'Foo',
258 'posts': [{
259 title: 't1'
260 }, {
261 title: 't2'
262 }]
263 }]).getRecords()[0];
264
265 var posts = getStore();
266 expect(posts.getCount()).toBe(2);
267 expect(posts.first().get('title')).toBe('t1');
268 expect(posts.last().get('title')).toBe('t2');
269 });
270
271 it("should read a complex association", function() {
272 defineThread({
273 associationKey: 'nested.another[1].two'
274 });
275
276 var reader = new Ext.data.reader.Json({
277 model: spec.Thread
278 });
279
280 rec = reader.read([{
281 id: 1,
282 name: 'Foo',
283 nested: {
284 another: [{
285
286 }, {
287 two: [{
288 title: 't1'
289 }, {
290 title: 't2'
291 }]
292 }]
293 }
294 }]).getRecords()[0];
295
296 var posts = getStore();
297 expect(posts.getCount()).toBe(2);
298 expect(posts.first().get('title')).toBe('t1');
299 expect(posts.last().get('title')).toBe('t2');
300 });
301 });
302
303 describe("inverse association", function() {
304 it("should set the record if it has an inverse belongsTo", function() {
305 Ext.define('spec.Parent', {
306 extend: 'Ext.data.Model',
307 fields: ['id'],
308 hasMany: 'spec.Child'
309 });
310
311 spyOn(Ext.log, 'warn');
312
313 Ext.define('spec.Child', {
314 extend: 'Ext.data.Model',
315 fields: ['id', 'parent_id'],
316 belongsTo: 'spec.Parent'
317 });
318
319 var reader = new Ext.data.reader.Json({
320 model: spec.Parent
321 });
322
323 rec = reader.read([{
324 id: 1,
325 children: [{
326 id: 17
327 }]
328 }]).getRecords()[0];
329
330 var children = rec.children();
331 expect(children.first().getParent()).toBe(rec);
332
333 Ext.undefine('spec.Parent');
334 Ext.undefine('spec.Child');
335 });
336 });
337
338 });