]> git.proxmox.com Git - extjs.git/blob - extjs/packages/core/test/specs/data/proxy/Server.js
add extjs 6.0.1 sources
[extjs.git] / extjs / packages / core / test / specs / data / proxy / Server.js
1 describe("Ext.data.proxy.Server", function() {
2 var proxy,
3 ServerProxy = Ext.data.proxy.Server,
4 reader = new Ext.data.reader.Reader(),
5 writer = new Ext.data.writer.Writer();
6
7 beforeEach(function() {
8 Ext.ClassManager.enableNamespaceParseCache = false;
9 });
10
11 afterEach(function(){
12 Ext.ClassManager.enableNamespaceParseCache = true;
13 Ext.data.Model.schema.clear();
14 Ext.undefine('spec.SomeModel');
15 });
16
17 describe("instantiation", function() {
18 var config;
19 beforeEach(function(){
20 config = {
21 extraParams: {
22 foo: true,
23 bar: false
24 },
25 reader: reader,
26 writer: writer
27 };
28 proxy = new ServerProxy(config);
29 });
30
31 it("should extend Ext.data.proxy.Proxy", function() {
32 expect(proxy.superclass).toEqual(Ext.data.proxy.Proxy.prototype);
33 });
34
35 it("should have caching disabled", function() {
36 expect(proxy.getNoCache()).toBe(true);
37 });
38
39 it("should have cacheString equal to _dc", function() {
40 expect(proxy.getCacheString()).toBe("_dc");
41 });
42
43 it("should have timeout equal to 30000", function() {
44 expect(proxy.getTimeout()).toBe(30000);
45 });
46
47 it("should have extraParams", function() {
48 expect(proxy.getExtraParams()).toEqual(config.extraParams);
49 });
50
51 it("should have reader", function() {
52 expect(proxy.getReader()).toBe(config.reader);
53 });
54
55 it("should have writer", function() {
56 expect(proxy.getWriter()).toBe(config.writer);
57 });
58 });
59
60 describe("CRUD Operations", function() {
61 beforeEach(function() {
62 proxy = new ServerProxy();
63 proxy.doRequest = jasmine.createSpy();
64 });
65
66 describe("create", function() {
67 it("should do a request", function() {
68 proxy.read('create', 'create');
69 expect(proxy.doRequest).toHaveBeenCalledWith('create', 'create');
70 });
71 });
72
73 describe("read", function() {
74 it("should do a request", function() {
75 proxy.read('read', 'read');
76 expect(proxy.doRequest).toHaveBeenCalledWith('read', 'read');
77 });
78 });
79
80 describe("update", function() {
81 it("should do a request", function() {
82 proxy.read('update', 'update');
83 expect(proxy.doRequest).toHaveBeenCalledWith('update', 'update');
84 });
85 });
86
87 describe("destroy", function() {
88 it("should do a request", function() {
89 proxy.read('destroy', 'destroy');
90 expect(proxy.doRequest).toHaveBeenCalledWith('destroy', 'destroy');
91 });
92 });
93 });
94
95 describe("buildRequest", function() {
96 describe("url", function() {
97 it("should use the proxy url", function() {
98 proxy = new ServerProxy({
99 url: 'foo',
100 noCache: false
101 });
102 var request = proxy.buildRequest(new Ext.data.operation.Read());
103 expect(request.getUrl()).toBe('foo');
104 });
105
106 it("should prefer the operation url", function() {
107 proxy = new ServerProxy({
108 url: 'foo',
109 noCache: false
110 });
111 var request = proxy.buildRequest(new Ext.data.operation.Read({
112 url: 'bar'
113 }));
114 expect(request.getUrl()).toBe('bar');
115 });
116 });
117 });
118
119 describe("buildUrl", function() {
120 var request = new Ext.data.Request({
121 url: 'keep'
122 }),
123 configWithNoCache = {
124 noCache: false
125 },
126 configWithCacheString = {
127 cacheString: '_cool'
128 };
129
130 beforeEach(function() {
131 spyOn(Ext.Date, "now").andReturn('bro');
132 });
133
134 it("should return keep?_dc=bro with an empty config", function() {
135 proxy = new ServerProxy({});
136 expect(proxy.buildUrl(request), 'keep?_dc=bro');
137 });
138
139 it("should disable caching", function() {
140 proxy = new ServerProxy(configWithNoCache);
141 expect(proxy.buildUrl(request), request.url);
142 });
143
144 it("should use cacheString", function() {
145 proxy = new ServerProxy(configWithCacheString);
146 expect(proxy.buildUrl(request), 'keep?_cool=bro');
147 });
148
149 describe("url precedence", function(){
150 it("should use the url on the proxy as a default", function(){
151 proxy = new ServerProxy({
152 url: 'proxy',
153 noCache: false
154 });
155 expect(proxy.buildUrl(new Ext.data.Request())).toBe('proxy');
156 });
157
158 it("should use the specified api by default", function(){
159 proxy = new ServerProxy({
160 api: {
161 read: 'read'
162 },
163 noCache: false
164 });
165 expect(proxy.buildUrl(new Ext.data.Request({
166 action: 'read'
167 }))).toBe('read');
168 });
169
170 it("should use the url on the request by default", function(){
171 proxy = new ServerProxy({
172 noCache: false
173 });
174 expect(proxy.buildUrl(new Ext.data.Request({
175 action: 'read',
176 url: 'request'
177 }))).toBe('request');
178 });
179
180 it("should use proxy url if the item in the proxy is undefined", function(){
181 proxy = new ServerProxy({
182 url: 'proxy',
183 api: {
184 read: 'read'
185 },
186 noCache: false
187 });
188 expect(proxy.buildUrl(new Ext.data.Request({
189 action: 'update'
190 }))).toBe('proxy');
191 });
192
193 it("should favour the api over the proxy url", function(){
194 proxy = new ServerProxy({
195 url: 'proxy',
196 api: {
197 read: 'read'
198 },
199 noCache: false
200 });
201 expect(proxy.buildUrl(new Ext.data.Request({
202 action: 'read'
203 }))).toBe('read');
204 });
205
206 it("should favour the request url over the proxy", function(){
207 proxy = new ServerProxy({
208 url: 'proxy',
209 noCache: false
210 });
211 expect(proxy.buildUrl(new Ext.data.Request({
212 action: 'update',
213 url: 'request'
214 }))).toBe('request');
215 });
216
217 it("should favour the request url over the api", function(){
218 proxy = new ServerProxy({
219 api: {
220 read: 'read'
221 },
222 noCache: false
223 });
224 expect(proxy.buildUrl(new Ext.data.Request({
225 action: 'read',
226 url: 'request'
227 }))).toBe('request');
228 });
229
230 it("should favour the request url over proxy & api", function(){
231 proxy = new ServerProxy({
232 url: 'proxy',
233 api: {
234 read: 'read'
235 },
236 noCache: false
237 });
238 expect(proxy.buildUrl(new Ext.data.Request({
239 action: 'read',
240 url: 'request'
241 }))).toBe('request');
242 });
243 });
244 });
245
246 describe("doRequest", function() {
247 it("should throw an error", function() {
248 expect(ServerProxy.prototype.doRequest).toThrow();
249 });
250 });
251
252 describe("getParams", function() {
253 var params, sorters, filters, grouper;
254
255 function createProxy(config) {
256 return new Ext.data.proxy.Server(config || {});
257 }
258
259 function createOperation(config) {
260 return new Ext.data.operation.Read(Ext.apply({}, config, {
261 page : 10,
262 start: 100,
263 limit: 10,
264
265 grouper: grouper,
266 sorters: sorters,
267 filters: filters
268 }));
269 }
270
271 function getParams(proxyConfig, operationConfig) {
272 var proxy = createProxy(proxyConfig),
273 operation = createOperation(operationConfig);
274
275 return proxy.getParams(operation);
276 }
277
278 beforeEach(function() {
279 sorters = [new Ext.util.Sorter({property: 'name', direction: 'ASC'})];
280 filters = [new Ext.util.Filter({property: 'name', value: 'Ed'})];
281 grouper = new Ext.util.Grouper({property: 'name', direction: 'ASC'});
282 });
283
284 describe("the page param", function() {
285 it("should default to 'page'", function() {
286 params = getParams();
287
288 expect(params.page).toBe(10);
289 });
290
291 it("should be customizable", function() {
292 params = getParams({pageParam: 'thePage'});
293
294 expect(params.thePage).toBe(10);
295 });
296
297 it("should not be sent if undefined", function() {
298 params = getParams({pageParam: undefined});
299
300 expect(params.page).toBeUndefined();
301 });
302 });
303
304 describe("the start param", function() {
305 it("should default to 'start'", function() {
306 params = getParams();
307
308 expect(params.start).toBe(100);
309 });
310
311 it("should be customizable", function() {
312 params = getParams({startParam: 'theStart'});
313
314 expect(params.theStart).toBe(100);
315 });
316
317 it("should not be sent if undefined", function() {
318 params = getParams({startParam: undefined});
319
320 expect(params.start).toBeUndefined();
321 });
322
323 it("should send a startParam of 0", function() {
324 params = getParams(undefined, {
325 start: 0
326 });
327
328 expect(params.start).toBe(0);
329 });
330 });
331
332 describe("the limit param", function() {
333 it("should default to 'limit'", function() {
334 params = getParams();
335
336 expect(params.limit).toBe(10);
337 });
338
339 it("should be customizable", function() {
340 params = getParams({limitParam: 'theLimit'});
341
342 expect(params.theLimit).toBe(10);
343 });
344
345 it("should not be sent if undefined", function() {
346 params = getParams({limitParam: undefined});
347
348 expect(params.limit).toBeUndefined();
349 });
350 });
351
352 describe("the group param", function() {
353
354 it("should default to 'group'", function() {
355 params = getParams();
356 expect(params.group).toBe('{"property":"name","direction":"ASC"}');
357 });
358
359 it("should be customizable", function() {
360 params = getParams({groupParam: 'theGroup'});
361
362 expect(params.theGroup).toBe('{"property":"name","direction":"ASC"}');
363 });
364
365 it("should not be sent if undefined", function() {
366 params = getParams({groupParam: undefined});
367
368 expect(params.group).toBeUndefined();
369 });
370
371 it("should not be set if there is no group defined", function() {
372 params = getParams({}, {grouper: undefined});
373
374 expect(params.group).toBeUndefined();
375 });
376 });
377
378 describe("the sort param", function() {
379 beforeEach(function() {
380 spyOn(Ext.data.proxy.Server.prototype, 'encodeSorters').andReturn("sorters");
381 });
382
383 it("should default to 'sort'", function() {
384 params = getParams();
385
386 expect(params.sort).toBe("sorters");
387 });
388
389 it("should be customizable", function() {
390 params = getParams({sortParam: 'theSorters'});
391
392 expect(params.theSorters).toBe("sorters");
393 });
394
395 it("should not be sent if undefined", function() {
396 params = getParams({sortParam: undefined});
397
398 expect(params.sort).toBeUndefined();
399 });
400
401 it("should encode the sorters", function() {
402 getParams();
403
404 expect(Ext.data.proxy.Server.prototype.encodeSorters).toHaveBeenCalledWith(sorters);
405 });
406
407 it("should not be set if there are no sorters", function() {
408 params = getParams({}, {sorters: []});
409
410 expect(params.sort).toBeUndefined();
411 });
412 });
413
414 describe("the filter param", function() {
415 beforeEach(function() {
416 spyOn(Ext.data.proxy.Server.prototype, 'encodeFilters').andReturn("filters");
417 });
418
419 it("should default to 'filter'", function() {
420 params = getParams();
421
422 expect(params.filter).toBe("filters");
423 });
424
425 it("should be customizable", function() {
426 params = getParams({filterParam: 'theFilters'});
427
428 expect(params.theFilters).toBe("filters");
429 });
430
431 it("should not be sent if undefined", function() {
432 params = getParams({filterParam: undefined});
433
434 expect(params.filter).toBeUndefined();
435 });
436
437 it("should encode the filters", function() {
438 getParams();
439
440 expect(Ext.data.proxy.Server.prototype.encodeFilters).toHaveBeenCalledWith(filters);
441 });
442
443 it("should not be set if there are no filters", function() {
444 params = getParams({}, {filters: []});
445
446 expect(params.filter).toBeUndefined();
447 });
448 });
449 });
450
451 describe("encoding sorters", function() {
452 it("should provide a default encoded string", function() {
453 var sorter1 = new Ext.util.Sorter({
454 property : "name",
455 direction: "ASC"
456 });
457
458 var sorter2 = new Ext.util.Sorter({
459 property : "age",
460 direction: "DESC"
461 });
462
463 proxy = new Ext.data.proxy.Server();
464
465 expect(Ext.decode(proxy.encodeSorters([sorter1, sorter2]))).toEqual([{
466 property: 'name',
467 direction: 'ASC'
468 }, {
469 property: 'age',
470 direction: 'DESC'
471 }]);
472 });
473 });
474
475 describe("encoding filters", function() {
476 it("should provide a default encoded string, operator should be excluded by default", function() {
477 var filter1 = new Ext.util.Filter({
478 property : "name",
479 value : "Ed"
480 });
481
482 var filter2 = new Ext.util.Filter({
483 property : "age",
484 value : 25,
485 operator: '>'
486 });
487
488 proxy = new Ext.data.proxy.Server();
489
490 expect(Ext.decode(proxy.encodeFilters([filter1, filter2]))).toEqual([{
491 property: 'name',
492 value: 'Ed'
493 }, {
494 property: 'age',
495 value: 25,
496 operator: '>'
497 }]);
498 });
499 });
500
501 describe("encoding group data", function() {
502 it("should JSON encode the data", function() {
503 var proxy = new Ext.data.proxy.Server(),
504 grouper = new Ext.util.Grouper({property: 'name', direction: 'ASC'});
505
506 expect(Ext.decode(proxy.encodeSorters([grouper], true))).toEqual({
507 property: 'name',
508 direction: 'ASC'
509 });
510 });
511 });
512
513 describe("reader accessors", function() {
514 var config,
515 sreader = 'xml',
516 defaultReaderType = 'xml',
517 modelName = 'spec.SomeModel',
518 model;
519
520
521 beforeEach(function(){
522 model = Ext.define(modelName, {
523 extend: 'Ext.data.Model',
524 fields: ['id']
525 });
526 });
527
528 describe("set the proxy's reader by reader instance", function() {
529 beforeEach(function(){
530 config = {
531 reader: reader
532 };
533 proxy = new ServerProxy(config);
534 });
535
536 it("should not create a new reader instance", function() {
537 var called = false;
538 spyOn(Ext, "createByAlias").andCallFake(function(name) {
539 if (name === 'reader.json') {
540 called = true;
541 }
542 });
543 expect(called).toBe(false);
544 });
545
546 it("should have a reader set", function() {
547 expect(proxy.getReader()).toEqual(reader);
548 });
549 });
550
551 describe("set the proxy's reader by string", function() {
552 beforeEach(function(){
553 config = {
554 reader: sreader,
555 proxy: proxy,
556 model: model,
557 defaultReaderType: defaultReaderType
558 };
559 proxy = new ServerProxy(config);
560 });
561
562 it("should create a new reader instance", function() {
563 expect(proxy.getReader().isReader).toBe(true);
564 });
565
566 it("should have a reader set", function() {
567 expect(proxy.getReader().$className).toBe('Ext.data.reader.Xml');
568 });
569 });
570 });
571
572 describe("writer accessors", function() {
573 var config,
574 swriter = 'xml',
575 defaultWriterType = 'xml',
576 modelName = 'spec.SomeModel', model;
577
578 beforeEach(function(){
579 model = Ext.define(modelName, {
580 extend: 'Ext.data.Model',
581 fields: ['id']
582 });
583 });
584
585 describe("set the proxy's writer by writer instance", function() {
586 beforeEach(function(){
587 config = {
588 writer: writer
589 };
590 proxy = new ServerProxy(config);
591 });
592
593 it("should not create a new writer instance", function() {
594 var called = false;
595 spyOn(Ext, "createByAlias").andCallFake(function(name) {
596 if (name === 'writer.json') {
597 called = false;
598 }
599 });
600 expect(called).toBe(false);
601 });
602
603 it("should have a writer set", function() {
604 expect(proxy.getWriter()).toEqual(writer);
605 });
606 });
607
608 describe("set the proxy's writer by string", function() {
609 beforeEach(function(){
610 config = {
611 writer: swriter,
612 model: model,
613 defaultWriterType: defaultWriterType
614 };
615 proxy = new ServerProxy(config);
616 });
617
618 it("should create a new writer instance", function() {
619 expect(proxy.getWriter().isWriter).toBe(true);
620 });
621
622 it("should have a writer set", function() {
623 expect(proxy.getWriter().$className).toBe('Ext.data.writer.Xml');
624 });
625 });
626 });
627
628 describe("destroy", function(){
629 var config, spy;
630
631 beforeEach(function(){
632 config = {
633 reader: reader,
634 writer: writer
635 };
636 proxy = new ServerProxy(config);
637 });
638
639 it('should destroy reader and writer', function(){
640 spy = spyOn(Ext, "destroy");
641 proxy.destroy();
642 expect(spy).toHaveBeenCalledWith(reader, writer);
643 });
644 });
645 });